Esempio n. 1
0
  public void run() {

    if (!star) {
      System.out.println(
          "Run PCC de "
              + zoneOrigine
              + ":"
              + origine
              + " vers "
              + zoneDestination
              + ":"
              + destination);
    }
    if (star) {
      System.out.println(
          "Run PCC-Star de "
              + zoneOrigine
              + ":"
              + origine
              + " vers "
              + zoneDestination
              + ":"
              + destination);
    }

    // decision temps ou distance. Si on est en mode covoiturage, ce n'est pas nécessaire car on est
    // forcément en temps
    if (covoit == 0) {
      Scanner sc = new Scanner(System.in);
      System.out.println("En temps(0) ou distance(1)?");
      if (sc.nextInt() == 1) {
        this.setdist(true);
      }
    }
    // chronometrage
    long start_time = System.currentTimeMillis();

    Label l = new Label(false, 0.0, null, this.graphe.sommets.get(origine));
    chem.add(l);
    tas.insert(l);
    maplabel.put(this.graphe.sommets.get(origine), l);

    // affichage de l'origine et de la destination
    this.graphe.getDessin().setColor(Color.BLUE);
    this.graphe
        .getDessin()
        .drawPoint(
            this.graphe.sommets.get(origine).getlon(),
            this.graphe.sommets.get(origine).getlat(),
            10);
    this.graphe
        .getDessin()
        .drawPoint(
            this.graphe.sommets.get(destination).getlon(),
            this.graphe.sommets.get(destination).getlat(),
            10);

    // compteur d'elements explores
    int nb_explor = 0;
    int tas_max = 0;

    while (!(tas.isEmpty())) {
      // extraction du minimum
      Label pere = tas.findMin();
      tas.deleteMin();

      // on arrete le programme une fois la destination trouvee, sauf si l'on ne souhaite pas
      // arreter ---> covoiturage
      if (pere.getCourant().getNum() == destination && (covoit == 0)) {
        System.out.println("Algorithme termine!");
        break;
      }

      pere.setMarquage(true);
      chem.add(pere);

      for (Route route : pere.getCourant().routes) {
        Sommet suiv = route.getArrivee();
        // affichage du parcours en temps reel. On ne le fait pas dans l'optimisation de covoiturage
        // car cela surcharge trop le dessin.
        if (this.getcovoit() == 0) {
          this.graphe.getDessin().setColor(Color.gray);
          this.graphe
              .getDessin()
              .drawLine(
                  suiv.getlon(),
                  suiv.getlat(),
                  pere.getCourant().getlon(),
                  pere.getCourant().getlat());
        }
        // declarations des variables de cout, cout secondaire et estimation
        double new_cout = 0.0;

        double estim = 0.0; // ESTIMATION VERY IMPORTANT

        double cout_sec = 0.0;

        if (this.dist_temps) // Cout n distance
        {
          double vitesse = ((double) (route.getDesc().vitesseMax()));
          new_cout = pere.getCout() + ((double) (route.getDist()));

          // cout secondaire, ici en temps
          cout_sec = pere.getCoutSec() + (60.0 * ((double) (route.getDist())) / (1000.0 * vitesse));
          if (this.star) {
            estim = suiv.dist_vol_oiseau(this.graphe.sommets.get(destination));
          }
        } else {
          // Cout en temps
          double vitesse = ((double) (route.getDesc().vitesseMax()));
          // cas du pieton
          if (this.getcovoit() == 1 && (vitesse == 130.0 || vitesse == 110.0)) {

            // ici, le pieton ne peut pas acceder a cette route,
            // elle est reservee aux voitures, le cout est donc le plus grand possible
            new_cout = Double.MAX_VALUE;
          } else {
            if (this.getcovoit() == 1) {
              vitesse = 4.0;
            }
            new_cout = pere.getCout() + (60.0 * ((double) (route.getDist())) / (1000.0 * vitesse));

            // cout secondaire, ici en distance
            cout_sec = pere.getCoutSec() + ((double) (route.getDist()));
          }

          // Ici on modifie le calcul de l'estimation dans le cas d'un calcul Astar.  VERY IMPORTANT
          if (this.star) {
            estim =
                suiv.cout_vol_oiseau(this.graphe.sommets.get(destination), this.graphe.getvitmax());
          }
        }

        // on regarde si le sommet a deja un label associe avec un cout different de l'infini)
        // pour ce faire, on verifie juste qu'il est integre a la hashmap ou pas
        if (maplabel.containsKey(suiv)) {
          maplabel.get(suiv).setEstim(estim);

          // maj du label, et de la hashmap
          // on ne verifie pas si le sommet est marque : en effet, s'il l'est, son cout
          // est deja minimal et la condition suivante sera toujours fausse.
          if (maplabel.get(suiv).getCout() > new_cout) {
            maplabel.get(suiv).setCout(new_cout);
            maplabel.get(suiv).setPere(pere.getCourant());
            maplabel.get(suiv).setCoutSec(cout_sec);
            tas.update(maplabel.get(suiv));
          }
        } else {
          // le sommet a un cout infini : on cree un label, et on l'integre a la hashmap
          Label lab = new Label(false, new_cout, pere.getCourant(), suiv);
          lab.setEstim(estim);
          lab.setCoutSec(cout_sec);
          tas.insert(lab);
          maplabel.put(suiv, lab);

          // compteurs de performance
          nb_explor++;
          if (tas.size() > tas_max) {
            tas_max = tas.size();
          }
        }
      }
    }
    // on affiche le chemin
    this.print_chemin(this.graphe.sommets.get(destination));

    // fin du chronometre
    long end_time = System.currentTimeMillis();
    long difference = end_time - start_time;

    // on affiche les performances
    System.out.println("Temps écoulé en millisecondes : " + difference);
    System.out.println("Elements explorés : " + nb_explor);
    System.out.println("Taille max du tas : " + tas_max);
  }
Esempio n. 2
0
 public String insererSommet(Sommet<U, T, V> sommet) {
   return sommet.setGraphe(this);
 }