Example #1
0
  @Override
  public void start() {
    super.start();

    environment =
        new Continuous2D(1.0, config.getEnvironmentWidth(), config.getEnvironmentHeight());
    drawProxy = new DrawProxy(environment.getWidth(), environment.getHeight());
    environment.setObjectLocation(drawProxy, new Double2D());

    physicsWorld = new World(new Vec2());
    placementArea =
        new PlacementArea((float) environment.getWidth(), (float) environment.getHeight());
    placementArea.setSeed(config.getSimulationSeed());
    schedule.reset();
    System.gc();

    physicsWorld.setContactListener(contactListener);

    // Create ALL the objects
    createWalls();
    createTargetArea();
    robotFactory.placeInstances(
        placementArea.new ForType<>(), physicsWorld, config.getTargetAreaPlacement());
    config.getResourceFactory().placeInstances(placementArea.new ForType<>(), physicsWorld);

    // Now actually add the objects that have been placed to the world and schedule
    for (PhysicalObject object : placementArea.getPlacedObjects()) {
      drawProxy.registerDrawable(object.getPortrayal());
      schedule.scheduleRepeating(object);
    }

    schedule.scheduleRepeating(
        simState -> physicsWorld.step(TIME_STEP, VELOCITY_ITERATIONS, POSITION_ITERATIONS));
  }
Example #2
0
  // Walls are simply added to environment since they do not need updating
  private void createWalls() {
    int environmentWidth = config.getEnvironmentWidth();
    int environmentHeight = config.getEnvironmentHeight();
    // Left
    Double2D pos = new Double2D(0, environmentHeight / 2.0);
    Double2D v1 = new Double2D(0, -pos.y);
    Double2D v2 = new Double2D(0, pos.y);
    WallObject wall = new WallObject(physicsWorld, pos, v1, v2);
    drawProxy.registerDrawable(wall.getPortrayal());

    // Right
    pos = new Double2D(environmentWidth, environmentHeight / 2.0);
    wall = new WallObject(physicsWorld, pos, v1, v2);
    drawProxy.registerDrawable(wall.getPortrayal());

    // Top
    pos = new Double2D(environmentWidth / 2.0, 0);
    v1 = new Double2D(-pos.x, 0);
    v2 = new Double2D(pos.x, 0);
    wall = new WallObject(physicsWorld, pos, v1, v2);
    drawProxy.registerDrawable(wall.getPortrayal());

    // Bottom
    pos = new Double2D(environmentWidth / 2.0, environmentHeight);
    wall = new WallObject(physicsWorld, pos, v1, v2);
    drawProxy.registerDrawable(wall.getPortrayal());
  }
Example #3
0
  public Simulation(SimConfig config, RobotFactory robotFactory) {
    super(config.getSimulationSeed());
    this.config = config;
    this.robotFactory = robotFactory;

    Settings.velocityThreshold = VELOCITY_THRESHOLD;
  }
Example #4
0
  private double getRobotAvgPolygonArea() {
    Set<PhysicalObject> objects = placementArea.getPlacedObjects();
    double totalArea = 0.0;

    for (PhysicalObject object : objects) {
      if (object instanceof RobotObject) {
        totalArea += ((RobotObject) object).getAverageCoveragePolgygonArea();
      }
    }
    return totalArea / config.getObjectsRobots();
  }
Example #5
0
 /** Gets the progress of the simulation as a percentage */
 public double getProgressFraction() {
   return (double) schedule.getSteps() / config.getSimulationIterations();
 }
Example #6
0
 public boolean allResourcesCollected() {
   return config.getResourceFactory().getNumberOfResources()
       == targetArea.getNumberOfContainedResources();
 }
Example #7
0
 /** Run the simulation for the number of iterations specified in the config. */
 public void run() {
   final int iterations = config.getSimulationIterations();
   runForNIterations(iterations);
 }
Example #8
0
 @Override
 public void setSeed(long seed) {
   super.setSeed(seed);
   config.setSimulationSeed(seed);
 }
Example #9
0
  // create target area
  private void createTargetArea() {
    int environmentWidth = config.getEnvironmentWidth();
    int environmentHeight = config.getEnvironmentHeight();

    final int width, height;
    final Vec2 position;
    if (config.getTargetAreaPlacement() == SimConfig.Direction.SOUTH) {
      width = environmentWidth;
      height = config.getTargetAreaThickness();
      position = new Vec2(width / 2f, height / 2f);
    } else if (config.getTargetAreaPlacement() == SimConfig.Direction.NORTH) {
      width = environmentWidth;
      height = config.getTargetAreaThickness();
      position = new Vec2(environmentWidth - width / 2f, environmentHeight - height / 2f);
    } else if (config.getTargetAreaPlacement() == SimConfig.Direction.EAST) {
      width = config.getTargetAreaThickness();
      height = environmentHeight;
      position = new Vec2(environmentWidth - width / 2f, height / 2f);
    } else if (config.getTargetAreaPlacement() == SimConfig.Direction.WEST) {
      width = config.getTargetAreaThickness();
      height = environmentHeight;
      position = new Vec2(width / 2f, height / 2f);
    } else {
      return; // Don't know where to place this target area
    }

    targetArea =
        new TargetAreaObject(
            physicsWorld,
            position,
            width,
            height,
            config.getResourceFactory().getTotalResourceValue(),
            config.getSimulationIterations());

    // Add target area to placement area (trust that space returned since nothing else placed
    // yet).
    PlacementArea.Space space = placementArea.getRectangularSpace(width, height, position, 0f);
    placementArea.placeObject(space, targetArea);
  }