|
From: <jom...@us...> - 2008-04-22 16:24:38
|
Revision: 1238
http://jason.svn.sourceforge.net/jason/?rev=1238&view=rev
Author: jomifred
Date: 2008-04-22 09:24:29 -0700 (Tue, 22 Apr 2008)
Log Message:
-----------
improvements in jason team
Modified Paths:
--------------
trunk/applications/jason-team/src/java/arch/ACArchitecture.java
trunk/applications/jason-team/src/java/arch/ACProxy.java
trunk/applications/jason-team/src/java/arch/CowboyArch.java
trunk/applications/jason-team/src/java/arch/LocalWorldModel.java
trunk/applications/jason-team/src/java/env/WorldModel.java
trunk/applications/jason-team/src/java/jia/Search.java
trunk/applications/jason-team/src/java/jia/Vec.java
trunk/applications/jason-team/src/java/jia/direction.java
trunk/applications/jason-team/src/java/jia/herd_position.java
trunk/applications/jason-team/src/java/test/TestBasicHerding.java
Modified: trunk/applications/jason-team/src/java/arch/ACArchitecture.java
===================================================================
--- trunk/applications/jason-team/src/java/arch/ACArchitecture.java 2008-04-22 11:20:01 UTC (rev 1237)
+++ trunk/applications/jason-team/src/java/arch/ACArchitecture.java 2008-04-22 16:24:29 UTC (rev 1238)
@@ -138,6 +138,7 @@
Queue<ActionExec> toExecute = new ConcurrentLinkedQueue<ActionExec>();
Lock lock = new ReentrantLock();
Condition cycle = lock.newCondition();
+ long timestartcycle = 0;
WaitSleep() {
super("WaitSpeepToSendAction");
@@ -155,24 +156,37 @@
}
void newCycle() {
- String w = "";
- if (lastActionInCurrentCycle == null) {
- addRestart();
- w = "*** ";
- }
- logger.info(w+"Last sent action was "+lastActionInCurrentCycle+" for cycle "+getCycle()+". The following was not sent: "+toExecute);
-
- setLastAct(lastActionInCurrentCycle);
- lastActionInCurrentCycle = null;
-
+ StringBuilder notsent = new StringBuilder();
+ if (toExecute.size() > 1) {
+ notsent.append(" The following was not sent: ");
+ }
// set all actions as successfully executed
List<ActionExec> feedback = getTS().getC().getFeedbackActions();
while (!toExecute.isEmpty()) {
ActionExec action = toExecute.poll();
action.setResult(true);
feedback.add(action);
+ if (!toExecute.isEmpty())
+ notsent.append(action.getActionTerm()+" ");
}
go(); // reset the wait
+
+ // prepare msg to print out
+ String w = "";
+ if (lastActionInCurrentCycle == null) {
+ addRestart();
+ w = "*** ";
+ }
+
+ long timetoact = 0;
+ if (timestartcycle > 0) {
+ timetoact = System.currentTimeMillis() - timestartcycle;
+ }
+ timestartcycle = System.currentTimeMillis();
+
+ logger.info(w+"Last sent action was "+lastActionInCurrentCycle+" for cycle "+getCycle()+ " (act in "+timetoact+" ms). "+notsent);
+ setLastAct(lastActionInCurrentCycle);
+ lastActionInCurrentCycle = null;
}
void go() {
Modified: trunk/applications/jason-team/src/java/arch/ACProxy.java
===================================================================
--- trunk/applications/jason-team/src/java/arch/ACProxy.java 2008-04-22 11:20:01 UTC (rev 1237)
+++ trunk/applications/jason-team/src/java/arch/ACProxy.java 2008-04-22 16:24:29 UTC (rev 1238)
@@ -206,7 +206,7 @@
}
- arq.sendCowsToTeam();
+ //arq.sendCowsToTeam();
arq.startNextStep(step, percepts);
//if (logger.isLoggable(Level.FINE))
Modified: trunk/applications/jason-team/src/java/arch/CowboyArch.java
===================================================================
--- trunk/applications/jason-team/src/java/arch/CowboyArch.java 2008-04-22 11:20:01 UTC (rev 1237)
+++ trunk/applications/jason-team/src/java/arch/CowboyArch.java 2008-04-22 16:24:29 UTC (rev 1238)
@@ -319,10 +319,12 @@
while (im.hasNext()) {
Message m = im.next();
if (m.getIlForce().equals("tell-cows")) {
- for (Location l: (Collection<Location>)m.getPropCont()) {
- cowPerceived(l.x, l.y);
+ im.remove();
+ if (model != null) {
+ for (Location l: (Collection<Location>)m.getPropCont()) {
+ cowPerceived(l.x, l.y);
+ }
}
- im.remove();
} else {
String ms = m.getPropCont().toString();
if (ms.startsWith("cell") && ms.endsWith("obstacle)") && model != null) {
Modified: trunk/applications/jason-team/src/java/arch/LocalWorldModel.java
===================================================================
--- trunk/applications/jason-team/src/java/arch/LocalWorldModel.java 2008-04-22 11:20:01 UTC (rev 1237)
+++ trunk/applications/jason-team/src/java/arch/LocalWorldModel.java 2008-04-22 16:24:29 UTC (rev 1238)
@@ -23,9 +23,13 @@
int minVisited = 0; // min value for near least visited
private Random random = new Random();
-
+
Set<Location> cows = new HashSet<Location>();
+ int[][] cowsrep; // cows repulsion
+ int[][] agsrep; // agents repulsion
+ int[][] obsrep; // obstacle repulsion
+
//private Logger logger = Logger.getLogger("jasonTeamSimLocal.mas2j." + LocalWorldModel.class.getName());
public static LocalWorldModel create(int w, int h, int nbAg) {
@@ -40,39 +44,109 @@
super(w, h, nbAg);
visited = new int[getWidth()][getHeight()];
- for (int i = 0; i < getWidth(); i++) {
- for (int j = 0; j < getHeight(); j++) {
+ for (int i = 0; i < getWidth(); i++)
+ for (int j = 0; j < getHeight(); j++)
visited[i][j] = 0;
- }
- }
+
+ cowsrep = new int[getWidth()][getHeight()];
+
+ agsrep = new int[getWidth()][getHeight()];
+ for (int i = 0; i < getWidth(); i++)
+ for (int j = 0; j < getHeight(); j++)
+ agsrep[i][j] = 0;
+
+ obsrep = new int[getWidth()][getHeight()];
+ for (int i = 0; i < getWidth(); i++)
+ for (int j = 0; j < getHeight(); j++)
+ obsrep[i][j] = 0;
+
}
+
+ @Override
+ public void add(int value, int x, int y) {
+ super.add(value, x, y);
+ //if (value == WorldModel.AGENT || value == WorldModel.ENEMY) {
+ if (value == WorldModel.ENEMY) {
+ increp(agsrep, x, y, 2, 2);
+ } else if (value == WorldModel.OBSTACLE) {
+ increp(obsrep, x, y, 1, 1);
+ }
+ }
+ @Override
+ public void remove(int value, int x, int y) {
+ super.remove(value, x, y);
+ //if (value == WorldModel.AGENT || value == WorldModel.ENEMY) {
+ if (value == WorldModel.ENEMY) {
+ increp(agsrep, x, y, 2, -2);
+ }
+ }
+
public void clearCows() {
removeAll(WorldModel.COW);
+
+ for (int i = 0; i < getWidth(); i++)
+ for (int j = 0; j < getHeight(); j++)
+ cowsrep[i][j] = 0;
+
cows.clear();
}
+
public void addCow(int x, int y) {
add(WorldModel.COW, x, y);
cows.add(new Location(x,y));
+
+ increp(cowsrep, x, y, 2, 1);
}
+
+ private void increp(int[][] m, int x, int y, int maxr, int value) {
+ for (int r = 1; r <= maxr; r++)
+ for (int c = x-r; c <= x+r; c++)
+ for (int l = y-r; l <= y+r; l++)
+ if (inGrid(c,l))
+ m[c][l] += value;
+ }
+
public void addCow(Location l) {
addCow(l.x, l.y);
}
+
public Collection<Location> getCows() {
return cows;
}
+
+ public int getCowsRep(int x, int y) {
+ return cowsrep[x][y];
+ }
+ public int getAgsRep(int x, int y) {
+ return agsrep[x][y];
+ }
+ public int getObsRep(int x, int y) {
+ return obsrep[x][y];
+ }
public Location nearFree(Location l) throws Exception {
int w = 0;
List<Location> options = new ArrayList<Location>();
while (true) {
options.clear();
+ for (int y=l.y-w+1; y<l.y+w; y++) {
+ //System.out.println(" "+(l.x+w)+" "+y);
+ //System.out.println(" "+(l.x-w)+" "+y);
+ if (isFree(l.x-w,y))
+ options.add(new Location(l.x-w,y));
+ if (isFree(l.x+w,y))
+ options.add(new Location(l.x+w,y));
+ }
for (int x=l.x-w; x<=l.x+w;x++) {
+ //System.out.println(" "+x+" "+(l.y-w));
+ //System.out.println(" "+x+" "+(l.y+w));
if (isFree(x,l.y-w))
options.add(new Location(x,l.y-w));
if (isFree(x,l.y+w))
options.add(new Location(x,l.y+w));
}
+ //System.out.println(w + " " + options);
if (!options.isEmpty())
return options.get(random.nextInt(options.size()));
w++;
@@ -100,15 +174,13 @@
}
public void incVisited(int x, int y) {
visited[x][y] += 2;
-
- // TODO: review this
- if (x > 0) visited[x-1][y]++;
- if (y > 0) visited[x][y-1]++;
- if (y > 0 && x > 0) visited[x-1][y-1]++;
- if (y+1 < getHeight()) visited[x][y+1]++;
- if (x > 0 && y+1 < getHeight()) visited[x-1][y+1]++;
- if (x+1 < getWidth()) visited[x+1][y]++;
- if (x+1 < getWidth() && y > 0) visited[x+1][y-1]++;
+ if (x > 0) visited[x-1][y ]++;
+ if (y > 0) visited[x ][y-1]++;
+ if (y > 0 && x > 0) visited[x-1][y-1]++;
+ if (y+1 < getHeight()) visited[x ][y+1]++;
+ if (x > 0 && y+1 < getHeight()) visited[x-1][y+1]++;
+ if (x+1 < getWidth()) visited[x+1][y ]++;
+ if (x+1 < getWidth() && y > 0) visited[x+1][y-1]++;
if (x+1 < getWidth() && y+1 < getHeight()) visited[x+1][y+1]++;
}
Modified: trunk/applications/jason-team/src/java/env/WorldModel.java
===================================================================
--- trunk/applications/jason-team/src/java/env/WorldModel.java 2008-04-22 11:20:01 UTC (rev 1237)
+++ trunk/applications/jason-team/src/java/env/WorldModel.java 2008-04-22 16:24:29 UTC (rev 1238)
@@ -158,7 +158,7 @@
public void wall(int x1, int y1, int x2, int y2) {
for (int i=x1; i<=x2; i++) {
for (int j=y1; j<=y2; j++) {
- data[i][j] = OBSTACLE;
+ add(OBSTACLE, i, j);;
}
}
}
Modified: trunk/applications/jason-team/src/java/jia/Search.java
===================================================================
--- trunk/applications/jason-team/src/java/jia/Search.java 2008-04-22 11:20:01 UTC (rev 1237)
+++ trunk/applications/jason-team/src/java/jia/Search.java 2008-04-22 16:24:29 UTC (rev 1238)
@@ -27,6 +27,9 @@
final boolean considerAgentsAsObstacles;
final boolean considerCorralAsObstacles;
final boolean considerCowsAsObstacles;
+ final boolean considerRepulsionForCows;
+ int maxDistFromCluster;
+
WorldModel.Move[] actionsOrder;
int nbStates = 0;
AgArch agArch;
@@ -44,30 +47,43 @@
Logger logger = Logger.getLogger(Search.class.getName());
- public Search(LocalWorldModel m, Location from, Location to, WorldModel.Move[] actions, boolean considerAgentsAsObstacles, boolean considerCorralAsObstacles, boolean considerCowsAsObstacles, AgArch agArch) {
+ public Search(LocalWorldModel m, Location from, Location to, WorldModel.Move[] actions,
+ boolean considerAgentsAsObstacles,
+ boolean considerCorralAsObstacles,
+ boolean considerCowsAsObstacles,
+ boolean considerRepulsionForCows,
+ AgArch agArch) {
+
this.model = m;
this.from = from;
this.to = to;
this.considerAgentsAsObstacles = considerAgentsAsObstacles;
this.considerCorralAsObstacles = considerCorralAsObstacles;
this.considerCowsAsObstacles = considerCowsAsObstacles;
+ this.considerRepulsionForCows = considerRepulsionForCows;
this.agArch = agArch;
if (actions != null) {
this.actionsOrder = actions;
} else {
this.actionsOrder = defaultActions;
}
+
+ this.maxDistFromCluster = 4;
}
+
+ public void setMaxDistFromCluster(int m) {
+ maxDistFromCluster = m;
+ }
/** used normally to discover the distance from 'from' to 'to' */
Search(LocalWorldModel m, Location from, Location to, AgArch agArch) {
- this(m,from,to,null,false, false, false, agArch);
+ this(m,from,to,null,false, false, false, false, agArch);
}
public Nodo search() throws Exception {
Busca searchAlg = new AEstrela();
//searchAlg.ssetMaxAbertos(1000);
- GridState root = new GridState(from, WorldModel.Move.skip, this, 1);
+ GridState root = new GridState(from, WorldModel.Move.skip, this);
root.setAsRoot();
return searchAlg.busca(root);
}
@@ -142,13 +158,11 @@
final Search ia;
final int hashCode;
boolean isRoot = false;
- int cost = 1;
- public GridState(Location l, WorldModel.Move op, Search ia, int cost) {
+ public GridState(Location l, WorldModel.Move op, Search ia) {
this.pos = l;
this.op = op;
this.ia = ia;
- this.cost = cost;
hashCode = pos.hashCode();
ia.nbStates++;
@@ -159,7 +173,19 @@
}
public int custo() {
- return cost;
+ if (isRoot)
+ return 0;
+ if (ia.considerCowsAsObstacles)
+ return ia.model.getCowsRep(pos.x, pos.y)+1;
+
+ if (ia.considerRepulsionForCows) {
+ // consider the cost of agents only if they are near
+ int c = ia.model.getObsRep(pos.x, pos.y) + 1;
+ if (ia.from.maxBorder(pos) <= ia.maxDistFromCluster)
+ c += ia.model.getAgsRep(pos.x, pos.y);
+ return c;
+ }
+ return 1;
}
public boolean ehMeta() {
@@ -208,14 +234,7 @@
if (ia.considerCowsAsObstacles && ia.model.hasObject(WorldModel.COW,newl) && ia.from.maxBorder(newl) <= Search.DIST_FOR_AG_OBSTACLE)
return;
- int cost = 1;
-
- if (ia.considerCowsAsObstacles) {
- cost += ia.model.countObjInArea(WorldModel.CORRAL, newl,1);
- cost += ia.model.countObjInArea(WorldModel.CORRAL, newl,2);
- }
-
- s.add(new GridState(newl,op,ia, cost));
+ s.add(new GridState(newl,op,ia));
}
public boolean equals(Object o) {
Modified: trunk/applications/jason-team/src/java/jia/Vec.java
===================================================================
--- trunk/applications/jason-team/src/java/jia/Vec.java 2008-04-22 11:20:01 UTC (rev 1237)
+++ trunk/applications/jason-team/src/java/jia/Vec.java 2008-04-22 16:24:29 UTC (rev 1238)
@@ -8,8 +8,9 @@
import arch.LocalWorldModel;
-public class Vec implements Cloneable {
+public final class Vec implements Cloneable {
+ // immutable fields (for a immutable object)
public final double x,y;
public final double r,t;
@@ -46,7 +47,7 @@
return new Vec(x - v.x, y - v.y);
}
public Vec product(double e) {
- return new Vec((int)(x * e), (int)(y *e));
+ return new Vec( x * e, y * e);
}
public Vec newAngle(double t) {
while (t > PI2) t = t - PI2;
@@ -69,7 +70,6 @@
return this; // it is an immutable object, no need to create a new one
}
-
/**
* Provides info on which octant (0-7) the vector lies in.
@@ -102,7 +102,7 @@
public static Vec max(List<Vec> vs) {
Vec max = null;
for (Vec v: vs) {
- if (max == null || max.magnitude() < v.magnitude())
+ if (max == null || max.r < v.r)
max = v;
}
return max;
@@ -117,14 +117,16 @@
}
public static List<Vec> cluster(List<Vec> vs, int maxstddev) {
- vs = new ArrayList<Vec>(vs);
- Vec stddev = Vec.stddev(vs);
+ vs = new ArrayList<Vec>(vs); // result vectors in the cluster
+ Vec mean = Vec.mean(vs);
+ Vec stddev = Vec.stddev(vs, mean);
+
// remove max if stddev is too big
while (stddev.magnitude() > maxstddev) {
- Vec mean = Vec.mean(vs);
Vec max = Vec.max(Vec.sub(vs, mean));
vs.remove(max.add(mean));
- stddev = Vec.stddev(vs);
+ mean = Vec.mean(vs);
+ stddev = Vec.stddev(vs, mean);
}
return vs;
}
@@ -132,7 +134,7 @@
public static Vec mean(List<Vec> vs) {
if (vs.isEmpty())
return new Vec(0,0);
- int x = 0, y = 0;
+ double x = 0, y = 0;
for (Vec v: vs) {
x += v.x;
y += v.y;
@@ -140,19 +142,18 @@
return new Vec(x/vs.size(), y/vs.size());
}
- public static Vec stddev(List<Vec> vs) {
+ public static Vec stddev(List<Vec> vs, Vec mean) {
if (vs.isEmpty())
return new Vec(0,0);
- Vec mean = mean(vs);
- int x = 0, y = 0;
+ double x = 0, y = 0;
for (Vec v: vs) {
- x += Math.pow(v.x - mean.x,2);
- y += Math.pow(v.y - mean.y,2);
+ x += Math.pow(v.x - mean.x, 2);
+ y += Math.pow(v.y - mean.y, 2);
}
x = x / vs.size();
y = y / vs.size();
- return new Vec( (int)Math.sqrt(x), (int)Math.sqrt(y));
+ return new Vec( Math.sqrt(x), Math.sqrt(y));
}
Modified: trunk/applications/jason-team/src/java/jia/direction.java
===================================================================
--- trunk/applications/jason-team/src/java/jia/direction.java 2008-04-22 11:20:01 UTC (rev 1237)
+++ trunk/applications/jason-team/src/java/jia/direction.java 2008-04-22 16:24:29 UTC (rev 1238)
@@ -61,7 +61,7 @@
actionsOrder[i2] = actionsOrder[i1];
actionsOrder[i1] = temp;
- Search astar = new Search(model, from, to, actionsOrder, true, true, true, ts.getUserAgArch());
+ Search astar = new Search(model, from, to, actionsOrder, true, true, true, false, ts.getUserAgArch());
Nodo solution = astar.search();
if (solution != null) {
WorldModel.Move m = astar.firstAction(solution);
Modified: trunk/applications/jason-team/src/java/jia/herd_position.java
===================================================================
--- trunk/applications/jason-team/src/java/jia/herd_position.java 2008-04-22 11:20:01 UTC (rev 1237)
+++ trunk/applications/jason-team/src/java/jia/herd_position.java 2008-04-22 16:24:29 UTC (rev 1238)
@@ -9,6 +9,7 @@
import java.util.ArrayList;
import java.util.List;
+import java.util.ListIterator;
import java.util.logging.Level;
import arch.CowboyArch;
@@ -97,12 +98,12 @@
cows = Vec.cluster(cows, 2); // find center/clusterise
Vec mean = Vec.mean(cows);
+ int stepsFromCenter = (int)Math.round(Vec.max(cows).sub(mean).magnitude())+1;
// run A* to see the cluster target in n steps
- Search s = new Search(model, mean.getLocation(model), model.getCorralCenter(), null, false, false, false, null);
+ Search s = new Search(model, mean.getLocation(model), model.getCorralCenter(), null, false, false, false, true, null);
+ s.setMaxDistFromCluster(stepsFromCenter+Search.DIST_FOR_AG_OBSTACLE);
List<Nodo> np = s.normalPath(s.search());
-
- int stepsFromCenter = (int)Vec.max(cows).sub(mean).magnitude()+1;
int n = Math.min(stepsFromCenter, np.size());
Vec cowstarget = new Vec(model, s.getNodeLocation(np.get(n)));
@@ -111,6 +112,13 @@
List<Location> r = new ArrayList<Location>();
for (int angle: formation.getAngles()) {
double nt = angle * (Math.PI / 180);
+
+ agTarget = agsTarget.newAngle(agsTarget.angle() + nt);
+ Location l = agTarget.add(mean).getLocation(model);
+ r.add(l);
+
+ // TODO: test the code below
+ /*
for (double varangle = nt; nt < 180; nt += 5) {
agTarget = agsTarget.newAngle(agsTarget.angle() + varangle);
Location l = agTarget.add(mean).getLocation(model);
@@ -118,7 +126,7 @@
// if l is in the path of cows, continue with next varangle
boolean inpath = false;
for (Nodo pn: np) {
- if (l.equals(s.getNodeLocation(pn))) {
+ if (l.maxBorder(s.getNodeLocation(pn)) <= 0) {
inpath = true;
break;
}
@@ -128,22 +136,27 @@
break;
}
}
+ */
}
- return r;
+ return r;
}
public Location nearFreeForAg(LocalWorldModel model, Location ag, Location t) throws Exception {
// run A* to get the path from ag to t
- Search s = new Search(model, t, ag, null, false, false, true, null);
+ if (! model.inGrid(t))
+ t = model.nearFree(t);
+
+ Search s = new Search(model, ag, t, null, true, true, true, false, null);
List<Nodo> np = s.normalPath(s.search());
int i = 0;
- for (Nodo n: np) {
+ ListIterator<Nodo> inp = np.listIterator(np.size());
+ while (inp.hasPrevious()) {
+ Nodo n = inp.previous();
if (model.isFree(s.getNodeLocation(n))) {
return s.getNodeLocation(n);
}
- i++;
- if (i > 3) // do not go to far from target
+ if (i++ > 3) // do not go to far from target
break;
}
return model.nearFree(t);
Modified: trunk/applications/jason-team/src/java/test/TestBasicHerding.java
===================================================================
--- trunk/applications/jason-team/src/java/test/TestBasicHerding.java 2008-04-22 11:20:01 UTC (rev 1237)
+++ trunk/applications/jason-team/src/java/test/TestBasicHerding.java 2008-04-22 16:24:29 UTC (rev 1238)
@@ -7,6 +7,7 @@
import java.util.ArrayList;
import java.util.List;
+import jia.Search;
import jia.Vec;
import jia.herd_position;
import jia.herd_position.Formation;
@@ -15,6 +16,7 @@
import org.junit.Test;
import arch.LocalWorldModel;
+import busca.Nodo;
import env.WorldModel;
public class TestBasicHerding {
@@ -32,7 +34,8 @@
public void scenario1() {
cowboy = new Vec(3,5);
-
+ model.add(WorldModel.AGENT, cowboy.getLocation(model));
+
cows = new Vec[5];
cows[0] = new Vec(6,7);
cows[1] = new Vec(5,30);
@@ -47,6 +50,7 @@
public void scenario2() {
cowboy = new Vec(11,3);
+ model.add(WorldModel.AGENT, cowboy.getLocation(model));
cows = new Vec[9];
cows[0] = new Vec(8,0);
@@ -76,6 +80,100 @@
assertEquals(v, v.newAngle(v.angle()));
}
+ @Test
+ public void testCowsRepMat() throws Exception {
+ scenario1();
+ assertEquals(2, model.getCowsRep(5,38));
+ assertEquals(1, model.getCowsRep(5,37));
+ assertEquals(0, model.getCowsRep(5,36));
+ assertEquals(5, model.getCowsRep(5,40));
+ }
+
+ @Test
+ public void testAgsRepMat() throws Exception {
+ scenario1();
+ //assertEquals(4, model.getAgsRep(3,43));
+ //assertEquals(2, model.getAgsRep(5,42));
+ //assertEquals(0, model.getAgsRep(6,42));
+
+ model.add(WorldModel.ENEMY, 4, 44);
+ assertEquals(4, model.getAgsRep(3,43));
+ assertEquals(2, model.getAgsRep(5,42));
+ assertEquals(2, model.getAgsRep(6,42));
+
+ model.remove(WorldModel.ENEMY, 4, 44);
+ /*assertEquals(4, model.getAgsRep(3,43));
+ assertEquals(2, model.getAgsRep(5,42));
+ assertEquals(0, model.getAgsRep(6,42));
+
+ model.remove(WorldModel.AGENT, cowboy.getLocation(model));
+ */
+ assertEquals(0, model.getAgsRep(3,43));
+ assertEquals(0, model.getAgsRep(5,42));
+ assertEquals(0, model.getAgsRep(6,42));
+
+ assertEquals(2, model.getObsRep(6, 44));
+ assertEquals(1, model.getObsRep(7, 43));
+ assertEquals(0, model.getObsRep(5, 44));
+ }
+
+ @Test
+ public void testAStar1() throws Exception {
+ scenario1();
+ Search s = new Search(model, cowboy.getLocation(model), new Location(8,37), null, true, true, true, false, null);
+ Nodo path = s.search();
+ assertEquals(13, s.normalPath(path).size());
+ }
+
+ @Test
+ public void moveCows2() throws Exception {
+ scenario1();
+
+ List<Vec> cowsl = new ArrayList<Vec>();
+ for (int i=0; i<cows.length; i++) {
+ cowsl.add(cows[i]);
+ }
+
+ // find center/clusterise
+ cowsl = Vec.cluster(cowsl, 2);
+ Vec stddev = Vec.stddev(cowsl, Vec.mean(cowsl));
+ assertTrue(stddev.magnitude() < 3);
+
+ Vec mean = Vec.mean(cowsl);
+ assertEquals(new Vec(5,8), mean);
+
+ //Search s = new Search(model, mean.getLocation(model), model.getCorralCenter(), null, false, false, false, true, null);
+ //List<Nodo> np = s.normalPath(s.search());
+
+ int stepsFromCenter = (int)Math.round(Vec.max(cowsl).sub(mean).magnitude())+1;
+ assertEquals(3, stepsFromCenter);
+
+ Location byIA = new herd_position().getAgTarget(model, Formation.one, cowboy.getLocation(model));
+ assertEquals(new Location(7,37), byIA);
+
+ byIA = new herd_position().getAgTarget(model, Formation.six, cowboy.getLocation(model));
+ assertEquals(new Location(6,37), byIA);
+
+ // add an agent in 6,38
+ model.add(WorldModel.AGENT, 6,38);
+ byIA = new herd_position().getAgTarget(model, Formation.six, cowboy.getLocation(model));
+ assertEquals(new Location(8,38), byIA);
+
+ // add an agent in 8,39 (near 8,38)
+ model.add(WorldModel.AGENT, 8,39);
+ byIA = new herd_position().getAgTarget(model, Formation.six,cowboy.getLocation(model));
+ assertEquals(new Location(3,37), byIA);
+ }
+
+ @Test
+ public void moveCows3() throws Exception {
+ scenario2();
+ model.add(WorldModel.ENEMY, 11,48);
+ Location byIA = new herd_position().getAgTarget(model, Formation.one, cowboy.getLocation(model));
+ byIA = new herd_position().nearFreeForAg(model, new Location(11,46), byIA);
+ assertEquals(new Location(11,49), byIA);
+ }
+
/*
@Test
public void oneCow() throws Exception {
@@ -184,54 +282,5 @@
}
*/
- @Test
- public void moveCows2() throws Exception {
- scenario1();
-
- List<Vec> cowsl = new ArrayList<Vec>();
- for (int i=0; i<cows.length; i++) {
- cowsl.add(cows[i]);
- }
- // find center/clusterise
- cowsl = Vec.cluster(cowsl, 2);
- Vec stddev = Vec.stddev(cowsl);
- assertTrue(stddev.magnitude() < 3);
-
- Vec mean = Vec.mean(cowsl);
- assertEquals(new Vec(5,8), mean);
-
- int stepsFromCenter = (int)Vec.max(cowsl).sub(mean).magnitude()+1;
- assertEquals(3, stepsFromCenter);
-
- Location byIA = new herd_position().getAgTarget(model, Formation.one, cowboy.getLocation(model));
- assertEquals(new Location(7,38), byIA);
-
- byIA = new herd_position().getAgTarget(model, Formation.six, cowboy.getLocation(model));
- assertEquals(new Location(6,38), byIA);
-
- // add an agent in 6,38
- model.add(WorldModel.AGENT, 6,38);
- byIA = new herd_position().getAgTarget(model, Formation.six, cowboy.getLocation(model));
- assertEquals(new Location(8,39), byIA);
-
- // add an agent in 8,40 (near 8,39)
- model.add(WorldModel.AGENT, 8,40);
- byIA = new herd_position().getAgTarget(model, Formation.six,cowboy.getLocation(model));
- assertEquals(new Location(3,38), byIA);
-
- // add an agent in 2,38 (near 3,38)
- // no good location possible, go to ags target
- model.add(WorldModel.AGENT, 2,38);
- byIA = new herd_position().getAgTarget(model, Formation.valueOf("six"), cowboy.getLocation(model));
- //assertEquals(new Location(7,38), byIA);
- }
-
- @Test
- public void moveCows3() throws Exception {
- scenario2();
- Location byIA = new herd_position().getAgTarget(model, Formation.one, cowboy.getLocation(model));
- byIA = new herd_position().nearFreeForAg(model, new Location(11,46), byIA);
- assertEquals(new Location(11,49), byIA);
- }
}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|