This site requires JavaScript, please enable it in your browser!
Greenfoot back
joandvgv
joandvgv wrote ...

2015/2/18

Pausing movement when there's one equal actor.

1
2
3
4
joandvgv joandvgv

2015/2/18

#
Well, the problem is in order to finish my project about garbage collector trucks. The assignment is: If a route didn't finish and there are available trucks, there must go out another truck and start the picking where the other one finished. The user chooses how many trucks they are gonna be in the simulation and I assign them the route randomly What I want to do: If there are 10 trucks, and of those 10, four have the same route, just the first of those four starts moving. If this one does not finish the route, then the second one starts moving. How? In order to do so I've thought of getIntersectingObjetcs method, which I think returns a List of all the objects that are intersecting mine. Since at the beginning of the simulation they all intersect each other, it will return all the objects and then i will read the route assigned randomly and if it's the same one i'll pause its movement till the other goes away. The problem? I don't know how to implement that. I've tried creating a List in my Truck class:
 List=getIntersectingObjects(Truck.class);
But then How do I read from that list the route of the truck? Is this way of pausing movement when there's one equal actor (same route makes it an equal object) too hard to implement? Is there a better way to do so? How do I read the information from the list of actors? Thanks again! You're helping me a lot!
danpost danpost

2015/2/18

#
Curious. Why would you assign route numbers to the additional trucks before you know where they might be needed? What would happen if no additional trucks had that route number? Clean-up of the garbage would never be completed. I am not sure this is what you want -- and the assignment stated that additional trucks should be sent out to finish any unfinished routes. Also, is there a reason that the trucks are randomly assigned route numbers? Is it so that they have a different ordering when leaving the pool? I would like to see the code that randomly assigns the route numbers to the trucks.
joandvgv joandvgv

2015/2/18

#
Yes. It is a very interesting project. As you said, Clean-up of the garbage would never be completed if so. The porpouse of that is allowing the "analist" or the "user" to see that in real life they need more trucks to cover that route and that's what the simulation is made for. There is no reason for the random assignation but as you said, they leave the pool at different times depending on it. Here's the code:
protected int routeNum = (1 + Greenfoot.getRandomNumber(5));
It is inside the truck constructor. In the act method we could see this:
if (timer<=0){
            if (!start){ 
               switch (routeNum){
                  case 1: {
                      timer=361;
                   break;
                   }
                   case 2: {
                       timer=1444;
                   break;
                   }
                   case 3: {
                      timer=2888;
                    break;
                   }
                    case 4: {
                      timer=4332;
                    break;
                   }
                    case 5: {
                      timer=5500;
                    break;
                   }
                   default: {
                   break;
                   }
             }
            start=true;
            }
        }
if (timer > 0) timer -= 2;
if (timer < 0)  timer=0;

So, this is for leaving when the timer is over. Now I need them to not leave if there's a truck with the same route even if it's time to leave.
danpost danpost

2015/2/18

#
What I am saying is that the additional trucks should not have a pre-assigned route number -- that each one would be assigned a route number at the time one was found not completed by a previous truck. By not pre-assigning a route number, you can use its value as the condition for 'rolling' (only roll if the route number is not zero -- if a route number is assigned). Even for the initial five trucks, one for each of the routes, a timer in the world can be used to determine when a route should be assigned to a truck. So, you would have one int timer field in the world class and one int route number field in the truck class -- that is it as far as fields. You would add a method to the truck class to set the route number and a non-zero value for that field will indicate that the truck can move. You would add a method to the world class to allow the truck to inform that its route was not completed. This method would then assign the route number of that truck to one of the additional trucks, if any are available to dispatch.
joandvgv joandvgv

2015/2/18

#
Ok, well. Your idea sounds just fine. I'll try to implement it and let you know if I have some questions. Thanks!
joandvgv joandvgv

2015/2/18

#
Ok, now i got a question: where exactly should I set the route number? I've already created the method but don't know where to call it. I suppose this is in the world class but then I don't know how to do it. I've tried to create this method in the world class and see what happens:
private void setRoute(){
       for (int a=1; a<=5; a++){
           recolector.setRoute(a);
        }
    }
      
and i got this exception: Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space
danpost danpost

2015/2/18

#
The act method in your subclass of World should run the timer. When the specified time has passed you would call the method that sets the route number on one of the trucks. It would help if you posted the code in your World subclass. EDIT: the random picking of routes to trucks can be dealt with later; as well as making the dispatch times vary (so the interval between each truck moving out varies)..
danpost danpost

2015/2/18

#
joandvgv wrote...
Ok, now i got a question: where exactly should I set the route number? I've already created the method but don't know where to call it. I suppose this is in the world class but then I don't know how to do it. I've tried to create this method in the world class and see what happens:
private void setRoute(){
       for (int a=1; a<=5; a++){
           recolector.setRoute(a);
        }
    }
      
and i got this exception: Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space
Remove that method from the world class. You need something like the following in your truck class:
// the field
private int routeNumber;
// the setter method
public void setRouteNumber(int routeNum)
{
    routeNumber = routeNum;
}
joandvgv joandvgv

2015/2/18

#
Ok then:
 public ArrayList path = new ArrayList();
   public ArrayList<ArrayList> path2 = new ArrayList<ArrayList>();
   public ArrayList ruta1 = new ArrayList();
   public ArrayList ruta2 = new ArrayList();
   public ArrayList ruta3 = new ArrayList();
   public ArrayList ruta4 = new ArrayList();
   public ArrayList ruta5 = new ArrayList();
   public int numero;
   private Ruta laRuta;
   private Punto elPunto;
   private Informacion lainformacion;
   private Infoparada infoparada;
   private Recolector recolector;
   private Relleno relleno;
   private Auto auto;
   private Nivel1 nivel;
   private Reloj reloj;
   private int cantidad;
   private int dias;
   private String cantidadCarros;
   private int buttonNbr;
   private int n;
   private boolean parada;
    public Ambiente()
    {    
        super(1024, 768, 1,false); 
        n=crearDialogoCarros();
        laRuta = new Ruta();
        elPunto = new Punto();
        lainformacion = new Informacion();
        infoparada = new Infoparada();
        recolector = new Recolector();
        relleno = new Relleno();
        auto = new Auto();
        addObject(relleno, 931, 141);
        addObject(lainformacion, 960, 658);
        addObject(infoparada, 960, 676);
        añadirpuntos();
        do añadircarros();
        while (!parada);
        addObject(new Reloj(true, true, 48, "Alert world after time is up", this), 645, 455);
        setRuta();
    }
      public void alert(){
       numero=100;   
       Greenfoot.stop();
    } 
    public Ruta getRuta()
   {
      return laRuta;
   }
    public Punto getPunto()
   {
      return elPunto;
   }
    public Informacion getInformacion(){
      return lainformacion;
   }
   public Recolector getRecolector(){
      return recolector;
    }
   public Auto getAuto(){
    return auto;
    }

    public Infoparada getInfoparada(){
       return infoparada;
    }
    public Relleno getRelleno(){
       return relleno;
    }
    
    public int getCantidad(){
        return cantidad;
    }
    public Reloj getReloj(){
        return reloj;
    }
    public void act() 
    {
      generatePath();
    } 
    public ArrayList getPath()
    {
     return ruta2;
    }
   public ArrayList<ArrayList> getPath2(){
       return path2;
    }
    public void generatePath(){
        ruta1.add("Left");
        ruta1.add("Left"); //there is so many of this

   }
       
    private int crearDialogoCarros(){
        cantidadCarros = JOptionPane.showInputDialog(
            "Introduzca el numero de carros");
        buttonNbr = JOptionPane.showConfirmDialog(
            null, "Estas seguro?");
        String msg = cantidadCarros
             + "\nPresiona Ok para continuar.";
        cantidad=Integer.parseInt(cantidadCarros);
        return cantidad;
    }

    private void añadirpuntos(){
          addObject(new Punto(), 255, 703);
          addObject(new Punto(), 549, 703);
     // i add several objects here
        }
        
   private void añadircarros(){
             if (getObjects(Recolector.class).size() < n){
             addObject(new Recolector(), 38, 702);
             parada=false;
            }
            else parada=true;
        }
    }

Just so you know Recolector is a subclass of Auto. Recolector is the class i call truck here.
danpost danpost

2015/2/18

#
Ok, this is going to take a while; so, I will go a little at a time. First, I do not see where anything is being added to the 'path' or 'path2' List objects declared on lines 1 and 2. The only reference to either list that I see is in the 'getPath2' method which just returns the presumably empty list. If I am correct, the field declaration and the method should be removed. There is a 'getPath' method that returns 'ruta2' which seems strange. Next, lines 3 through 7 declare your 'ruta1' through 'ruta5' List objects. This may be better as an array of ArrayLists; but, before doing any changes with regard to this, some information needs to be shared. * Are the routes pre-determined? I noticed that your act method seems to generate a route during run-time, adding more than one directional command per act. This certainly cannot be good as only one command can be possibly executed per act. The list will grow and grow and eventually you will either run out of memory or some other error event might occur. So, the question remains (and the 'act' method and the 'generatePath' methods will probably end up being remove and some other way of building the paths will be implemented.
joandvgv joandvgv

2015/2/18

#
yes, you don't see it because the fact is that i deleted some code in order to make it shorter, but it is in my code. The return ruta2 and path don't matter at all. I don't use them. Just was trying at that moment. The routes are pre determined. There's a lot in the code that I just don't use because I tried a lot.of things in order to make the movement work as I wanted. So don't mind about it. My problem is what I mentioned. Trying to implement what you said I noticed that the when I call the set route method it does not work
joandvgv joandvgv

2015/2/18

#
Here's the entire generatePath code.
    public void generatePath(){
        ruta1.add("Left");
        ruta1.add("Left");
        ruta1.add("Right");
        ruta1.add("Right");
        path2.add(ruta1);
        ruta2.add("Left");
        ruta2.add("Right");
        ruta2.add("Left");
        ruta2.add("Left");
        ruta2.add("Right");
        ruta2.add("Right");
        ruta2.add("Left");
        ruta2.add("Left");
        ruta2.add("Right");
        ruta2.add("Right");
        path2.add(ruta2);
        ruta3.add("Left");
        ruta3.add("Right");
        ruta3.add("Left");
        ruta3.add("Left");
        ruta3.add("Right");
        ruta3.add("Right");
        path2.add(ruta3);
        ruta4.add("Left");
        ruta4.add("Right");
        ruta4.add("Left");
        ruta4.add("Left");
        ruta4.add("Right");
        ruta4.add("Left");
        ruta4.add("Right");
        ruta4.add("Right");
        path2.add(ruta4);
        ruta5.add("Left");
        ruta5.add("Right");
        ruta5.add("Left");
        path2.add(ruta5);
    }
But as i said, it worked just fine. In the world subclass I'm trying to do this:
 public void act() 
    {
      generatePath();
      setRuta();
    } 
The setRuta() method is here:
private void setRuta(){
       for (int a=1; a<=5; a++){
       auto.setRuta(a);
    }
   }
It just does not work. When i create the trucks they all start in 0 so no one moves, If i try to set manually a value to the route, it works perfectly, so the truck starts moving right away. But when i try to initialize in the world subclass the first 5, nothing happens.
danpost danpost

2015/2/18

#
You should remove any code that is not being used. It only serves to clutter up your code and make things confusing. If your routes are pre-determined, then you have no need to use List objects for them. You can create arrays for them. For example (data needs to be filled in):
// declare and assign values to array in world class
public String[][] rutas = { { "Left", "Left", /** etc */ }, // route 1 directions
                            { /** ... */}, // route 2 directions
                            // ...
                            { /** ... */ } // route 5 directions
};
After removing any and all un-used code (and possibly adjusting your fields), post the entire class code, so I can tell heads from tails.
joandvgv joandvgv

2015/2/18

#
Ok thanks for that suggestion. I'll change that after I solve this. And sorry for not removing the useless code before posting it. Once again:
import greenfoot.*;  
import java.util.ArrayList;
import java.util.List;
import javax.swing.JOptionPane;

public class Ambiente extends World
{
    /**
     * Constructor para objetos de clase ambiente.
     * 
     */
   public ArrayList path = new ArrayList();
   public ArrayList<ArrayList> path2 = new ArrayList<ArrayList>();
   public ArrayList ruta1 = new ArrayList();
   public ArrayList ruta2 = new ArrayList();
   public ArrayList ruta3 = new ArrayList();
   public ArrayList ruta4 = new ArrayList();
   public ArrayList ruta5 = new ArrayList();
   private Ruta laRuta;
   private Punto elPunto;
   private Informacion lainformacion;
   private Infoparada infoparada;
   private Recolector recolector;
   private Relleno relleno;
   private Auto auto;
   private Nivel1 nivel;
   private Reloj reloj;
   private int cantidad;
   private int dias;
   private int numero;
   private String cantidadCarros;
   private int buttonNbr;
   private int n;
   private boolean parada;
    public Ambiente()
     {    
        super(1024, 768, 1,false); 
        n=crearDialogoCarros();
        laRuta = new Ruta();
        elPunto = new Punto();
        lainformacion = new Informacion();
        infoparada = new Infoparada();
        recolector = new Recolector();
        relleno = new Relleno();
        auto = new Auto();
        addObject(relleno, 931, 141);
        addObject(lainformacion, 960, 658);
        addObject(infoparada, 960, 676);
        añadirpuntos();
        do añadircarros();
        while (!parada);
        addObject(new Reloj(true, true, 48, "Alert world after time is up", this), 645, 455);
     }
    public void alert(){
       numero=100;   
       Greenfoot.stop();
      } 
    public Ruta getRuta()
    {
      return laRuta;
    }
    public Punto getPunto()
    {
      return elPunto;
    }
    public Informacion getInformacion(){
      return lainformacion;
    }
    public Recolector getRecolector(){
      return recolector;
    }
    public Auto getAuto(){
    return auto;
    }
    public Infoparada getInfoparada(){
       return infoparada;
    }
    public Relleno getRelleno(){
       return relleno;
    }
  
    public int getCantidad(){
        return cantidad;
    }
    public Reloj getReloj(){
        return reloj;
    }
    public void act() 
    {
      generatePath();
      setRuta();
    } 
    public ArrayList<ArrayList> getPath2(){
       return path2;
    }
 
    private void generatePath(){
        ruta1.add("Left");
        ruta1.add("Left");
        ruta1.add("Right");
        ruta1.add("Right");
        path2.add(ruta1);
        ruta2.add("Left");
        ruta2.add("Right");
        ruta2.add("Left");
        ruta2.add("Left");
        ruta2.add("Right");
        ruta2.add("Right");
        ruta2.add("Left");
        ruta2.add("Left");
        ruta2.add("Right");
        ruta2.add("Right");
        path2.add(ruta2);
        ruta3.add("Left");
        ruta3.add("Right");
        ruta3.add("Left");
        ruta3.add("Left");
        ruta3.add("Right");
        ruta3.add("Right");
        path2.add(ruta3);
        ruta4.add("Left");
        ruta4.add("Right");
        ruta4.add("Left");
        ruta4.add("Left");
        ruta4.add("Right");
        ruta4.add("Left");
        ruta4.add("Right");
        ruta4.add("Right");
        path2.add(ruta4);
        ruta5.add("Left");
        ruta5.add("Right");
        ruta5.add("Left");
        path2.add(ruta5);
    }
       
    private int crearDialogoCarros(){ //ask for number of trucks and time of simulation
        cantidadCarros = JOptionPane.showInputDialog(
            "Introduzca el numero de carros");
        buttonNbr = JOptionPane.showConfirmDialog(
            null, "Estas seguro?");
        String msg = cantidadCarros
             + "\nPresiona Ok para continuar.";
        cantidad=Integer.parseInt(cantidadCarros);
        return cantidad;
    }

    private void añadirpuntos(){ //add garbage at different points of the scenario. 
          addObject(new Punto(), 255, 703);
          addObject(new Punto(), 549, 703);
          addObject(new Punto(),766, 703);
          addObject(new Punto(),858, 451);
          addObject(new Punto(),770, 345);
          addObject(new Punto(),335, 345);
          addObject(new Punto(),203, 137);
          addObject(new Punto(),300, 64);
          addObject(new Punto(),473, 64);
          addObject(new Punto(),727, 64);
          addObject(new Punto(),108, 490);
          addObject(new Punto(),254, 490);
          addObject(new Punto(),134, 418);
          addObject(new Punto(),49, 250);
          addObject(new Punto(),250, 204);
          addObject(new Punto(),524, 204);
          addObject(new Punto(),721, 204);
          addObject(new Punto(),618, 563);
          addObject(new Punto(),763, 563);
          addObject(new Punto(),964, 458);
          addObject(new Punto(),964, 365);
          addObject(new Punto(),732, 631);
          addObject(new Punto(),776, 275);
          addObject(new Punto(),535, 275);
          addObject(new Punto(),279, 275);
          addObject(new Punto(),86, 65);
          addObject(new Punto(),375, 603);
          addObject(new Punto(),497, 490);
          addObject(new Punto(),704, 490);
          addObject(new Punto(),611, 415);
          addObject(new Punto(),619, 137);
    }
        
   private void añadircarros(){ //add the number of trucks read from keyboard
             if (getObjects(Recolector.class).size() < n){
             addObject(new Recolector(), 38, 702);
             parada=false;
            }
            else parada=true;
   }
        
   private void setRuta(){ //this what i'm trying. It is supossed to intialize the first 5 trucks with its route number.
       for (int a=1; a<=5; a++){
       auto.setRuta(a);
      }
   }
    }


Note that I haven't changed the Lists for the arrays because I'm still trying to implement what you suggested about route numbers. But still can't make it work.
danpost danpost

2015/2/18

#
Having fields for individual Recolector and Auto objects in the world class does not make any sense. Using 'auto.setRuta(a)' in a loop like that will end up with the Auto object created on line 34, which I do not believe is ever added into the world, with a route number of 5 (net result of changing the value from 0 to 1 to 2 to 3 to 4 to 5 via the loop). To assign a route number to a truck, use something like this:
// call a method to do this action
dispatchTruck(/** route number */); // number needs to be supplied
// and add the method
private void dispatchTruck(int rteNum)
{
    if ( ! getObjectsAt(38, 702, Recolector.class).isEmpty())
    {
        ((Recolector)getObjectsAt(38, 702, Recolector.class).get(0)).setRouteNumber(rteNum);
    }
}
By the way, all Recolector objects ARE also Auto type objects, because Recolector is a subclass of Auto.
There are more replies on the next page.
1
2
3
4