swarm-support
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Swarm-Support] QSort and its compare function


From: Steve Railsback
Subject: Re: [Swarm-Support] QSort and its compare function
Date: Fri, 11 Nov 2005 11:53:26 -0800
User-agent: Mozilla Thunderbird 1.0.2 (Windows/20050317)

Marcus G. Daniels wrote:
Steve Railsback wrote:

I'm not getting QSort to work in Java. The 'compare' method is never

Marcus- I am afraid that my test model still does not work, even after I tried to make it as close to yours as I could...

The model swarm creates a list of bugs, which are each given a random size. The schedule has only two actions: sort the bugs, then have them print their size.

The compare method still is never executed- I have a print statement in it that never prints.

Thanks very much if you can take a look sometime.

Steve

--
Lang Railsback & Assoc.
250 California Ave.
Arcata, California 95521
(707) 822-0453
package SortTest2;

import swarm.Globals;

public class StartStupidModel {

        /*
         By convention, Java Swarm models have a "StartModel" class
         that contains the "main" method. Its purpose is to build the 
         highest-level swarm and to start execution. 
         
         or: as the JavaHeatbugs code (Copyright © 1999-2000 Swarm Development 
Group)
         says: 
        
            The main() function is the top-level place where everything
        starts.  For a typical Swarm simulation, in main() you create
        a toplevel Swarm, let it build and activate, and set it to
        running. 

         */
        public static void main(String[] args) {
        // Swarm initialization: all Swarm apps must call this first.
        Globals.env.initSwarm ("StupidModel", "2.2", "address@hidden", args);

        StupidObserverSwarm topLevelSwarm = 
            new StupidObserverSwarm (Globals.env.globalZone);
        Globals.env.setWindowGeometryRecordName (topLevelSwarm, 
"topLevelSwarm");
        
        topLevelSwarm.buildObjects ();
        topLevelSwarm.buildActions ();
        topLevelSwarm.activateIn (null);
        topLevelSwarm.go ();
//        topLevelSwarm.drop ();  This statement will be needed when a stopping 
rule is added.


        }

}
package SortTest2;


public class StupidBug {

        // Declarations
        public double mySize = 1.0;
        
        public StupidBug (double aSize) {
                mySize = aSize;
                
        }
        

        // Starting in V. 10, this compare method is needed
        // by the model swarm's bugSorter to sort bugs by size
        // This non-standard compare method will sort them in 
        // descending instead of ascending order.
        public int compare(StupidBug otherBug) {
                double otherBugSize = otherBug.getMySize();
                System.out.println("Hello World from compare- this does not 
print");
                if (mySize > otherBugSize) return -1;
                else if (mySize < otherBugSize) return 1;
                else return 0;
        }


        public double getMySize() {
                return mySize;
        }

        public void printSize () {
                System.out.println(mySize);
                return;
        }

}
package SortTest2;


import swarm.Globals;
import swarm.NonUniqueMethodSignatureException;
import swarm.Selector;
import swarm.SignatureNotFoundException;
import swarm.activity.ActionGroupImpl;
import swarm.activity.Activity;
import swarm.activity.ScheduleImpl;
import swarm.collections.ListImpl;
import swarm.defobj.Zone;
import swarm.objectbase.Swarm;
import swarm.objectbase.SwarmImpl;
import swarm.simtools.QSortImpl;

public class StupidModelSwarm extends SwarmImpl {

        // Declarations
        // Note that starting in Version 4, there are two Grid2d space objects
        public ListImpl bugList;
        private ScheduleImpl modelSchedule; 
        private QSortImpl bugSorter; 
        

          public StupidModelSwarm (Zone aZone) {
                    super (aZone);
            
                    
          }    
          
          public Object buildObjects () {
                  // allow our parent class to build anything.
                  super.buildObjects();
                  
                  // Now set up the grid used for cell locations
                  
                  
                  // Create a list of the stupidBugs
                  
                  bugList = new ListImpl (Globals.env.globalZone);
                  
                // Make some agents
                // The bugs put themselves on the space.
                  for(int i=0; i<5; i++) bugList.addLast 
                        (new 
StupidBug(Globals.env.uniformDblRand.getDoubleWithMin$withMax(0.0, 100.0)));

                


        return this;
          }

          
          public Object buildActions () {
//                      System.out.println("StupidModelSwarm >>>> buildActions 
>>>> BEGIN");

                    super.buildActions();
                    
                    // In version 10 we must start each time step
                    // by sorting the bugs by size. This action calls the sort 
method.
                    ActionGroupImpl updateActions;
                    updateActions = new ActionGroupImpl (getZone ());
                        
                        Selector sortSel = null;
                        try {
                                sortSel = new Selector (getClass(), "sortBugs", 
false);
                        } catch (NonUniqueMethodSignatureException e) {
                                e.printStackTrace();
                        } catch (SignatureNotFoundException e) {
                                e.printStackTrace();
                        }

                        updateActions.createActionTo$message(this, sortSel);
                        
                    Selector printSel = null;
                        try {
                                printSel = new Selector 
(bugList.getFirst().getClass(), "printSize", false);
                        } catch (NonUniqueMethodSignatureException e) {
                                e.printStackTrace();
                        } catch (SignatureNotFoundException e) {
                                e.printStackTrace();
                        }
                        updateActions.createActionForEach$message (bugList, 
printSel);

                    
                        // Then we create a schedule and put the 
                // action group(s) on it.             
                modelSchedule = new ScheduleImpl (getZone (), 1);
                modelSchedule.at$createAction (0, updateActions);
                    
                    return this;                  
          }
          
          public Activity activateIn (Swarm swarmContext) {
                  // First, activate ourselves via the superclass
                  // activateIn: method.  Just pass along the context: the
                  // activity library does the right thing.
                  super.activateIn (swarmContext);
                  
                  // Now activate our own schedule.
                  modelSchedule.activateIn (this);
                  
                  // Finally, return our activity.
                  return getActivity ();
          }
          
        
        // For version 10, sortBugs uses Swarm's QSort to sort
        // the bug list by size. It operates using the "compare" 
        // method now added to the bugs.
        public void sortBugs() {
                bugSorter = new QSortImpl (Globals.env.globalZone);
                Selector compareSel = null;
                try {
                        compareSel = new Selector 
(bugList.getFirst().getClass(), "compare", false);
                } catch (NonUniqueMethodSignatureException e) {
                        e.printStackTrace();
                } catch (SignatureNotFoundException e) {
                        e.printStackTrace();
                }
                System.out.println("Hello World from sortBugs- this prints");
                bugSorter.sortObjectsIn$using(bugList, compareSel);
        }

        public ListImpl getBugList() {
                return bugList;
        }

                    
}
package SortTest2;

import swarm.Globals;
import swarm.NonUniqueMethodSignatureException;
import swarm.Selector;
import swarm.SignatureNotFoundException;
import swarm.activity.ActionGroupImpl;
import swarm.activity.Activity;
import swarm.activity.ScheduleImpl;
import swarm.analysis.EZBin;
import swarm.analysis.EZBinCImpl;
import swarm.analysis.EZBinImpl;
import swarm.defobj.Zone;
import swarm.gui.ColormapImpl;
import swarm.gui.ZoomRasterImpl;
import swarm.objectbase.Swarm;
import swarm.simtoolsgui.GUISwarmImpl;
import swarm.space.Object2dDisplayImpl;


public class StupidObserverSwarm extends GUISwarmImpl {
        
        // Declarations
        
        private StupidModelSwarm stupidModelSwarm;
        public ZoomRasterImpl worldRaster; 
        public Object2dDisplayImpl bugDisplay;
        public Object2dDisplayImpl cellDisplay;
        private ActionGroupImpl displayActions;
        private ActionGroupImpl checkToStopActions; 
        private ScheduleImpl displaySchedule; 
        private ColormapImpl colormap; 
        private EZBinCImpl bugSizeHistogramC;
        private EZBin bugSizeHistogram;


        public StupidObserverSwarm (Zone aZone) {
                    super(aZone);
        }
        
        public Object buildObjects () {
                      
            super.buildObjects ();
                        
            // First, we create the model that we're actually observing. The
            // model is a subswarm of the observer. 
                        
            stupidModelSwarm = new StupidModelSwarm (getZone ());
                        
            getControlPanel ().setStateStopped ();
                        
            // When the control panel is started, 
            // the model swarm builds its objects.
                stupidModelSwarm.buildObjects ();
                
                    
            return this;

        }
        
        public Object buildActions () {
//              System.out.println("StupidObserverSwarm >>>> buildActions >>>> 
BEGIN");
                super.buildActions();
                
                // First, let our model swarm build its own schedule.
                stupidModelSwarm.buildActions();
                
                
                return this;
        }  

  /*
    activateIn is called by Main; it puts the schedules of the
    Observer and Model swarms together in one overall schedule.
    A swarm's "activateIn(aSwarm)" method means "execute my schedule in
    the schedule of aSwarm". "aSwarm" is referred to as the "context". */
        public Activity activateIn (Swarm swarmContext) {
                // First, activate ourselves (just pass along the context).
                super.activateIn (swarmContext);
                
                /* Activate the model swarm in ourselves. This makes the model 
                swarm a subswarm of the observer swarm -- and executes its
                schedule before the observer's each time step. */
                stupidModelSwarm.activateIn (this);
                
                // Activate returns the swarm activity - the thing that's ready 
to run.
                return getActivity();
        }
                    
}

reply via email to

[Prev in Thread] Current Thread [Next in Thread]