コード例 #1
0
    /*
     * Completely erases the current state of the Simulation
     */
    public void reset() {
      e.purgeAgents();
      paint.clearHistory();
      children.clear();
      fillWithAgents();

      Agent a = new Agent();
      a.setName("My Agent");
      a.setChrome("000 000 000 000 000 000 000 011 011 011 011 011 100 101 101 101");
      children.add(a);

      cur_gen = 0;
      current_elite = null;
      prev_elite_total = 0;
      running = true;
    }
コード例 #2
0
    public void run() {

      // keep the thread alive
      while (true) {

        // wait for a signal to re-continue processing agents
        if (!running) {
          synchronized (this) {
            try {
              wait();
            } catch (InterruptedException ie) {
              System.err.println("ERROR: wait() call failed" + ie);
            }
          }
        }

        // run for the number of generations specified
        while (cur_gen < generations && running) {

          // create our list of agents
          e.purgeAgents();
          e.addAgents(children);

          // Process 100-days
          for (int i = 0; i < day_count; i++) {
            e.simulateDay();
          }

          // record this generation
          history.addGeneration(children);

          // sort the results by money, descending
          Collections.sort(children);
          Collections.reverse(children);

          // if the elite total changed, alert the main class
          int elite_most_money = children.get(0).getMoney();
          if (elite_most_money > prev_elite_total) {

            // cache the new elite total
            prev_elite_total = elite_most_money;
            current_elite = children.get(0);

            ep.setNewElite(current_elite);
            ep.repaint();

            // fire an event to the main class
            listener.actionPerformed(
                new ActionEvent(this, ActionEvent.ACTION_PERFORMED, "elite_total"));
          }

          // now we need to select agents for crossover, mutation,
          // and elites for elites, keep the first x%
          List<Agent> elites = children.subList(0, (int) (children.size() * elite_percent));

          // for parents to keep, simply choose the top y%
          // (including the elites)
          List<Agent> parents = children.subList(0, (int) (children.size() * parent_percent));

          // create some children, and perform mutations
          children = evo.performCrossover(parents);
          children = evo.performMutation(children);

          // add the elite agents into the pool
          children.addAll(elites);

          // System.out.println(elites.get(0) + ": " + elites.get(0).getMoney());

          // reset the elites for the next generation
          for (Agent elitea : elites) elitea.reset();

          // repopulate the next generation with new Agents
          fillWithAgents();

          cur_gen++;
        }
        running = false;
      }
    }