Now I wonder if I could modify the agents "HabitatCell" to become normal agents and forget the idea of cell versus agents.
What I did:
I decided to consider each HabitatCell as an agent that shares the grid with the other agents.
1- I've created a new class which extends DefaultStyleOGL2D (I had to look for a way to exchange the shape of the agents (http://repast.10935.n7.nabble.com/Changing-agent-shape-on-the-fly-using-extending-DefaultStyleOGL2D-td9543.html) to the folder 'observer':
package stupidmodel.observer;
import java.awt.Color;
import repast.simphony.visualizationOGL2D.DefaultStyleOGL2D;
import saf.v3d.scene.VSpatial;
import stupidmodel.HabitatCell;
/**
* A simple custom color implementation for {@link HabitatStyle} agents.
*
* <p>
* The color of each agent depends on the food quantity.
* Dark green means more food
* </p>
*
* @authorJosé Cascalho (jose.cascalho)
* @since 2014
* @version $Id$
*/
public class HabitatStyleOGL2D extends DefaultStyleOGL2D {
/**
* @return a circle of radius 4.
*/
public VSpatial getVSpatial(Object agent, VSpatial spatial) {
//if (spatial == null) {
spatial = shapeFactory.createRectangle(10, 10);
//}
return spatial;
}
/**
* Returns a modified color value for an agent. Bit tricky, though.
*
* @see repast.simphony.visualizationOGL2D.DefaultStyleOGL2D#getColor(java.lang.Object)
*/
@Override
public Color getColor(final Object agent) {
if (agent instanceof HabitatCell) {
final HabitatCell cell = (HabitatCell) agent;
final double food = cell.getFoodAvailability();
//System.out.println("Food availability:"+ food);
final int strength = (int) Math.min(200 * food, 255);
return new Color(0, strength, 0); // 0x000000 - black,
// 0x00FF00 - green
}
return super.getColor(agent);
}
2- At the StupidModelContextBuilder class I've added the following lines (and removed all the lines related to 'ValueLayer':
/** In the same grid, HabitatCell agents are added: */
for (int i = 0; i < Constants.GRID_SIZE; ++i) {
for (int j = 0; j < Constants.GRID_SIZE; j++){
final HabitatCell cell = new HabitatCell(i,j);
context.add(cell);
grid.moveTo(cell, i, j);
}
}
3- At the HabitatCell class the following method is simplified:
@ScheduledMethod(start = 1, interval = 1, priority = 1)
public void growFood() {
foodAvailability += RandomHelper.nextDoubleFromTo(0.0,
maximumFoodProductionRate);
}
4- Finally at the Agent Selection of the Options Editor, I've added the agent HabitatCell and at the grid style, selected the 'stupidmodel.observer.HabitatStyleOGL2D'.
Sem comentários:
Enviar um comentário