All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.rinde.rinsim.examples.warehouse.WarehouseExample Maven / Gradle / Ivy

/*
 * Copyright (C) 2011-2017 Rinde van Lon, imec-DistriNet, KU Leuven
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.github.rinde.rinsim.examples.warehouse;

import static com.google.common.collect.Lists.newArrayList;

import java.util.Map;

import javax.measure.unit.SI;

import com.github.rinde.rinsim.core.Simulator;
import com.github.rinde.rinsim.core.model.road.CollisionGraphRoadModelImpl;
import com.github.rinde.rinsim.core.model.road.RoadModelBuilders;
import com.github.rinde.rinsim.geom.Graph;
import com.github.rinde.rinsim.geom.Graphs;
import com.github.rinde.rinsim.geom.LengthData;
import com.github.rinde.rinsim.geom.ListenableGraph;
import com.github.rinde.rinsim.geom.Point;
import com.github.rinde.rinsim.geom.TableGraph;
import com.github.rinde.rinsim.ui.View;
import com.github.rinde.rinsim.ui.renderers.AGVRenderer;
import com.github.rinde.rinsim.ui.renderers.WarehouseRenderer;
import com.google.common.collect.ImmutableTable;
import com.google.common.collect.Lists;
import com.google.common.collect.Table;

/**
 * Example showcasing the {@link CollisionGraphRoadModelImpl} with an
 * {@link WarehouseRenderer} and {@link AGVRenderer}.
 * @author Rinde van Lon
 */
public final class WarehouseExample {

  private static final double VEHICLE_LENGTH = 2d;
  private static final int NUM_AGVS = 20;
  private static final long TEST_END_TIME = 10 * 60 * 1000L;
  private static final int TEST_SPEED_UP = 16;

  private WarehouseExample() {}

  /**
   * @param args - No args.
   */
  public static void main(String[] args) {
    run(false);
  }

  /**
   * Runs the example.
   * @param testing If true the example will run in testing mode,
   *          automatically starting and stopping itself such that it can be run
   *          from a unit test.
   */
  public static void run(boolean testing) {
    View.Builder viewBuilder = View.builder()
      .with(WarehouseRenderer.builder()
        .withMargin(VEHICLE_LENGTH))
      .with(AGVRenderer.builder()
        .withDifferentColorsForVehicles());

    if (testing) {
      viewBuilder = viewBuilder.withAutoPlay()
        .withAutoClose()
        .withSimulatorEndTime(TEST_END_TIME)
        .withTitleAppendix("TESTING")
        .withSpeedUp(TEST_SPEED_UP);
    } else {
      viewBuilder = viewBuilder.withTitleAppendix("Warehouse Example");
    }

    final Simulator sim = Simulator.builder()
      .addModel(
        RoadModelBuilders.dynamicGraph(GraphCreator.createSimpleGraph())
          .withCollisionAvoidance()
          .withDistanceUnit(SI.METER)
          .withVehicleLength(VEHICLE_LENGTH))
      .addModel(viewBuilder)
      .build();

    for (int i = 0; i < NUM_AGVS; i++) {
      sim.register(new AGVAgent(sim.getRandomGenerator()));
    }

    sim.start();
  }

  static class GraphCreator {
    static final int LEFT_CENTER_U_ROW = 4;
    static final int LEFT_CENTER_L_ROW = 5;
    static final int LEFT_COL = 4;
    static final int RIGHT_CENTER_U_ROW = 2;
    static final int RIGHT_CENTER_L_ROW = 4;
    static final int RIGHT_COL = 0;

    GraphCreator() {}

    static ImmutableTable createMatrix(int cols,
        int rows, Point offset) {
      final ImmutableTable.Builder builder =
        ImmutableTable.builder();
      for (int c = 0; c < cols; c++) {
        for (int r = 0; r < rows; r++) {
          builder.put(r, c, new Point(
            offset.x + c * VEHICLE_LENGTH * 2,
            offset.y + r * VEHICLE_LENGTH * 2));
        }
      }
      return builder.build();
    }

    static ListenableGraph createSimpleGraph() {
      final Graph g = new TableGraph<>();

      final Table matrix = createMatrix(8, 6,
        new Point(0, 0));

      for (int i = 0; i < matrix.columnMap().size(); i++) {

        final Iterable path;
        if (i % 2 == 0) {
          path = Lists.reverse(newArrayList(matrix.column(i).values()));
        } else {
          path = matrix.column(i).values();
        }
        Graphs.addPath(g, path);
      }

      Graphs.addPath(g, matrix.row(0).values());
      Graphs.addPath(g, Lists.reverse(newArrayList(matrix.row(
        matrix.rowKeySet().size() - 1).values())));

      return new ListenableGraph<>(g);
    }

    static ListenableGraph createGraph() {
      final Graph g = new TableGraph<>();

      final Table leftMatrix = createMatrix(5, 10,
        new Point(0, 0));
      for (final Map column : leftMatrix.columnMap().values()) {
        Graphs.addBiPath(g, column.values());
      }
      Graphs.addBiPath(g, leftMatrix.row(LEFT_CENTER_U_ROW).values());
      Graphs.addBiPath(g, leftMatrix.row(LEFT_CENTER_L_ROW).values());

      final Table rightMatrix = createMatrix(10, 7,
        new Point(30, 6));
      for (final Map row : rightMatrix.rowMap().values()) {
        Graphs.addBiPath(g, row.values());
      }
      Graphs.addBiPath(g, rightMatrix.column(0).values());
      Graphs.addBiPath(g, rightMatrix.column(rightMatrix.columnKeySet().size()
        - 1).values());

      Graphs.addPath(g,
        rightMatrix.get(RIGHT_CENTER_U_ROW, RIGHT_COL),
        leftMatrix.get(LEFT_CENTER_U_ROW, LEFT_COL));
      Graphs.addPath(g,
        leftMatrix.get(LEFT_CENTER_L_ROW, LEFT_COL),
        rightMatrix.get(RIGHT_CENTER_L_ROW, RIGHT_COL));

      return new ListenableGraph<>(g);
    }
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy