/** Start the background thread. */
  public void start() {
    // create a random list of cities

    cities = new City[TravelingSalesman.CITY_COUNT];
    for (int i = 0; i < TravelingSalesman.CITY_COUNT; i++) {
      cities[i] =
          new City(
              (int) (Math.random() * (getBounds().width - 10)),
              (int) (Math.random() * (getBounds().height - 60)));
    }

    // create the initial chromosomes

    chromosomes = new Chromosome[TravelingSalesman.POPULATION_SIZE];
    for (int i = 0; i < TravelingSalesman.POPULATION_SIZE; i++) {
      chromosomes[i] = new Chromosome(cities);
      chromosomes[i].setCut(cutLength);
      chromosomes[i].setMutation(TravelingSalesman.MUTATION_PERCENT);
    }
    Chromosome.sortChromosomes(chromosomes, TravelingSalesman.POPULATION_SIZE);

    // start up the background thread
    started = true;
    map.update(map.getGraphics());

    generation = 0;

    if (worker != null) worker = null;
    worker = new Thread(this);
    // worker.setPriority(Thread.MIN_PRIORITY);
    worker.start();
  }
Beispiel #2
1
 public void run() {
   Refs.setSim(this);
   Refs.setCanvas(canvas);
   Refs.setEnts(ents);
   Refs.setMap(map);
   // TODO TEMP
   ents.add(new Creature());
   //
   long startTime = 0;
   long endTime = 0;
   long timeElapsed = 0;
   int delay = 1000 / rate.getValue();
   averageX = topQuarterX = 0;
   smooth = false;
   while (true) {
     smooth = smoothMotion.isSelected();
     startTime = System.currentTimeMillis();
     canvas.repaint();
     map.update();
     ents.update();
     if (ents.hasPopulationAbove(0)) {
       averageX = ents.getAverageX();
       topQuarterX = ents.getTopQuarterX();
     }
     endTime = System.currentTimeMillis();
     timeElapsed = endTime - startTime;
     delay = 1000 / rate.getValue();
     Util.sleep(Util.trimInt((int) (delay - timeElapsed), 0, 99999999));
   }
 }
  /** Start the background thread. */
  public void start() {
    // create a random list of cities

    cities = new City[TravelingSalesman.CITY_COUNT];
    for (int i = 0; i < TravelingSalesman.CITY_COUNT; i++) {
      cities[i] =
          new City(
              (int) (Math.random() * (getBounds().width - 10)),
              (int) (Math.random() * (getBounds().height - 60)));
    }

    // start up the background thread
    started = true;
    map.update(map.getGraphics());

    if (worker != null) worker = null;
    worker = new SimulateAnnealing(this);
    worker.setPriority(Thread.MIN_PRIORITY);
    worker.start();
  }
  @Override
  public void update(float delta) {
    if (!isGameOver) {
      List<TouchEvent> events = stateManager.getTouchHandler().getTouchEvents();
      if (!isExitDialogShowing) isExitDialogShowing = stateManager.isBackPressed();
      if (isExitDialogShowing) {
        events = yesButton.update(events);
        events = noButton.update(events);
        if (yesButton.state == Button.ACTIVATED) {
          yesButton.disarm();
          stateManager.setState(new MainMenuState(stateManager));
        }
        if (noButton.state == Button.ACTIVATED) {
          noButton.disarm();
          isExitDialogShowing = false;
        }
      } else {
        if (switchTurns) {

          if (map.getPlayer2() == map.getCurrentPlayersTurn()) events.clear();

          events = switchTurnsOKButton.update(events);
          events.clear();
          if (switchTurnsOKButton.state == Button.ACTIVATED) {
            switchTurnsOKButton.disarm();
            switchTurns = false;
            checkWin = true;
            map.doneSwitchingTurns();
          }
        }
        map.update(events);
      }
    } else {
      // TODO: maybe take us to a game breakdown state which records stats for the match
      // right now, just take us back to the main menu
      stateManager.setState(new MainMenuState(stateManager));
    }
  }
Beispiel #5
1
  public static void main(String[] args) throws IOException {

    Map map = Parser.readMap("example1.map");

    Robot r = new Robot(map, Parser.posX, Parser.posY);

    map.printMap();
    saveState(map, r);

    while (!r.isOver && !map.killedRobot) {
      int type = getInput(r);
      if (type == 0) {
        map.update();
        map.printMap();
        play++;
        saveState(map, r);
      }

      if (type == 1) {
        undo();
      }
    }
  }
  /** The main loop for the background thread. It is here that most of the work os orchestrated. */
  public void run() {

    double thisCost = 500.0;
    double oldCost = 0.0;
    double dcost = 500.0;
    int countSame = 0;

    map.update(map.getGraphics());

    while (countSame < 100) {

      generation++;

      int ioffset = matingPopulationSize;
      int mutated = 0;

      // Mate the chromosomes in the favoured population
      // with all in the mating population
      for (int i = 0; i < favoredPopulationSize; i++) {
        Chromosome cmother = chromosomes[i];
        // Select partner from the mating population
        int father = (int) (0.999999 * Math.random() * (double) matingPopulationSize);
        Chromosome cfather = chromosomes[father];

        mutated += cmother.mate(cfather, chromosomes[ioffset], chromosomes[ioffset + 1]);
        ioffset += 2;
      }

      // The new generation is in the matingPopulation area
      // move them to the correct area for sort.
      for (int i = 0; i < matingPopulationSize; i++) {
        chromosomes[i] = chromosomes[i + matingPopulationSize];
        chromosomes[i].calculateCost(cities);
      }

      // Now sort the new mating population
      Chromosome.sortChromosomes(chromosomes, matingPopulationSize);

      double cost = chromosomes[0].getCost();
      dcost = Math.abs(cost - thisCost);
      thisCost = cost;
      double mutationRate = 100.0 * (double) mutated / (double) matingPopulationSize;

      NumberFormat nf = NumberFormat.getInstance();
      nf.setMinimumFractionDigits(2);
      nf.setMinimumFractionDigits(2);

      status.setText(
          "Generation "
              + generation
              + " Cost "
              + (int) thisCost
              + " Mutated "
              + nf.format(mutationRate)
              + "%");

      if ((int) thisCost == (int) oldCost) {
        countSame++;
      } else {
        countSame = 0;
        oldCost = thisCost;
      }
      map.update(map.getGraphics());
    }
    status.setText("Solution found after " + generation + " generations.");
  }
 public void paint() {
   map.update(getGraphics());
 }