The Ascape Model Developer’s Manual
Appendix. Code Samples
In order for these code samples to compile, the developer must have ascape and related libraries available on the classpath. Additionally, the files ‘CoordinationGame.java’ and ‘CoordinationGamePlayer.java’ should be in a src code directory that matches the package structure, i.e. [src]/edu.brook.manual.model[n]/.
package edu.brook.manual.model1;
import java.awt.Color;
import org.ascape.model.HostCell;
import org.ascape.model.Scape;
import org.ascape.model.event.ScapeEvent;
import org.ascape.model.space.Array2DVonNeumann;
import org.ascape.util.data.StatCollector;
import org.ascape.util.data.StatCollectorCond;
import org.ascape.view.vis.ChartView;
import org.ascape.view.vis.Overhead2DView;
public class CoordinationGame extends Scape {
// model-scope variables
protected int nPlayers = 100;
protected int latticeWidth = 30;
protected int latticeHeight = 30;
public int coordinateOnBlue = 1;
public int coordinateOnRed = 1;
public int error = 0;
Scape lattice;
Scape players;
Overhead2DView overheadView;
// creates scapes and agents
public void createScape() {
super.createScape();
lattice = new Scape(new Array2DVonNeumann());
lattice.setPrototypeAgent(new HostCell());
lattice.setExtent(latticeWidth, latticeHeight);
CoordinationGamePlayer cgplayer = new CoordinationGamePlayer();
cgplayer.setHostScape(lattice);
players = new Scape();
players.setName("Players");
players.setPrototypeAgent(cgplayer);
players.setExecutionOrder(Scape.RULE_ORDER);
add(lattice);
add(players);
StatCollector CountReds = new StatCollectorCond("Reds") {
public boolean meetsCondition(Object object) {
return (((CoordinationGamePlayer) object).myColor == Color.red);
}
};
StatCollector CountBlues = new StatCollectorCond("Blues") {
public boolean meetsCondition(Object object) {
return (((CoordinationGamePlayer) object).myColor == Color.blue);
}
};
players.addStatCollector(CountReds);
players.addStatCollector(CountBlues);
}
public void scapeSetup(ScapeEvent scapeEvent) {
((Scape) players).setExtent(nPlayers);
}
// create views and charts
public void createGraphicViews() {
super.createGraphicViews();
ChartView chart = new ChartView();
players.addView(chart);
chart.addSeries("Count Reds", Color.red);
chart.addSeries("Count Blues", Color.blue);
chart.setDisplayPoints(100);
overheadView = new Overhead2DView();
overheadView.setCellSize(15);
lattice.addView(overheadView);
}
// get() and set() methods for the model variables
public int getRedScore() {
return coordinateOnRed;
}
public void setRedScore(int NewcoordinateOnRed) {
coordinateOnRed = NewcoordinateOnRed;
}
public int getBlueScore() {
return coordinateOnBlue;
}
public void setBlueScore(int NewcoordinateOnBlue) {
coordinateOnBlue = NewcoordinateOnBlue;
}
public int getnPlayers() {
return nPlayers;
}
public void setnPlayers(int NewnPlayers) {
nPlayers = NewnPlayers;
}
}
}
package edu.brook.manual.model1;
import java.awt.Color;
import java.util.List;
import org.ascape.model.Agent;
import org.ascape.model.CellOccupant;
public class CoordinationGamePlayer extends CellOccupant {
// cgplayer variables
protected Color myColor;
protected int totalScore;
protected int[] recentPlays;
protected int count = 0;
protected int totalreds;
protected int totalblues;
// agent initialization method
public void initialize() {
if (randomInRange(0, 1) > 0) {
myColor = Color.red;
} else {
myColor = Color.blue;
}
recentPlays = new int[5];
}
// add rules to the scape
public void scapeCreated() {
getScape().addInitialRule(MOVE_RANDOM_LOCATION_RULE);
getScape().addRule(RANDOM_WALK_RULE);
getScape().addRule(UPDATE_RULE);
getScape().addRule(PLAY_RANDOM_NEIGHBOR_RULE);
}
// imitation rule
public void update() {
count++;
if (count > recentPlays.length) {
int currScore = 0;
int bestScore = this.totalScore;
List neighbors = findNeighborsOnHost();
for (Object neighbor : neighbors) {
currScore = ((CoordinationGamePlayer) neighbor).totalScore;
if (currScore > bestScore) {
bestScore = currScore;
myColor = ((CoordinationGamePlayer) neighbor).getColor();
}
}
}
}
// play rule
public void play(Agent partner) {
int score;
if ((((CoordinationGamePlayer) partner).getColor() == myColor) && (myColor == Color.blue)) {
score = ((CoordinationGame) getRoot()).getBlueScore();
} else if ((((CoordinationGamePlayer) partner).getColor() == myColor) && (myColor == Color.red)) {
score = ((CoordinationGame) getRoot()).getRedScore();
} else {
score = 0;
}
count = count + 1;
updateRecentPlays(score);
}
// keep track of the recent scores
public void updateRecentPlays(int score) {
totalScore = 0;
for (int i = 0; i < (recentPlays.length - 1); i++) {
recentPlays[i] = recentPlays[i + 1];
totalScore = totalScore + recentPlays[i];
}
recentPlays[recentPlays.length - 1] = score;
totalScore = totalScore + recentPlays[recentPlays.length - 1];
}
// cgplayer get() and set() methods
public Color getColor() {
return myColor;
}
public void setColor(Color ncolor) {
myColor = ncolor;
}
}
Model 2: Stochasticity and Averaging Statistics
package edu.brook.manual.model2;
import java.awt.Color;
import org.ascape.model.HostCell;
import org.ascape.model.Scape;
import org.ascape.model.event.ScapeEvent;
import org.ascape.model.space.Array2DVonNeumann;
import org.ascape.util.data.StatCollector;
import org.ascape.util.data.StatCollectorCSAMM;
import org.ascape.util.data.StatCollectorCond;
import org.ascape.view.vis.ChartView;
import org.ascape.view.vis.Overhead2DView;
public class CoordinationGame extends Scape {
// model-scope variables
protected int nPlayers = 100;
protected int latticeWidth = 30;
protected int latticeHeight = 30;
public int coordinateOnBlue = 1;
public int coordinateOnRed = 1;
public int error = 0;
Scape lattice;
Scape players;
Overhead2DView overheadView;
// creates scapes and agents
public void createScape() {
super.createScape();
lattice = new Scape(new Array2DVonNeumann());
lattice.setPrototypeAgent(new HostCell());
lattice.setExtent(latticeWidth, latticeHeight);
CoordinationGamePlayer cgplayer = new CoordinationGamePlayer();
cgplayer.setHostScape(lattice);
players = new Scape();
players.setName("Players");
players.setPrototypeAgent(cgplayer);
players.setExecutionOrder(Scape.RULE_ORDER);
add(lattice);
add(players);
StatCollector CountReds = new StatCollectorCond("Reds") {
public boolean meetsCondition(Object object) {
return (((CoordinationGamePlayer) object).getColor() == Color.red);
}
};
StatCollector CountBlues = new StatCollectorCond("Blues") {
public boolean meetsCondition(Object object) {
return (((CoordinationGamePlayer) object).getColor()== Color.blue);
}
};
StatCollector AvgPayoff = new StatCollectorCSAMM("Payoff") {
public double getValue(Object object) {
return ((CoordinationGamePlayer) object).getRunningTotal();
}
};
players.addStatCollector(CountReds);
players.addStatCollector(CountBlues);
players.addStatCollector(AvgPayoff);
}
public void scapeSetup(ScapeEvent scapeEvent) {
((Scape) players).setExtent(nPlayers);
}
// create views and charts
public void createGraphicViews() {
super.createGraphicViews();
ChartView chart = new ChartView();
players.addView(chart);
chart.addSeries("Count Reds", Color.red);
chart.addSeries("Count Blues", Color.blue);
chart.addSeries("Average Payoff", Color.black);
chart.setDisplayPoints(100);
overheadView = new Overhead2DView();
overheadView.setCellSize(15);
lattice.addView(overheadView);
}
// get() and set() methods for the model variables
public int getRedScore() {
return coordinateOnRed;
}
public void setRedScore(int NewcoordinateOnRed) {
coordinateOnRed = NewcoordinateOnRed;
}
public int getBlueScore() {
return coordinateOnBlue;
}
public void setBlueScore(int NewcoordinateOnBlue) {
coordinateOnBlue = NewcoordinateOnBlue;
}
public int getError() {
return error;
}
public void setError(int NewError) {
error = NewError;
}
public int getnPlayers() {
return nPlayers;
}
public void setnPlayers(int NewnPlayers) {
nPlayers = NewnPlayers;
}
}
package edu.brook.manual.model2;
import java.awt.Color;
import java.util.List;
import org.ascape.model.Agent;
import org.ascape.model.CellOccupant;
public class CoordinationGamePlayer extends CellOccupant {
// cgplayer variables
protected Color myColor;
protected int totalScore;
protected int[] recentPlays;
protected int count = 0;
protected int totalreds;
protected int totalblues;
// agent initialization method
public void initialize() {
if (randomInRange(0, 1) > 0) {
myColor = Color.red;
} else {
myColor = Color.blue;
}
recentPlays = new int[5];
}
// add rules to the scape
public void scapeCreated() {
getScape().addInitialRule(MOVE_RANDOM_LOCATION_RULE);
getScape().addRule(RANDOM_WALK_RULE);
getScape().addRule(UPDATE_RULE);
getScape().addRule(PLAY_RANDOM_NEIGHBOR_RULE);
}
// imitation rule
public void update() {
count++;
if (count > recentPlays.length) {
if (randomInRange(0, 100) >= ((CoordinationGame) getRoot()).error) {
int currScore = 0;
int bestScore = this.totalScore;
List neighbors = findNeighborsOnHost();
for (Object neighbor : neighbors) {
currScore = ((CoordinationGamePlayer) neighbor).totalScore;
if (currScore > bestScore) {
bestScore = currScore;
myColor = ((CoordinationGamePlayer) neighbor).getColor();
}
}
} else {
if (randomInRange(0, 1) > 0) {
myColor = Color.red;
} else {
myColor = Color.blue;
}
}
}
}
// play rule
public void play(Agent partner) {
int score;
if ((((CoordinationGamePlayer) partner).getColor() == myColor) && (myColor == Color.blue)) {
score = ((CoordinationGame) getRoot()).getBlueScore();
} else if ((((CoordinationGamePlayer) partner).getColor() == myColor) && (myColor == Color.red)) {
score = ((CoordinationGame) getRoot()).getRedScore();
} else {
score = 0;
}
count = count + 1;
updateRecentPlays(score);
}
// keep track of the recent scores
public void updateRecentPlays(int score) {
totalScore = 0;
for (int i = 0; i < (recentPlays.length - 1); i++) {
recentPlays[i] = recentPlays[i + 1];
totalScore = totalScore + recentPlays[i];
}
recentPlays[recentPlays.length - 1] = score;
totalScore = totalScore + recentPlays[recentPlays.length - 1];
}
// cgplayer get() and set() methods
public Color getColor() {
return myColor;
}
public void setColor(Color ncolor) {
myColor = ncolor;
}
public int getRunningTotal() {
return (totalScore * 10);
}
}
package edu.brook.manual.model3;
import java.awt.Color;
import org.ascape.model.HostCell;
import org.ascape.model.Scape;
import org.ascape.model.event.ScapeEvent;
import org.ascape.model.space.Array2DVonNeumann;
import org.ascape.util.data.StatCollector;
import org.ascape.util.data.StatCollectorCSAMM;
import org.ascape.util.data.StatCollectorCond;
import org.ascape.view.vis.ChartView;
import org.ascape.view.vis.Overhead2DView;
public class CoordinationGame extends Scape {
// model-scope variables
// model-scope variables
protected int nPlayers = 100;
protected int latticeWidth = 30;
protected int latticeHeight = 30;
public int coordinateOnBlue = 1;
public int coordinateOnRed = 1;
public int error = 0;
Scape lattice;
Scape players;
Overhead2DView overheadView;
// creates scapes and agents
public void createScape() {
super.createScape();
lattice = new Scape(new Array2DVonNeumann());
lattice.setPrototypeAgent(new HostCell());
lattice.setExtent(latticeWidth, latticeHeight);
CoordinationGamePlayer cgplayer = new CoordinationGamePlayer();
cgplayer.setHostScape(lattice);
players = new Scape();
players.setName("Players");
players.setPrototypeAgent(cgplayer);
players.setExecutionOrder(Scape.RULE_ORDER);
add(lattice);
add(players);
StatCollector CountReds = new StatCollectorCond("Reds") {
public boolean meetsCondition(Object object) {
return (((CoordinationGamePlayer) object).getColor() == Color.red);
}
};
StatCollector CountBlues = new StatCollectorCond("Blues") {
public boolean meetsCondition(Object object) {
return (((CoordinationGamePlayer) object).getColor() == Color.blue);
}
};
StatCollector AvgPayoff = new StatCollectorCSAMM("Payoff") {
public double getValue(Object object) {
return ((CoordinationGamePlayer) object).getRunningTotal();
}
};
players.addStatCollector(CountReds);
players.addStatCollector(CountBlues);
players.addStatCollector(AvgPayoff);
}
public void scapeSetup(ScapeEvent scapeEvent) {
((Scape) players).setExtent(nPlayers);
}
// create views and charts
public void createGraphicViews() {
super.createGraphicViews();
ChartView chart = new ChartView();
players.addView(chart);
chart.addSeries("Count Reds", Color.red);
chart.addSeries("Count Blues", Color.blue);
chart.addSeries("Average Payoff", Color.black);
chart.setDisplayPoints(100);
overheadView = new Overhead2DView();
overheadView.setCellSize(15);
lattice.addView(overheadView);
}
// get() and set() methods for the model variables
public int getRedScore() {
return coordinateOnRed;
}
public void setRedScore(int NewcoordinateOnRed) {
coordinateOnRed = NewcoordinateOnRed;
}
public int getBlueScore() {
return coordinateOnBlue;
}
public void setBlueScore(int NewcoordinateOnBlue) {
coordinateOnBlue = NewcoordinateOnBlue;
}
public int getError() {
return error;
}
public void setError(int NewError) {
error = NewError;
}
public int getnPlayers() {
return nPlayers;
}
public void setnPlayers(int NewnPlayers) {
nPlayers = NewnPlayers;
}
}
package edu.brook.manual.model3;
import java.awt.Color;
import org.ascape.model.Agent;
import org.ascape.model.CellOccupant;
public class CoordinationGamePlayer extends CellOccupant {
// cgplayer variables
protected Color myColor;
protected int totalScore;
protected int[] recentPlays;
protected Color[] recentPartners;
protected int count = 0;
protected int totalreds;
protected int totalblues;
// agent initialization method
public void initialize() {
doRandom();
recentPlays = new int[5];
recentPartners = new Color [5];
}
// add rules to the scape
public void scapeCreated() {
getScape().addInitialRule(MOVE_RANDOM_LOCATION_RULE);
getScape().addRule(RANDOM_WALK_RULE);
getScape().addRule(UPDATE_RULE);
getScape().addRule(PLAY_RANDOM_NEIGHBOR_RULE);
}
// best reply rule
public void update() {
count++;
if (count > recentPlays.length) {
if (randomInRange(0, 100) >= ((CoordinationGame) getRoot()).error) {
int redScore;
int blueScore;
blueScore = totalblues * ((CoordinationGame) getRoot()).getBlueScore();
redScore = totalreds * ((CoordinationGame) getRoot()).getRedScore();
if (redScore > blueScore) {
myColor = Color.red;
} else if (blueScore > redScore) {
myColor = Color.blue;
} else {
doRandom();
}
} else {
doRandom();
}
}
}
public void doRandom() {
if (randomInRange(0, 1) > 0) {
myColor = Color.red;
} else {
myColor = Color.blue;
}
}
// play rule
public void play(Agent partner) {
int score;
if ((((CoordinationGamePlayer) partner).getColor() == myColor) && (myColor == Color.blue)) {
score = ((CoordinationGame) getRoot()).getBlueScore();
} else if ((((CoordinationGamePlayer) partner).getColor() == myColor) && (myColor == Color.red)) {
score = ((CoordinationGame) getRoot()).getRedScore();
} else {
score = 0;
}
count = count + 1;
updateRecentPlays(score);
updateRecentPartners(((CoordinationGamePlayer) partner).getColor());
}
// keep track of recent partners
public void updateRecentPartners(Color ncolor) {
totalreds = 0;
totalblues = 0;
for (int i = 0; i < (recentPartners.length - 1); i++) {
recentPartners[i] = recentPartners[i + 1];
if (recentPartners[i] == Color.red) {
totalreds = totalreds + 1;
} else if (recentPartners[i] == Color.blue) {
totalblues = totalblues + 1;
}
}
recentPartners[recentPartners.length - 1] = ncolor;
if (ncolor == Color.red) {
totalreds = totalreds + 1;
}
if (ncolor == Color.blue) {
totalblues = totalblues + 1;
}
}
// keep track of the recent scores
public void updateRecentPlays(int score) {
totalScore = 0;
for (int i = 0; i < (recentPlays.length - 1); i++) {
recentPlays[i] = recentPlays[i + 1];
totalScore = totalScore + recentPlays[i];
}
recentPlays[recentPlays.length - 1] = score;
totalScore = totalScore + recentPlays[recentPlays.length - 1];
}
// cgplayer get() and set() methods
public Color getColor() {
return myColor;
}
public void setColor(Color ncolor) {
myColor = ncolor;
}
public int getRunningTotal() {
return (totalScore * 10);
}
}
Model 4: Biological Coordination Game
package edu.brook.manual.model4;
import java.awt.Color;
import org.ascape.model.HostCell;
import org.ascape.model.Scape;
import org.ascape.model.event.ScapeEvent;
import org.ascape.model.space.Array2DVonNeumann;
import org.ascape.util.data.StatCollector;
import org.ascape.util.data.StatCollectorCSAMM;
import org.ascape.util.data.StatCollectorCond;
import org.ascape.view.vis.ChartView;
import org.ascape.view.vis.Overhead2DView;
public class CoordinationGame extends Scape {
// model-scope variables
// model-scope variables
protected int nPlayers = 100;
protected int latticeWidth = 30;
protected int latticeHeight = 30;
public int coordinateOnBlue = 1;
public int coordinateOnRed = 1;
public int error = 0;
public int turns = 5;
public int repScore = 5;
public int minScore = 3;
Scape lattice;
Scape players;
Overhead2DView overheadView;
// creates scapes and agents
public void createScape() {
super.createScape();
lattice = new Scape(new Array2DVonNeumann());
lattice.setPrototypeAgent(new HostCell());
lattice.setExtent(latticeWidth, latticeHeight);
CoordinationGamePlayer cgplayer = new CoordinationGamePlayer();
cgplayer.setHostScape(lattice);
players = new Scape();
players.setName("Players");
players.setPrototypeAgent(cgplayer);
players.setExecutionOrder(Scape.RULE_ORDER);
add(lattice);
add(players);
StatCollector CountReds = new StatCollectorCond("Reds") {
public boolean meetsCondition(Object object) {
return (((CoordinationGamePlayer) object).getColor() == Color.red);
}
};
StatCollector CountBlues = new StatCollectorCond("Blues") {
public boolean meetsCondition(Object object) {
return (((CoordinationGamePlayer) object).getColor() == Color.blue);
}
};
StatCollector AvgPayoff = new StatCollectorCSAMM("Payoff") {
public double getValue(Object object) {
return ((CoordinationGamePlayer) object).getRunningTotal();
}
};
players.addStatCollector(CountReds);
players.addStatCollector(CountBlues);
players.addStatCollector(AvgPayoff);
}
public void scapeSetup(ScapeEvent scapeEvent) {
((Scape) players).setExtent(nPlayers);
}
// create views and charts
public void createGraphicViews() {
super.createGraphicViews();
ChartView chart = new ChartView();
players.addView(chart);
chart.addSeries("Count Reds", Color.red);
chart.addSeries("Count Blues", Color.blue);
chart.addSeries("Average Payoff", Color.black);
chart.setDisplayPoints(100);
overheadView = new Overhead2DView();
overheadView.setCellSize(15);
lattice.addView(overheadView);
}
// get() and set() methods for the model variables
public int getRedScore() {
return coordinateOnRed;
}
public void setRedScore(int NewcoordinateOnRed) {
coordinateOnRed = NewcoordinateOnRed;
}
public int getBlueScore() {
return coordinateOnBlue;
}
public void setBlueScore(int NewcoordinateOnBlue) {
coordinateOnBlue = NewcoordinateOnBlue;
}
public int getError() {
return error;
}
public void setError(int NewError) {
error = NewError;
}
public int getnPlayers() {
return nPlayers;
}
public void setnPlayers(int NewnPlayers) {
nPlayers = NewnPlayers;
}
public int getTurnstoDie() {
return turns;
}
public void setTurnstoDie(int Newnum) {
turns = Newnum;
}
public int getrepScore() {
return repScore;
}
public void setrepScore(int Newscore) {
repScore = Newscore;
}
public int getMinDieScore() {
return minScore;
}
public void setMinDieScore(int Newscore) {
minScore = Newscore;
}
}
package edu.brook.manual.model4;
import java.awt.Color;
import org.ascape.model.Agent;
import org.ascape.model.CellOccupant;
public class CoordinationGamePlayer extends CellOccupant {
// cgplayer variables
protected Color myColor;
protected int totalScore;
protected int[] recentPlays;
protected int count = 0;
protected int totalreds;
protected int totalblues;
// agent initialization method
public void initialize() {
doRandom();
recentPlays = new int[5];
}
// add rules to the scape
public void scapeCreated() {
getScape().addInitialRule(MOVE_RANDOM_LOCATION_RULE);
getScape().addRule(RANDOM_WALK_RULE);
getScape().addRule(PLAY_RANDOM_NEIGHBOR_RULE);
getScape().addRule(METABOLISM_RULE);
}
public void doRandom() {
if (randomInRange(0, 1) > 0) {
myColor = Color.red;
} else {
myColor = Color.blue;
}
}
// play rule
public void play(Agent partner) {
int score;
if ((((CoordinationGamePlayer) partner).getColor() == myColor) && (myColor == Color.blue)) {
score = ((CoordinationGame) getRoot()).getBlueScore();
} else if ((((CoordinationGamePlayer) partner).getColor() == myColor) && (myColor == Color.red)) {
score = ((CoordinationGame) getRoot()).getRedScore();
} else {
score = 0;
}
count = count + 1;
updateRecentPlays(score);
}
// keep track of the recent scores
public void updateRecentPlays(int score) {
totalScore = 0;
for (int i = 0; i < (recentPlays.length - 1); i++) {
recentPlays[i] = recentPlays[i + 1];
totalScore = totalScore + recentPlays[i];
}
recentPlays[recentPlays.length - 1] = score;
totalScore = totalScore + recentPlays[recentPlays.length - 1];
}
// either die or reproduce
public void metabolism() {
if (totalScore == ((CoordinationGame) getRoot()).getrepScore()) {
count = 0;
reproduce();
for (int i = 0; i < (recentPlays.length - 1); i++) {
recentPlays[i] = 0;
}
} else if (totalScore <= ((CoordinationGame) getRoot()).getMinDieScore() && count >= ((CoordinationGame) getRoot()).getTurnstoDie()) {
this.die();
}
}
public void reproduce() {
if (this.getHostCell().isNeighborAvailable()) {
CoordinationGamePlayer baby = (CoordinationGamePlayer) this.clone();
getScape().add(baby);
baby.initialize();
baby.setColor(myColor);
baby.moveTo(this.getHostCell().findRandomAvailableNeighbor());
}
}
// cgplayer get() and set() methods
public Color getColor() {
return myColor;
}
public void setColor(Color ncolor) {
myColor = ncolor;
}
public int getRunningTotal() {
return (totalScore * 10);
}
}
Model 5: Friends Network Model
package edu.brook.manual.model5;
import java.awt.Color;
import org.ascape.model.HostCell;
import org.ascape.model.Scape;
import org.ascape.model.event.ScapeEvent;
import org.ascape.model.space.Array2DVonNeumann;
import org.ascape.util.data.StatCollector;
import org.ascape.util.data.StatCollectorCSAMM;
import org.ascape.util.data.StatCollectorCond;
import org.ascape.view.vis.ChartView;
import org.ascape.view.vis.Overhead2DView;
public class CoordinationGame extends Scape {
// model-scope variables
// model-scope variables
protected int nPlayers = 100;
protected int latticeWidth = 30;
protected int latticeHeight = 30;
public int coordinateOnBlue = 1;
public int coordinateOnRed = 1;
public int error = 0;
public int turns = 5;
public int repScore = 5;
public int minScore = 3;
public int friends_size = 4;
Scape lattice;
Scape players;
Overhead2DView overheadView;
// creates scapes and agents
public void createScape() {
super.createScape();
lattice = new Scape(new Array2DVonNeumann());
lattice.setPrototypeAgent(new HostCell());
lattice.setExtent(latticeWidth, latticeHeight);
CoordinationGamePlayer cgplayer = new CoordinationGamePlayer();
cgplayer.setHostScape(lattice);
players = new Scape();
players.setName("Players");
players.setPrototypeAgent(cgplayer);
players.setExecutionOrder(Scape.RULE_ORDER);
add(lattice);
add(players);
StatCollector CountReds = new StatCollectorCond("Reds") {
public boolean meetsCondition(Object object) {
return (((CoordinationGamePlayer) object).getColor() == Color.red);
}
};
StatCollector CountBlues = new StatCollectorCond("Blues") {
public boolean meetsCondition(Object object) {
return (((CoordinationGamePlayer) object).getColor() == Color.blue);
}
};
StatCollector AvgPayoff = new StatCollectorCSAMM("Payoff") {
public double getValue(Object object) {
return ((CoordinationGamePlayer) object).getRunningTotal();
}
};
players.addStatCollector(CountReds);
players.addStatCollector(CountBlues);
players.addStatCollector(AvgPayoff);
}
public void scapeSetup(ScapeEvent scapeEvent) {
((Scape) players).setExtent(nPlayers);
}
// create views and charts
public void createGraphicViews() {
super.createGraphicViews();
ChartView chart = new ChartView();
players.addView(chart);
chart.addSeries("Count Reds", Color.red);
chart.addSeries("Count Blues", Color.blue);
chart.addSeries("Average Payoff", Color.black);
chart.setDisplayPoints(100);
overheadView = new Overhead2DView();
overheadView.setCellSize(15);
overheadView.setDrawNetwork(true);
lattice.addView(overheadView);
}
// get() and set() methods for the model variables
public int getRedScore() {
return coordinateOnRed;
}
public void setRedScore(int NewcoordinateOnRed) {
coordinateOnRed = NewcoordinateOnRed;
}
public int getBlueScore() {
return coordinateOnBlue;
}
public void setBlueScore(int NewcoordinateOnBlue) {
coordinateOnBlue = NewcoordinateOnBlue;
}
public int getError() {
return error;
}
public void setError(int NewError) {
error = NewError;
}
public int getnPlayers() {
return nPlayers;
}
public void setnPlayers(int NewnPlayers) {
nPlayers = NewnPlayers;
}
public int getTurnstoDie() {
return turns;
}
public void setTurnstoDie(int Newnum) {
turns = Newnum;
}
public int getrepScore() {
return repScore;
}
public void setrepScore(int Newscore) {
repScore = Newscore;
}
public int getMinDieScore() {
return minScore;
}
public void setMinDieScore(int Newscore) {
minScore = Newscore;
}
public Scape getPlayers() {
return players;
}
}
package edu.brook.manual.model5;
import java.awt.Color;
import java.util.ArrayList;
import java.util.List;
import org.ascape.model.Agent;
import org.ascape.model.CellOccupant;
import org.ascape.model.rule.Rule;
public class CoordinationGamePlayer extends CellOccupant {
// cgplayer variables
protected Color myColor;
protected int totalScore;
protected int[] recentPlays;
protected int count = 0;
protected int totalreds;
protected int totalblues;
protected List<CoordinationGamePlayer> friends;
// agent initialization method
public void initialize() {
doRandom();
recentPlays = new int[5];
friends = new ArrayList<CoordinationGamePlayer>();
}
// add rules to the scape
public void scapeCreated() {
getScape().addInitialRule(MOVE_RANDOM_LOCATION_RULE);
getScape().addInitialRule(new Rule("SetNewNetwork") {
public void execute(Agent agent) {
((CoordinationGamePlayer) agent).setNewNetwork();
}
});
getScape().addRule(RANDOM_WALK_RULE);
getScape().addRule(UPDATE_RULE);
getScape().addRule(PLAY_RANDOM_NEIGHBOR_RULE);
}
public void doRandom() {
if (randomInRange(0, 1) > 0) {
myColor = Color.red;
} else {
myColor = Color.blue;
}
}
// imitation rule
public void update() {
count++;
if (count > recentPlays.length) {
if (randomInRange(0, 100) >= ((CoordinationGame) getRoot()).error) {
int currScore = 0;
int bestScore = this.totalScore;
// CellOccupant[] neighbors;
// neighbors = getHostCell().getNeighboringOccupants();
// New Social Network Code
List neighbors = getNetwork();
for (Object agent : neighbors) {
currScore = ((CoordinationGamePlayer) agent).totalScore;
if (currScore > bestScore) {
bestScore = currScore;
myColor = ((CoordinationGamePlayer) agent).getColor();
}
}
} else {
doRandom();
}
}
}
// play rule
public void play(Agent partner) {
int score;
if ((((CoordinationGamePlayer) partner).getColor() == myColor) && (myColor == Color.blue)) {
score = ((CoordinationGame) getRoot()).getBlueScore();
} else if ((((CoordinationGamePlayer) partner).getColor() == myColor) && (myColor == Color.red)) {
score = ((CoordinationGame) getRoot()).getRedScore();
} else {
score = 0;
}
count = count + 1;
updateRecentPlays(score);
}
// keep track of the recent scores
public void updateRecentPlays(int score) {
totalScore = 0;
for (int i = 0; i < (recentPlays.length - 1); i++) {
recentPlays[i] = recentPlays[i + 1];
totalScore = totalScore + recentPlays[i];
}
recentPlays[recentPlays.length - 1] = score;
totalScore = totalScore + recentPlays[recentPlays.length - 1];
}
// create friends networks at model setup
public void setNewNetwork() {
friends.clear();
for (int i = 0; i < ((CoordinationGame) getRoot()).friends_size; i++) {
friends.add((CoordinationGamePlayer) ((CoordinationGame) getRoot()).getPlayers().findRandom());
}
this.setNetwork(friends);
}
// cgplayer get() and set() methods
public Color getColor() {
return myColor;
}
public void setColor(Color ncolor) {
myColor = ncolor;
}
public int getRunningTotal() {
return (totalScore * 10);
}
}