Menu

Tutorial_NewProblem

Anita Thomas D Melanie Schranz
Attachments
05CreateNewComponent.png (33439 bytes)
08RunConfig.png (41724 bytes)
09ProblemDefSim.png (53428 bytes)
10ProblemDefSimViz.png (2558 bytes)
11ProblemDefReplay.png (60638 bytes)
EmergencyExit.jpg (17783 bytes)

In this tutorial we will implement a problem definition. In particular, it will be a simulation where agents try to find an emergency exit. As a prerequisite, you should have installed Java, eclipse, downloaded Frevo and imported it as an eclipse project.

Let's start:

  • Run FREVO and open the Component Creator

  • A window opens. Here you have to select the type of the component you want to create:
    • Problem with absolute fitness
    • Problem with tournament fitness
    • Method
    • Representation
    • Ranking

We will select "Problem". Then you have to enter a name and a short description.

  • Click on "Create" and follow the instructions that are shown. A new folder, containing your .java file, and an .xml file will be generated. (in this case "EmergencyExit.java" and "EmergencyExit.xml").

The generated .java file looks like this:


public class EmergencyExit extends AbstractSingleProblem {

  @Override
  protected double evaluateCandidate(AbstractRepresentation candidate) {
    return 0;
  }

  @Override
  public void replayWithVisualization(AbstractRepresentation candidate) {
  }

  @Override
  public double getMaximumFitness() {
    return Double.MAX_VALUE;
  }
}

  • evaluateCandidate() is called to simulate without visualization, it should return a fitness value.
  • replayWithVisualization() is (as the name says) called to replay the simulation with a (possible graphical) visualization.
  • getMaximumFitness() should return the maximum Fitness that can be reached. If this Value is not known Double.MAX_VALUE should be returned. This value can be used by the optimization algorithm to stop optimization if this value has been reached

Implement your simulation - it is useful to extract it in an own function, so you can call it from getResult() and replayWithVisualization).
For this tutorial we start with a very simple simulation, that needs two additonal packages:


import net.jodk.lang.FastMath;
import java.util.ArrayList;


  int steps;
  int xpositionofEmergencyExit = 0;
  int ypositionofEmergencyExit = 0;
  int width;
  int height;
  int xpositionofAgent;
  int ypositionofAgent;
  AbstractRepresentation c;

  void calcSim(){

    xpositionofEmergencyExit = Integer.parseInt(getProperties().get("xpositionofEmergencyExit").getValue());
    ypositionofEmergencyExit = Integer.parseInt(getProperties().get("ypositionofEmergencyExit").getValue());
    xpositionofAgent = Integer.parseInt(getProperties().get("xpositionofAgent").getValue());
    ypositionofAgent = Integer.parseInt(getProperties().get("ypositionofAgent").getValue());

    for (int step = 0; step < steps; step++) {
      ArrayList<Float> input = new ArrayList<Float>();
      input.add((float) (xpositionofEmergencyExit - xpositionofAgent));
      input.add((float) (ypositionofEmergencyExit - ypositionofAgent));

      ArrayList<Float> output = c.getOutput(input);

      float xVelocity = output.get(0).floatValue()*2.0f-1.0f;
      float yVelocity = output.get(1).floatValue()*2.0f-1.0f;

      if (xVelocity >= 1.0 && xpositionofAgent < width - 1) xpositionofAgent += 1;
      else if (xVelocity <= -1.0 && xpositionofAgent > 0 ) xpositionofAgent -= 1;
      if (yVelocity >= 1.0 && ypositionofAgent < height - 1) ypositionofAgent += 1;
      else if (yVelocity <= -1.0 && ypositionofAgent > 0) ypositionofAgent -= 1;
    }
  }

The position of the emergency exit and the agent are read from the .xml file which is accessed over getProperties().get(name).getValue(). name represents the name of the value in the .xml file.

The value of "steps", "width" and "height" are written in the functions evaluateCandidate() and replayWithVisualization().

The main function of FREVO is to find the best function between input and the output automatically. So you just have to collect all the inputs and the representation (here it is c) will return the output. It is important that all the inputs and all the outputs are always in the same order. The output is always a float value between 0.0 and 1.0. You have to decide how to handle these outputs. In this simulation the output defines how the agent moves.

Now, after the functionality of the simulation is implemented, we need to call it in evaluateCandidate().


protected double evaluateCandidate(AbstractRepresentation candidate) {
  steps = Integer.parseInt(getProperties().get("steps").getValue());
  width = Integer.parseInt(getProperties().get("width").getValue());
  height = Integer.parseInt(getProperties().get("height").getValue());
  c = candidate;

  calcSim();

  return -FastMath.hypot(xpositionofEmergencyExit - xpositionofAgent, ypositionofEmergencyExit - ypositionofAgent);
  }

As we said before the values of “steps”, “width” and “height” have to be written in this function. They are read from the .xml file. The return value of this function says how good this representation was. It the value is high, the connection of input and output is good. For the EmergencyExit simulation this value is the negative distance between the agent and the emergency exit. So if the agent reaches the emergency exit within the amount of steps, the distance will be 0, thus, it is a good way of connecting input and output.

So, we have to edit EmergencyExit.xml in order to add the component-specific properties:


  <properties>
  <propentry key="xpositionofEmergencyExit" type="INT" value="99"/>
  <propentry key="ypositionofEmergencyExit" type="INT" value="99"/>
  <propentry key="width" type="INT" value="100"/>
  <propentry key="height" type="INT" value="100"/>
  <propentry key="xpositionofAgent" type="INT" value="50"/>
  <propentry key="ypositionofAgent" type="INT" value="50"/>
  <propentry key="steps" type="INT" value="50"/>
  </properties>

and to configure the number of inputs and outputs for the agent:

    <reqentry key="inputnumber" type="INT" value="2"/>
    <reqentry key="outputnumber" type="INT" value="2"/>

Finally, we have to implement a graphical visualization for the simulation:


import frevoutils.JGridMap.Display;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JFrame;
import GridVisualization.WhiteBoard;


    WhiteBoard whiteboard = null;
    Display display =null;

    public void replayWithVisualization(AbstractRepresentation candidate) {
        steps = 0;
        c = candidate;
        width = Integer.parseInt(getProperties().get("width").getValue());
        height = Integer.parseInt(getProperties().get("height").getValue());
        display = new Display(440, 495, "SimplifiedEmergencyExit");
        display.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        whiteboard = new WhiteBoard(400, 400, width, height, 1);
        whiteboard.addColorToScale(0, Color.WHITE);
        whiteboard.addColorToScale(1, Color.BLACK);
        whiteboard.addColorToScale(2, Color.GREEN);
        JButton minusbutton = new JButton("-");
        JButton plusbutton = new JButton("+");
        display.add(whiteboard);
        display.add(minusbutton);
        display.add(plusbutton);
        minusbutton.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                if (steps > 0)
                    steps--;
                calcSim();
                displayResult();
                display.setTitle("Simplified Emergency Exit    Step: " + steps);
            }
        });
        plusbutton.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                steps++;
                calcSim();
                displayResult();
                display.setTitle("Simplified Emergency Exit    Step: " + steps);
            }
        });
        display.setVisible(true);
        calcSim();
        displayResult();
        display.setTitle("Simplified Emergency Exit    Step: " + steps);
    }

    private void displayResult() {
        int[][] data = new int[width][height];
        for (int x = 0; x < width; x++) {
            for (int y = 0; y < height; y++) {
                if /* */ (x == xpositionofEmergencyExit && y == ypositionofEmergencyExit)
                    data[x][y] = 2;
                else if (x == xpositionofAgent /*    */ && y == ypositionofAgent)
                    /* */data[x][y] = 1;
                else
                    /* */data[x][y] = 0;
            }
        }

        whiteboard.setData(data);
        whiteboard.repaint();
    }

Before running FREVO, check if the right class is used as main in Run - Run Configurations...

Click on the Run button in Eclipse and FREVO will open. Use following settings for the simulation:

  • Selected Problem: EmergencyExit
  • Selected Method: CEA2D
  • Selected Representation: FullyMeshedNet
  • Selected Ranking: Absolute Ranking

Beside the simulation another window appears. It shows the quality of the solution in the search space with a color code ranging from best solution (green) to worst solution (red).

Finally, you can hit two times the Replay button, and you can have a look of the Emergency Exit solution. By pressing the + or - button you can follow the agent (black square) finding the exit (green square).


MongoDB Logo MongoDB