Advertisement
Guest User

Untitled

a guest
Dec 14th, 2019
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 13.61 KB | None | 0 0
  1. package sim.app.exploration.env;
  2.  
  3. import java.lang.reflect.Constructor;
  4. import java.util.Vector;
  5.  
  6. import java.io.*;
  7.  
  8. import sim.engine.SimState;
  9. import sim.engine.Steppable;
  10. import sim.field.grid.SparseGrid2D;
  11. import sim.util.Bag;
  12. import sim.util.Int2D;
  13. import sim.app.exploration.agents.*;
  14. import sim.app.exploration.objects.Bush;
  15. import sim.app.exploration.objects.SimObject;
  16. import sim.app.exploration.objects.Tree;
  17. import sim.app.exploration.objects.Water;
  18. import sim.app.exploration.objects.Antelope;
  19. import sim.app.exploration.objects.Zebra;
  20.  
  21. public class SimEnvironment implements Steppable{
  22.  
  23.     private static final long serialVersionUID = 1L;
  24.  
  25.     private SparseGrid2D world;
  26.    
  27.     private Vector<ExplorerAgent> explorers;
  28.     private MapperAgent mapper;
  29.     private BrokerAgent broker;
  30.     private Class[][] occupied;
  31.    
  32.     private int step = 0;
  33.     private final int maxSteps = 5000;
  34.    
  35.     FileWriter writer;
  36.    
  37.     public SimEnvironment(SimState state, int width, int height, int nAgents){
  38.        
  39.         this.world = new SparseGrid2D(width, height);
  40.         this.occupied = new Class[width][height];
  41.        
  42.         this.explorers = new Vector<ExplorerAgent>(nAgents);
  43.         this.mapper = new MapperAgent(width, height);
  44.         this.broker = new BrokerAgent();
  45.                                  
  46.         this.setup(state);
  47.        
  48.         try {
  49.             writer = new FileWriter("stats.csv");
  50.         } catch (IOException e) {
  51.             e.printStackTrace();
  52.         }
  53.     }
  54.    
  55.     /**
  56.      * This method should setup the environment: create the objects and populate
  57.      * it with them and with the explorer agents
  58.      */
  59.     private void setup(SimState state){
  60.        
  61.         addExplorersRandomly(state);
  62.         //addExplorersCornersCenter(state); // This always adds 8 Explorers
  63.        
  64.         buildRandomMap(state);
  65.         //buildMap(state);
  66.        
  67.         //APAGAR
  68.         //buildDonutMap(state);
  69.         //buildStructuredMap(state);
  70.        
  71.     }
  72.    
  73.     /* Explorer Adding Methods */
  74.    
  75.     private void addExplorersRandomly(SimState state) {
  76.         for(int i= 0; i < explorers.capacity(); i++){
  77.             Int2D loc = new Int2D(state.random.nextInt(world.getWidth()),state.random.nextInt(world.getHeight()));
  78.             addExplorer(state, loc);
  79.         }
  80.     }
  81.    
  82.     private void addExplorersCornersCenter(SimState state) {
  83.         // 4 Explorers in the center of the map
  84.         for (int i = 0; i < 4; i++) {
  85.             Int2D loc = new Int2D(world.getWidth() / 2, world.getHeight() / 2);
  86.             addExplorer(state, loc);
  87.         }
  88.        
  89.         // 4 Explorers on all 4 corners
  90.         Int2D locs[] = new Int2D[4];
  91.         locs[0] = new Int2D(0, 0);
  92.         locs[1] = new Int2D(world.getWidth(), world.getHeight());
  93.         locs[2] = new Int2D(0, world.getHeight());
  94.         locs[3] = new Int2D(world.getWidth(), 0);
  95.        
  96.         for (Int2D l : locs)
  97.             addExplorer(state, l);
  98.        
  99.     }
  100.    
  101.     private void addExplorer(SimState state, Int2D loc) {
  102.         ExplorerAgent explorer = new ExplorerAgent(loc);
  103.         explorers.add(explorer);
  104.        
  105.         mapper.updateLocation(explorer,loc);
  106.         this.updateLocation(explorer, loc);
  107.         explorer.env = this;
  108.         explorer.mapper = mapper;
  109.         explorer.broker = broker;
  110.     }
  111.    
  112.     /* Map Generation Methods */
  113.    
  114.     private void buildMap(SimState state) {
  115.         Int2D loc;
  116.         Int2D groupLoc;
  117.        
  118.         // Number of instances per block
  119.         int numInstancesWater = 300;//x2 lakes
  120.         int numInstancesBush = 300; //all over the map
  121.         int numInstancesTree = 50; //all over the map
  122.         int numInstancesAntelope = 100;
  123.         int numInstancesZebra = 20; //all over the map
  124.                
  125.         int height_separation = world.getHeight()/4;
  126.         int width_separation = world.getWidth()/4;
  127.         int sep = 50;
  128.                
  129.         //bottom left corner lake
  130.         for(int j = 0; j < numInstancesWater; j++) {
  131.             do {
  132.                 loc = new Int2D(state.random.nextInt(width_separation) + sep, state.random.nextInt(height_separation) + 2*height_separation + sep);
  133.             }while ( occupied[loc.x][loc.y] != null);
  134.             addObject(Water.class, loc);
  135.         }
  136.        
  137.         //top center lake
  138.         for(int j = 0; j < numInstancesWater; j++) {
  139.             do {
  140.                 loc = new Int2D(state.random.nextInt(width_separation) + 2*width_separation, state.random.nextInt(height_separation) + sep);
  141.             }while ( occupied[loc.x][loc.y] != null);
  142.             addObject(Water.class, loc);
  143.         }
  144.        
  145.         //bushes all over the environment
  146.         for(int j = 0; j < numInstancesBush; j++) {
  147.             loc = singleRandomLocation(state, sep, width_separation, height_separation);
  148.             addObject(Bush.class, loc);
  149.         }
  150.        
  151.         //trees all over the environment
  152.         for(int j = 0; j < numInstancesTree; j++) {
  153.             loc = singleRandomLocation(state, sep, width_separation, height_separation);
  154.             addObject(Tree.class, loc);
  155.         }
  156.        
  157.         //antelopes all over the environment (CHANGE THIS)
  158.         for(int j = 0; j < numInstancesAntelope; j++) {
  159.             loc = singleRandomLocation(state, sep, width_separation, height_separation);
  160.             addObject(Antelope.class, loc);
  161.         }
  162.        
  163.        
  164.         //zebras all over the environment (CHANGE THIS)
  165.         for(int j = 0; j < numInstancesZebra; j++) {
  166.             loc = singleRandomLocation(state, sep, width_separation, height_separation);
  167.             addObject(Zebra.class, loc);
  168.         }
  169.        
  170.     }
  171.      
  172.     private Int2D singleRandomLocation(SimState state, int sep, int width_separation, int height_separation) {
  173.         Int2D loc;
  174.        
  175.         //prevent from being in lakes location or a already occupied one
  176.         do {
  177.             loc = new Int2D(state.random.nextInt(world.getWidth()),state.random.nextInt(world.getHeight()));   
  178.         }while (((loc.x >= sep && loc.x <= width_separation + sep && loc.y >= 2*height_separation + sep && loc.y <= 3*height_separation + sep) ||
  179.                  (loc.x >= 2*width_separation && loc.x <= 3*width_separation && loc.y >= sep && loc.y <= height_separation + sep)) ||
  180.                 occupied[loc.x][loc.y] != null);
  181.        
  182.            
  183.         return loc;
  184.     }
  185.    
  186.     /*
  187.     private Int2D groupRandomLocation(SimState state, int sep, int width_separation, int height_separation) {
  188.         Int2D loc;
  189.        
  190.         //prevent from being in lakes location or a already occupied one
  191.         //and not too near to the limits of the environment
  192.         do {
  193.             loc = new Int2D(state.random.nextInt(world.getWidth()),state.random.nextInt(world.getHeight()));   
  194.         }while ((loc.x > sep && loc.x < width_separation + sep && loc.y > 2*height_separation + sep && loc.y < 3*height_separation + sep ||
  195.                 loc.x > 2*width_separation && loc.x < 3*width_separation && loc.y > sep && loc.y < height_separation + sep) &&
  196.                 occupied[loc.x][loc.y] != null &&
  197.                 loc.x > sep && loc.x < world.getWidth() - sep && loc.y > sep && loc.y < world.getHeight() - sep);
  198.         System.out.println("grouploc  x:"+loc.x+" y:"+loc.y);
  199.         return loc;
  200.     }*/
  201.    
  202.     private void buildRandomMap(SimState state) {
  203.         //Class classes[] = {Wall.class, Tree.class, Bush.class, Water.class, House.class};
  204.         //int numberOfInstances[] = {400, 200, 200, 100, 20};
  205.         Class classes[] = {Tree.class, Bush.class, Water.class, Antelope.class, Zebra.class};
  206.         //int numberOfInstances[] = {100, 100, 5, 100, 50, 50};
  207.         int numberOfInstances[] = {100, 100, 100, 100, 100, 100};
  208.         Int2D loc;
  209.        
  210.         for (int i = 0; i < classes.length; i++) {
  211.            
  212.             for(int j = 0; j < numberOfInstances[i]; j++) {
  213.                 do { loc = new Int2D(state.random.nextInt(world.getWidth()),state.random.nextInt(world.getHeight())); }
  214.                 while (occupied[loc.x][loc.y] != null);
  215.                
  216.                 addObject(classes[i], loc);
  217.             }
  218.            
  219.         }
  220.     }
  221.    
  222.     //APAGAR
  223.     /*
  224.     private void buildDonutMap(SimState state) {
  225.         Int2D loc;
  226.        
  227.         // Define the two classes
  228.         Class outer_class = Tree.class;
  229.         Class inner_class = Bush.class;
  230.        
  231.         // Number of instances
  232.         int num_outer = 500;
  233.         int num_inner = 500;
  234.        
  235.         // Define the size of the inner square
  236.         int inner_width = world.getWidth() / 2;
  237.         int inner_height = world.getHeight() / 2;
  238.        
  239.         int inner_x = (world.getWidth() / 2) - (inner_width / 2);
  240.         int inner_y = (world.getHeight() / 2) - (inner_height / 2);
  241.        
  242.         // Add the outer instances
  243.         for(int j = 0; j < num_outer; j++) {
  244.             do { loc = new Int2D(state.random.nextInt(world.getWidth()),state.random.nextInt(world.getHeight())); }
  245.             while ( occupied[loc.x][loc.y] != null ||
  246.                     ( (loc.x >= inner_x && loc.x <= inner_x + inner_width) &&
  247.                     (loc.y >= inner_y && loc.y <= inner_y + inner_height)));
  248.            
  249.             addObject(outer_class, loc);
  250.         }
  251.        
  252.         // Add the inner instances
  253.         for(int j = 0; j < num_inner; j++) {
  254.             do { loc = new Int2D(state.random.nextInt(inner_width) + inner_x, state.random.nextInt(inner_height) + inner_y); }
  255.             while ( occupied[loc.x][loc.y] != null);
  256.            
  257.             addObject(inner_class, loc);
  258.         }
  259.     }
  260.    
  261.     private void buildStructuredMap(SimState state) {
  262.         Int2D loc;
  263.        
  264.         // Number of instances per block
  265.         int num_instances = 500;
  266.        
  267.         int height_separation = world.getHeight()/3;
  268.         int width_separation = world.getWidth()/3;
  269.         int sep = 50;
  270.        
  271.         // First Block - Top Forest
  272.         for(int j = 0; j < num_instances; j++) {
  273.             do { loc = new Int2D(state.random.nextInt(world.getWidth()), state.random.nextInt(height_separation - sep/2)); }
  274.             while ( occupied[loc.x][loc.y] != null);
  275.            
  276.             addObject(Tree.class, loc);
  277.         }
  278.        
  279.         // Bush Block - Bushes below the Forest
  280.         for(int j = 0; j < num_instances; j++) {
  281.             do { loc = new Int2D(state.random.nextInt(world.getWidth()), state.random.nextInt(30) + (height_separation - 30/2)); }
  282.             while ( occupied[loc.x][loc.y] != null);
  283.            
  284.             addObject(Bush.class, loc);
  285.         }
  286.        
  287.         // Central Block - House neighborhood
  288.         /*for(int j = 0; j < num_instances; j++) {
  289.             do { loc = new Int2D(state.random.nextInt(world.getWidth()), state.random.nextInt(height_separation - sep) + (height_separation + sep/2)); }
  290.             while ( occupied[loc.x][loc.y] != null);
  291.            
  292.             addObject(House.class, loc);
  293.         }
  294.        
  295.         // Wall Block - Wall below the neighborhood
  296.         for(int j = 0; j < num_instances; j++) {
  297.             do { loc = new Int2D(state.random.nextInt(world.getWidth()), state.random.nextInt(30) + (2*height_separation - 30/2)); }
  298.             while ( occupied[loc.x][loc.y] != null);
  299.            
  300.             addObject(Wall.class, loc);
  301.         }
  302.        
  303.         // Down Left Block - Forest
  304.         for(int j = 0; j < num_instances; j++) {
  305.             do { loc = new Int2D(state.random.nextInt(width_separation - sep/2), state.random.nextInt(height_separation - sep/2) + (2*height_separation + sep/2)); }
  306.             while ( occupied[loc.x][loc.y] != null);
  307.            
  308.             addObject(Tree.class, loc);
  309.         }
  310.        
  311.         // Down Center Block - Water
  312.         for(int j = 0; j < num_instances; j++) {
  313.             do { loc = new Int2D(state.random.nextInt(width_separation) + (width_separation), state.random.nextInt(height_separation - sep/2) + (2*height_separation + sep/2)); }
  314.             while ( occupied[loc.x][loc.y] != null);
  315.            
  316.             addObject(Water.class, loc);
  317.         }
  318.        
  319.         // Down Right Block - Forest
  320.         for(int j = 0; j < num_instances; j++) {
  321.             do { loc = new Int2D(state.random.nextInt(width_separation - sep/2) + (2*width_separation + sep/2), state.random.nextInt(height_separation - sep/2) + (2*height_separation + sep/2)); }
  322.             while ( occupied[loc.x][loc.y] != null);
  323.            
  324.             addObject(Tree.class, loc);
  325.         }
  326.     }
  327.     */
  328.    
  329.     private void addObject(Class c, Int2D loc) {
  330.         Class[] params = {int.class,int.class};
  331.         Object[] args = {loc.x,loc.y};
  332.         SimObject obj;
  333.        
  334.         try {
  335.             Constructor cons = c.getConstructor(params);   
  336.             obj = (SimObject) cons.newInstance(args);
  337.         }
  338.        
  339.         catch (Exception e) { System.err.println("Oops. See addObject."); return; };
  340.        
  341.         world.setObjectLocation(obj,loc);
  342.         occupied[loc.x][loc.y] = c;
  343.     }
  344.  
  345.     /* End of Map Methods */
  346.    
  347.     @Override
  348.     public void step(SimState state) {
  349.         step = step + 1;
  350.        
  351.         if(step > maxSteps){
  352.             try {
  353.                 writer.close();
  354.             } catch (IOException e) {
  355.                 e.printStackTrace();
  356.             }
  357.             state.finish();
  358.         }
  359.        
  360.         int stepCheckpoint = maxSteps/100;
  361.         if( step%stepCheckpoint == 0 ){
  362.             printStats();
  363.         }
  364.        
  365.         /*
  366.          * Step over all the explorers in the environment, making them step
  367.          */
  368.         for(ExplorerAgent agent : explorers){
  369.             agent.step(state);
  370.         }
  371.        
  372.     }
  373.  
  374.     private void printStats() {
  375.         int objsSeen = 0;
  376.         int nObjs = 0;
  377.         int nErrors = 0;
  378.        
  379.         for(int i = 0; i<world.getWidth(); i++){
  380.             for (int j = 0; j < world.getHeight(); j++) {
  381.                 Class real = occupied[i][j];
  382.                 Class identified = mapper.identifiedObjects[i][j];
  383.                
  384.                 nObjs += real != null ? 1 : 0;
  385.                 objsSeen += identified != null ? 1 : 0;
  386.                 nErrors += ((real != null && identified != null) && (real != identified)) ? 1 : 0;
  387.             }
  388.         }
  389.        
  390.         System.err.println("SEEN: " + objsSeen);
  391.         System.err.println("EXIST: " + nObjs);
  392.        
  393.         System.err.println("-------------------------");
  394.         System.err.println("STATISTICS AT STEP: " + this.step);
  395.         System.err.println("-------------------------");
  396.         System.err.println("% OF OBJECTS SEEN: " + (int) Math.ceil(((double)objsSeen/(double)nObjs)*100) + "%");
  397.         System.err.println("% OF ERROR: " + ((double)nErrors/(double)objsSeen)*100.0 + "%");
  398.         System.err.println("-------------------------");
  399.        
  400.         try {
  401.             writer.append("" + step + " , " + ((double)objsSeen/(double)nObjs)*100.0 + " , " + ((double)nErrors/(double)objsSeen)*100.0 + "\n");
  402.         } catch (IOException e) {
  403.             e.printStackTrace();
  404.         }
  405.     }
  406.  
  407.     public MapperAgent getMapper() {
  408.         return mapper;
  409.     }
  410.  
  411.     public BrokerAgent getBroker() {
  412.         return broker;
  413.     }
  414.  
  415.     public Bag getVisibleObejcts(int x, int y, int viewRange) {
  416.        
  417.         Bag all = world.getNeighborsHamiltonianDistance(x, y, viewRange, false, null, null, null);
  418.         Bag visible = new Bag();
  419.        
  420.         for(Object b: all){
  421.             if(b instanceof ExplorerAgent) continue;
  422.            
  423.             SimObject o = (SimObject) b;
  424.             visible.add(new SimObject(o));
  425.         }
  426.        
  427.         return visible;
  428.     }
  429.  
  430.     public SimObject identifyObject(Int2D loc) {
  431.        
  432.         Bag here = world.getObjectsAtLocation(loc.x, loc.y);
  433.         int i = 0;
  434.        
  435.         if(here == null){
  436.             return null;
  437.         }
  438.        
  439.         while((here.get(i) instanceof ExplorerAgent) && i<here.numObjs) i++;
  440.        
  441.         SimObject real = (SimObject) here.get(i);
  442.        
  443.         return real;
  444.     }
  445.  
  446.     public void updateLocation(ExplorerAgent agent, Int2D loc) {
  447.        
  448.         world.setObjectLocation(agent, loc);   
  449.     }
  450.  
  451. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement