Ants Simulation - Class list
The classes in our simulation program can be divided to two groups. On the first is the algorithmic part of the program. The classes Point, Ant, Nest, Straight_line and Cyclic_pursuit supply an interface to the location and of every ant in the nest, and a method to move them, each in it direction. Every class in this group has a toString method (default output function), so one can easily construct some text driven simulation with it.
The second group of classes, including DrawControls, DrawPanel and MoveAntInt is use for the graphical interface. Every class in this group inherits from some graphical Java component (the first two from Panel, the third from Applet).
The main class of the program is MoveAntInt. This class holds as data members' instance of classes from the two groups. It use the GUI group to get input from the user, drive the data to the algorithmic group, where the calculation take plase, and back to the GUI group for display.
Class list:
- Point. Represent a point in a 2d space. The class includes a set off function for relation between points in the space.
- Public data members: double x,y
Revelator functions:
distTo(Point p) return the Euclidean distance between the this point to Point p.
angle(Point p) return the angle (in degrees) to point p.
- Mutetor functions:
delteStepTo(Point p, double delta) move this point delta distance to Point p.
If the distance to p is then delta, move this point to p.
- Ant. Represent one ant. Hold the position of the ant and pointers to others ants in the nest. When method move() invoke the ant will perform one step toward the next ant in the nest.
- Public data members: Point position: the position of the ant in the 2D space.
Ant next, prev: pointers to the next and previous ant in the nest (if exist) in a double linked list.
Static delta: the Euclidean distance an ant will do in one step.
Revelator functions:
GetAngle() return the angle of from the next ant.
- Mutetor functions:
Move(): move the ant delta step toward the next ant.
- Nest. An abstract class represents a set of ants, initial positions for the ants and function for move all the ants in the nest.
- Public data members: ants_list, set of Ants.
inital_points: array of initial points for the nest. This member has different use in the two subclasses.
time_stemp: increase by one every time the method move invoke. Represent the time pass from the start of the simulation (the first move).
- Mutetor functions:
Move(): invoke the move method of every ant in the nest.
NormelizePath: remove and add points to the inital_points array, so the distance between every to points is equal to Ant.delta. This function is now in use only by the Straight_line subclass.
- Straight_line. Subclass of Nest. Represent a nest of ants, in the simulation case of convergence into a straight line. In this subclass, the Nest inital_points represent the path of the first ant from the nest to the food.
- Public data members: gap_between_ants, the number of time the method move is invokes before the next ants is created and sent from the nest after the previous ant.
- Mutetor functions:
Move() move the first ant along the initial points path, and all the other ants one after the other (call Nest.move). Every gap_between_ants create a new ant.
- Cyclic_pursuit. Subclass of Nest. Represent a nest of ants, in the simulation case of cyclic pursuit. In this subclass, the Nest inital_points represent the initial positions of the ants. Here no ants are created during the simulation, the ant will persuit each other till they convergence to a point.
- Mutetor functions:
Move() move every ant after the others (call Nest.move).
- DrawControls. Interface classes inherit from Panel. This class presents to the user a set of button and option button to control the simulations. The purpose of every button will be described in details in the user guide sections.
- DrawPanel. Interface classes inherit from Panel. This class presents to the user a white panel that use for two purposes: let the user input the initial state of the simulation, display the ants and the paths during the simulation.
In the running phase of the simulation an image of an ant is present on the posions of every ant in the nest. If the draw_path flag is on, a line represent every ant path so far is display too.
In the input phase, the display is divided to two:
Cyclic pursuit, an image of an ant is display for every click of the user on the panel, this will be initial poison for one ant in the simulation running phase.
Straight-line pursuit, a line is drawn on the panel from each mouse click to the other. This line will be the first ant path in the simulation running phase.
- RepaintThread. Inherit from Thread. The class instance run in a unique thread and performs an endless loop. At every phase of the loop three actions are invoke:
The ants nest move(), moves every ant in the nest one step forward.
Repaint the simulation panel, display the ants new positions.
Sleep for a few milliseconds (determine by the delay member of the MoveAntInt.delay).
- MoveAntInt. The program main class. Create and initial:
User interface classes (DrawPanel, DrawControls)
Ants nest member (Nest)
Start the simulation loop (RepaintThread).
- Data members:
The class holds a set of Boolean flag coding the Simulation State. The user with the DrawControls interface can determine the values of each of this flag:
- sim_status: False before the simulation begin, at the user input phase,
True after the simulation begins.
- step_status: True only when the simulation is in step by step mode. In this mode, the RepaintThread perform one simulation and stop. The next step invokes only after the user press the step button again. This flag is relevant only when the sim_status flag is true.
- draw_path: When true, every ant path is drawn on the simulation panel.
- The mode flag has two states: POSITIONS when the simulation is in straight-line mode and TRAIL when the simulation is in cyclic pursuit mode.
Delay: the time in millisecond the simulation thread sleep after each step.
Lines: an array of the point the user input as initial data for the simulation. When in straight-line mode, every new point is present continuation in the first ant path, in the cyclic pursuit mode every new point present the initial position of a new ant.
- Main methods:
Init(): initilay all the data member, load the images needed for the simulations,
Create the two interface panels and the simulation thread.
ClearLines(): clear all the simulation data (inputs from the user, ants paths and so on).
InitalSim(): Create a nest of ants using the Straight_line or the Cyclic_pursuit class according to the simulation mode.