Ejemplo n.º 1
0
  /*
   * (non-Javadoc)
   *
   * @see java.lang.Runnable#run()
   */
  public void run() {
    // Do not run something terminated
    if (isTerminated()) return;

    logger.info("Run optimizer");
    generation = 0;
    while (!isTerminated()) {
      pauseLock.lock();
      try {
        while (isPaused) unpaused.await();
        if (isTerminated) return;
      } catch (InterruptedException ie) {
        // Finish
        return;
      } finally {
        pauseLock.unlock();
      }

      //
      // Initialise the population with a dummy individual
      //
      if (population.isEmpty()) {
        Solution solution = new Solution();
        for (Node_Variable variable : request.variables())
          solution.add(new Binding(variable, Node.NULL));
        population.add(solution);
      }

      // Increment the generation counter
      ++generation;

      //
      // Generate a new set of offspring and copy the parents into it
      // first
      //
      // logger.info("Generate");
      Set<Solution> newPopulation = new HashSet<Solution>();
      newPopulation.addAll(population); // Add the parents
      generateOp.createPopulation(population, newPopulation);

      //
      // Evaluate all of them
      //
      // logger.info("Evaluate " + newPopulation.size());
      // Counts the number of different solutions
      evaluationsCounter += newPopulation.size() - population.size();
      evaluateOp.evaluatePopulation(newPopulation);

      /*
       * String buffer = "Fitnesses "; for (Solution s : newPopulation)
       * buffer += s.getFitness() + " "; logger.info(buffer);
       */

      // Provide feed back to the generation operator
      generateOp.updateProviderRewards(newPopulation);

      //
      // Get rid of the previous population and insert the kids
      //
      // logger.info("Cut");
      population.clear();
      population.addAll(newPopulation);
      while (population.size() > POPULATION_SIZE) population.remove(population.first());

      //
      // Track for optimality
      //
      double topFitness = population.last().getFitness();
      for (Solution s : population) {
        // Increment age
        if (s.getFitness() != topFitness) s.resetAge();
        s.incrementAge();

        // Check optimality
        s.setOptimal(false);
        if (s.getAge() >= MAXIMUM_GENERATION && s.getFitness() > 0) s.setOptimal(true);
        if (s.getFitness() == 1.0d) s.setOptimal(true);

        // If the solution is optimal add its (valid!) triples to the
        // black
        // list
        if (s.isOptimal()) {
          synchronized (blackListedTriples) {
            blackListedTriples.addAll(request.getTripleSet(s));
          }
        }

        // Print solution
        // logger.info(s.toString());
      }

      logger.info("Generation " + generation + ", best fitness=" + topFitness);
      for (Solution s : population) logger.info(s.toString());

      //
      // Notify observers that a loop has been done
      //
      setChanged();
      notifyObservers(population);

      // for (Solution s : population)
      //	if (s.isOptimal())
      //		this.terminate();

      //
      // Wait a bit for the data layer
      //
      datalayer.waitForLatencyBuffer();

      //
      // Remove all optimum individuals from the population
      //
      List<Solution> toRemove = new ArrayList<Solution>();
      for (Solution s : population) if (s.isOptimal()) toRemove.add(s);
      population.removeAll(toRemove);
    }
  }
Ejemplo n.º 2
0
 @Override
 public void actionPerformed(ActionEvent a) {
   JTextField mapWidthInput = new JTextField("11"), mapHeightInput = new JTextField("11");
   JPanel optionPanel = new JPanel(new GridLayout(0, 1));
   width = 0;
   height = 0;
   String Width = "", Height = "";
   optionPanel.add(new JLabel("Map Width: "));
   optionPanel.add(mapWidthInput);
   optionPanel.add(new JLabel("Map Height: "));
   optionPanel.add(mapHeightInput);
   int result =
       JOptionPane.showConfirmDialog(
           Main.parentFrame,
           optionPanel,
           "Map Options",
           JOptionPane.OK_CANCEL_OPTION,
           JOptionPane.PLAIN_MESSAGE);
   if (result == JOptionPane.OK_OPTION) {
     Width = mapWidthInput.getText();
     Height = mapHeightInput.getText();
     try {
       width = Integer.parseInt(Width);
       height = Integer.parseInt(Height);
     } catch (NumberFormatException err) {
       JOptionPane.showMessageDialog(null, "Is it that hard to just use numbers? \n" + err);
       return;
     }
   }
   if (width >= 900 && width <= 10
       || height >= 600
       || height <= 10
       || width % 2 == 0
       || height % 2 == 0) {
     JOptionPane.showMessageDialog(
         null,
         "Invalid Input(s). Width must be 11-899 and height must be 11-599. Only odd numbers.");
     return;
   }
   startTime = System.currentTimeMillis();
   map = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
   int GX = 0, GY = 0, maxGX = 0, maxGY = 0;
   cells = new cell[width][height];
   for (int x = 0; x < width; x++) {
     for (int y = 0; y < height; y++) {
       if (x % 2 == 0 || y % 2 == 0) map.setRGB(x, y, Color.black.getRGB());
       else map.setRGB(x, y, Color.white.getRGB());
       if (y % 2 == 1)
         if (x % 2 == 1) {
           cells[GX][GY] = new cell(x, y, GX, GY);
           // JOptionPane.showMessageDialog(Main.parentFrame,
           // "GX: " + GX + " GY: " + GY);
           GY++;
         }
       if (GX > maxGX) maxGX = GX;
       if (GY > maxGY) maxGY = GY;
     }
     if (x % 2 == 1) {
       GX++;
       GY = 0;
     }
   }
   image = (Image) map;
   scale = Math.min(600 / height, 900 / width);
   Main.Canvas.setBorder(BorderFactory.createLineBorder(Color.black));
   Main.map = map;
   image = map.getScaledInstance(width * scale, height * scale, Image.SCALE_DEFAULT);
   Main.Canvas.setIcon(new ImageIcon(image));
   Random r = new Random(System.currentTimeMillis());
   int startX = r.nextInt(maxGX + 1), startY = r.nextInt(maxGY + 1);
   startCell = currentCell = cells[startX][startY];
   Generate inst = new Generate();
   inst.execute();
   Main.map = null;
   Main.checkMapBuffered();
 }