|
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.
|
|
From: <jom...@us...> - 2008-04-28 17:39:03
|
Revision: 1276
http://jason.svn.sourceforge.net/jason/?rev=1276&view=rev
Author: jomifred
Date: 2008-04-28 10:38:52 -0700 (Mon, 28 Apr 2008)
Log Message:
-----------
jason team: new cluster alg
Modified Paths:
--------------
trunk/applications/jason-team/src/java/jia/Vec.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/jia/Vec.java
===================================================================
--- trunk/applications/jason-team/src/java/jia/Vec.java 2008-04-27 18:25:28 UTC (rev 1275)
+++ trunk/applications/jason-team/src/java/jia/Vec.java 2008-04-28 17:38:52 UTC (rev 1276)
@@ -8,7 +8,7 @@
import arch.LocalWorldModel;
-public final class Vec implements Cloneable {
+public final class Vec implements Cloneable, Comparable<Vec> {
// immutable fields (for a immutable object)
public final double x,y;
@@ -67,22 +67,7 @@
public Vec newMagnitude(double r) {
return new Vec(r*Math.cos(t), r*Math.sin(t));
}
- @Override
- public boolean equals(Object o) {
- if (o == null) return false;
- if (o == this) return true;
- if (o instanceof Vec) {
- Vec v = (Vec)o;
- return (getX() == v.getX()) && (getY() == v.getY());
- }
- return false;
- }
-
- public Object clone() {
- 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.
* 0 indicates 0 radians +- PI/8 1-7 continue CCW.
@@ -106,7 +91,34 @@
return ((int)(temp/(Math.PI/2))%4);
}
+
+ @Override
+ public boolean equals(Object o) {
+ if (o == null) return false;
+ if (o == this) return true;
+ if (o instanceof Vec) {
+ Vec v = (Vec)o;
+ return (getX() == v.getX()) && (getY() == v.getY());
+ }
+ return false;
+ }
+
+ public int compareTo(Vec o) {
+ if (r > o.r) return 1;
+ if (r < o.r) return -1;
+ return 0;
+ }
+ public Object clone() {
+ return this; // it is an immutable object, no need to create a new one
+ }
+
+
+ @Override
+ public String toString() {
+ return getX() + "," + getY();
+ }
+
//
// Useful static methods for list of vecs
//
@@ -128,6 +140,7 @@
return r;
}
+ /*
public static List<Vec> cluster(List<Vec> vs, int maxstddev) {
vs = new ArrayList<Vec>(vs); // result vectors in the cluster
Vec mean = Vec.mean(vs);
@@ -142,6 +155,7 @@
}
return vs;
}
+ */
public static Vec mean(List<Vec> vs) {
if (vs.isEmpty())
@@ -168,9 +182,4 @@
return new Vec( Math.sqrt(x), Math.sqrt(y));
}
-
- @Override
- public String toString() {
- return getX() + "," + getY();
- }
}
Modified: trunk/applications/jason-team/src/java/jia/herd_position.java
===================================================================
--- trunk/applications/jason-team/src/java/jia/herd_position.java 2008-04-27 18:25:28 UTC (rev 1275)
+++ trunk/applications/jason-team/src/java/jia/herd_position.java 2008-04-28 17:38:52 UTC (rev 1276)
@@ -12,6 +12,8 @@
import jason.environment.grid.Location;
import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Iterator;
import java.util.List;
import java.util.logging.Level;
@@ -34,7 +36,7 @@
public static final double maxStdDev = 3;
public static final int agDistanceInFormation = 4;
-
+
public enum Formation {
one { int[] getDistances() { return new int[] { 0 }; } },
two { int[] getDistances() { return new int[] { sd, -sd }; } },
@@ -47,14 +49,20 @@
private static final int sd = agDistanceInFormation/2;
};
+ LocalWorldModel model;
+
+ public void setModel(LocalWorldModel model) {
+ this.model = model;
+ }
+
@Override
public Object execute(TransitionSystem ts, Unifier un, Term[] args) throws Exception {
try {
CowboyArch arch = (CowboyArch)ts.getUserAgArch();
- LocalWorldModel model = arch.getModel();
+ model = arch.getModel();
if (model == null)
return false;
- Location agLoc = model.getAgPos(arch.getMyId());
+ Location agLoc = model.getAgPos(arch.getMyId());
// identify the formation id
Formation formation = Formation.six;
@@ -67,20 +75,20 @@
// update GUI
if (arch.hasGUI())
- setFormationLoc(model, formation);
+ setFormationLoc(formation);
// if the return is a location for one agent
if (args.length == 3) {
- Location agTarget = getAgTarget(model, formation, agLoc);
+ Location agTarget = getAgTarget(formation, agLoc);
if (agTarget != null) {
return un.unifies(args[1], new NumberTermImpl(agTarget.x)) &&
un.unifies(args[2], new NumberTermImpl(agTarget.y));
} else {
- ts.getLogger().info("No target! I am at "+agLoc+" places are "+formationPlaces(model, formation));
+ ts.getLogger().info("No target! I am at "+agLoc+" places are "+formationPlaces(formation));
}
} else {
// return all the locations for the formation
- List<Location> locs = formationPlaces(model, formation);
+ List<Location> locs = formationPlaces(formation);
if (locs != null) {
ListTerm r = new ListTermImpl();
ListTerm tail = r;
@@ -91,7 +99,7 @@
}
return un.unifies(args[1], r);
} else {
- ts.getLogger().info("No formation possible! I am at "+agLoc+" places are "+formationPlaces(model, formation));
+ ts.getLogger().info("No formation possible! I am at "+agLoc+" places are "+formationPlaces(formation));
}
}
} catch (Throwable e) {
@@ -100,9 +108,9 @@
return false;
}
- public Location getAgTarget(LocalWorldModel model, Formation formation, Location ag) throws Exception {
+ public Location getAgTarget(Formation formation, Location ag) throws Exception {
Location r = null;
- List<Location> locs = formationPlaces(model, formation);
+ List<Location> locs = formationPlaces(formation);
if (locs != null) {
for (Location l : locs) {
if (ag.equals(l) || // I am there
@@ -118,9 +126,9 @@
return r;
}
- public void setFormationLoc(LocalWorldModel model, Formation formation) throws Exception {
+ public void setFormationLoc(Formation formation) throws Exception {
model.removeAll(WorldModel.FORPLACE);
- List<Location> locs = formationPlaces(model, formation);
+ List<Location> locs = formationPlaces(formation);
if (locs != null) {
for (Location l : locs) {
if (model.inGrid(l)) {
@@ -130,7 +138,7 @@
}
}
- private List<Location> formationPlaces(LocalWorldModel model, Formation formation) throws Exception {
+ private List<Location> formationPlaces(Formation formation) throws Exception {
List<Vec> cows = new ArrayList<Vec>();
for (Location c: model.getCows()) {
cows.add(new Vec(model, c));
@@ -138,8 +146,14 @@
if (cows.isEmpty())
return null;
- cows = Vec.cluster(cows, 2); // find center/clusterise
+ //cows = Vec.cluster(cows, 2); // find center/clusterise
+ cows = cluster(cows, WorldModel.cowPerceptionRatio);
+ List<Location> clusterLocs = new ArrayList<Location>();
+ for (Vec v: cows) {
+ clusterLocs.add(v.getLocation(model));
+ }
+
Vec mean = Vec.mean(cows);
int stepsFromCenter = (int)Math.round(Vec.max(cows).sub(mean).magnitude())+1;
//Vec max = Vec.max(cows);
@@ -167,7 +181,7 @@
Location l = findFirstFreeLocTowardsTarget(agTarget, mean.add(agsTarget), initAgTS, dist, model);
//System.out.println(" = "+dist+" result "+l);
if (l != null)
- r.add(pathToNearCow(model, l));
+ r.add(pathToNearCow(l, clusterLocs));
/*
Location lastloc = null;
@@ -207,9 +221,9 @@
return l; //ref.getLocation(model); //target.add(ref).getLocation(model);
}
- private Location pathToNearCow(LocalWorldModel model, Location t) {
+ private Location pathToNearCow(Location t, List<Location> cluster) {
Location near = null;
- for (Location c: model.getCows()) {
+ for (Location c: cluster) {
if (near == null || t.maxBorder(c) < t.maxBorder(near))
near = c;
}
@@ -226,6 +240,59 @@
return t;
}
+ public static List<Vec> cluster(List<Vec> cows, int maxDist) {
+ /*
+ Vs = set of all seen cows (sorted by distance to the centre of cluster)
+ Cs = { the cow near to the center of Vs }
+
+ add = true
+ while (add)
+ add = false
+ for all v in Vs
+ if (some cow in Cs sees v)
+ move v from Vs to Cs
+ add = true
+ */
+ Vec mean = Vec.mean(cows);
+ List<Vec> vs = new ArrayList<Vec>();
+ // place all cows in ref to mean
+ for (Vec v: cows)
+ vs.add(v.sub(mean));
+
+ Collections.sort(vs);
+
+ List<Vec> cs = new ArrayList<Vec>();
+ if (!vs.isEmpty())
+ cs.add(vs.remove(0));
+
+ boolean add = true;
+ while (add) {
+ add = false;
+ Iterator<Vec> i = vs.iterator();
+ while (i.hasNext()) {
+ Vec v = i.next();
+
+ Iterator<Vec> j = cs.iterator();
+ while (j.hasNext()) {
+ Vec c = j.next();
+ if (c.sub(v).magnitude() < maxDist) {
+ cs.add(v);
+ i.remove();
+ add = true;
+ break;
+ }
+ }
+ }
+ }
+
+ List<Vec> r = new ArrayList<Vec>();
+ // place all cows in ref to 0,0
+ for (Vec v: cs)
+ r.add(v.add(mean));
+ return r;
+ }
+
+
/*
public Location nearFreeForAg(LocalWorldModel model, Location ag, Location t) throws Exception {
// run A* to get the path from ag to t
Modified: trunk/applications/jason-team/src/java/test/TestBasicHerding.java
===================================================================
--- trunk/applications/jason-team/src/java/test/TestBasicHerding.java 2008-04-27 18:25:28 UTC (rev 1275)
+++ trunk/applications/jason-team/src/java/test/TestBasicHerding.java 2008-04-28 17:38:52 UTC (rev 1276)
@@ -1,10 +1,10 @@
package test;
import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertTrue;
import jason.environment.grid.Location;
import java.util.ArrayList;
+import java.util.Collections;
import java.util.List;
import jia.Search;
@@ -70,7 +70,7 @@
}
@Test
- public void testVect() {
+ public void testVec() {
scenario1();
//assertEquals(new Vec(6,7), cow.add(cowboy));
//assertEquals(new Location(6,42), cow.add(cowboy).getLocation(model));
@@ -80,6 +80,22 @@
Vec v = new Vec(3,2);
assertEquals(v, v.newAngle(v.angle()));
}
+
+ @Test
+ public void testVecSort() {
+ scenario1();
+
+ List<Vec> cowsl = new ArrayList<Vec>();
+ for (int i=0; i<cows.length; i++) {
+ cowsl.add(cows[i]);
+ }
+ Collections.sort(cowsl);
+ assertEquals(new Vec(0,1), cowsl.get(0));
+ assertEquals(new Vec(4,8), cowsl.get(1));
+ assertEquals(new Vec(6,7), cowsl.get(2));
+ assertEquals(new Vec(5,10), cowsl.get(3));
+ assertEquals(new Vec(5,30), cowsl.get(4));
+ }
@Test
public void testCowsRepMat() throws Exception {
@@ -136,9 +152,9 @@
}
// find center/clusterise
- cowsl = Vec.cluster(cowsl, 2);
- Vec stddev = Vec.stddev(cowsl, Vec.mean(cowsl));
- assertTrue(stddev.magnitude() < 3);
+ cowsl = herd_position.cluster(cowsl, WorldModel.cowPerceptionRatio);
+ //Vec stddev = Vec.stddev(cowsl, Vec.mean(cowsl));
+ assertEquals(3, cowsl.size());
Vec mean = Vec.mean(cowsl);
assertEquals(new Vec(5,8), mean);
@@ -149,30 +165,33 @@
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));
+ herd_position hp = new herd_position();
+ hp.setModel(model);
+
+ Location byIA = hp.getAgTarget(Formation.one, cowboy.getLocation(model));
assertEquals(new Location(6,38), byIA);
- byIA = new herd_position().getAgTarget(model, Formation.six, cowboy.getLocation(model));
+ byIA = hp.getAgTarget(Formation.six, cowboy.getLocation(model));
assertEquals(new Location(6,39), byIA);
// add an agent in 6,39
model.add(WorldModel.AGENT, 6,39);
- byIA = new herd_position().getAgTarget(model, Formation.six, cowboy.getLocation(model));
+ byIA = hp.getAgTarget(Formation.six, cowboy.getLocation(model));
assertEquals(new Location(5,38), byIA);
// add an agent in 5,38
model.add(WorldModel.AGENT, 5,38);
- byIA = new herd_position().getAgTarget(model, Formation.six,cowboy.getLocation(model));
+ byIA = hp.getAgTarget(Formation.six,cowboy.getLocation(model));
assertEquals(new Location(7,42), byIA);
// add an agent in 7,42
model.add(WorldModel.AGENT, 7,42);
- byIA = new herd_position().getAgTarget(model, Formation.six,cowboy.getLocation(model));
+ byIA = hp.getAgTarget(Formation.six,cowboy.getLocation(model));
assertEquals(new Location(4,38), byIA);
// add an agent in 4,38
model.add(WorldModel.AGENT, 4,38);
- byIA = new herd_position().getAgTarget(model, Formation.six,cowboy.getLocation(model));
+ byIA = hp.getAgTarget(Formation.six,cowboy.getLocation(model));
assertEquals(null, byIA);
// add an agent in 5,37
@@ -185,7 +204,19 @@
public void moveCows3() throws Exception {
scenario2();
model.add(WorldModel.ENEMY, 11,48);
- Location byIA = new herd_position().getAgTarget(model, Formation.one, cowboy.getLocation(model));
+
+ List<Vec> cowsl = new ArrayList<Vec>();
+ for (int i=0; i<cows.length; i++) {
+ cowsl.add(cows[i]);
+ }
+
+ cowsl = herd_position.cluster(cowsl, WorldModel.cowPerceptionRatio);
+ assertEquals(9, cowsl.size());
+
+ herd_position hp = new herd_position();
+ hp.setModel(model);
+
+ Location byIA = hp.getAgTarget(Formation.one, cowboy.getLocation(model));
assertEquals(new Location(11,49), byIA);
}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|
|
From: <jom...@us...> - 2008-05-26 13:54:20
|
Revision: 1309
http://jason.svn.sourceforge.net/jason/?rev=1309&view=rev
Author: jomifred
Date: 2008-05-26 06:54:15 -0700 (Mon, 26 May 2008)
Log Message:
-----------
jason team: avoid to lead cows to enemy corral
Modified Paths:
--------------
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/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/ACProxy.java
===================================================================
--- trunk/applications/jason-team/src/java/arch/ACProxy.java 2008-05-25 18:46:25 UTC (rev 1308)
+++ trunk/applications/jason-team/src/java/arch/ACProxy.java 2008-05-26 13:54:15 UTC (rev 1309)
@@ -197,7 +197,7 @@
} else if (type.getNodeName().equals("obstacle")) {
arq.obstaclePerceived(absx, absy, CowboyArch.createCellPerception(absx, absy, CowboyArch.aOBSTACLE));
} else if (type.getNodeName().equals("corral") && type.getAttribute("type").equals("enemy")) {
- arq.obstaclePerceived(absx, absy, CowboyArch.createCellPerception(absx, absy, CowboyArch.aOBSTACLE));
+ arq.enemyCorralPerceived(absx, absy, CowboyArch.createCellPerception(absx, absy, CowboyArch.aENEMYCORRAL));
//} else if (type.getNodeName().equals("empty")) {
// percepts.add(CowboyArch.createCellPerception(cellx, celly, CowboyArch.aEMPTY));
Modified: trunk/applications/jason-team/src/java/arch/CowboyArch.java
===================================================================
--- trunk/applications/jason-team/src/java/arch/CowboyArch.java 2008-05-25 18:46:25 UTC (rev 1308)
+++ trunk/applications/jason-team/src/java/arch/CowboyArch.java 2008-05-26 13:54:15 UTC (rev 1309)
@@ -48,10 +48,11 @@
protected Logger logger = Logger.getLogger(CowboyArch.class.getName());
- public static Atom aOBSTACLE = new Atom("obstacle");
- public static Atom aENEMY = new Atom("enemy");
- public static Atom aALLY = new Atom("ally");
- public static Atom aEMPTY = new Atom("empty");
+ public static Atom aOBSTACLE = new Atom("obstacle");
+ public static Atom aENEMY = new Atom("enemy");
+ public static Atom aENEMYCORRAL = new Atom("enemycorral");
+ public static Atom aALLY = new Atom("ally");
+ public static Atom aEMPTY = new Atom("empty");
@Override
@@ -156,6 +157,17 @@
}
}
+ public void enemyCorralPerceived(int x, int y, Literal p) {
+ if (! model.hasObject(WorldModel.ENEMYCORRAL, x, y)) {
+ model.add(WorldModel.ENEMYCORRAL, x, y);
+ if (acView != null) acView.addObject(WorldModel.OBSTACLE, x, y);
+ Message m = new Message("tell", null, null, p);
+ try {
+ broadcast(m);
+ } catch (Exception e) { e.printStackTrace(); }
+ }
+ }
+
Location lo1 = new Location(-1,-1), // last locations of the agent
lo2 = new Location(-1,-1),
lo3 = new Location(-1,-1),
@@ -323,6 +335,17 @@
}
//getTS().getAg().getLogger().info("received obs="+p);
+ } else if (ms.startsWith("cell") && ms.endsWith(aENEMYCORRAL.toString()+")") && model != null) {
+ im.remove();
+
+ Literal p = (Literal)m.getPropCont();
+ int x = (int)((NumberTerm)p.getTerm(0)).solve();
+ int y = (int)((NumberTerm)p.getTerm(1)).solve();
+ if (model.inGrid(x,y)) {
+ model.add(WorldModel.ENEMYCORRAL, x, y);
+ if (acView != null) acView.addObject(WorldModel.OBSTACLE, x, y);
+ }
+
} else if (ms.startsWith("my_status") && model != null) {
im.remove();
Modified: trunk/applications/jason-team/src/java/arch/LocalWorldModel.java
===================================================================
--- trunk/applications/jason-team/src/java/arch/LocalWorldModel.java 2008-05-25 18:46:25 UTC (rev 1308)
+++ trunk/applications/jason-team/src/java/arch/LocalWorldModel.java 2008-05-26 13:54:15 UTC (rev 1309)
@@ -34,6 +34,7 @@
int[][] cowsrep; // cows repulsion
int[][] agsrep; // agents repulsion
int[][] obsrep; // obstacle repulsion
+ int[][] enemycorralrep; // repulsion from enemy corral
BeliefBase bb; // agent's BB
@@ -48,29 +49,32 @@
for (int j = 0; j < getHeight(); j++)
visited[i][j] = 0;
- cowsrep = new int[getWidth()][getHeight()];
+ cowsrep = new int[getWidth()][getHeight()];
- agsrep = new int[getWidth()][getHeight()];
+ agsrep = new int[getWidth()][getHeight()];
+ obsrep = new int[getWidth()][getHeight()];
+ enemycorralrep = new int[getWidth()][getHeight()];
for (int i = 0; i < getWidth(); i++)
- for (int j = 0; j < getHeight(); j++)
+ 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;
+ enemycorralrep[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);
+ } else if (value == WorldModel.ENEMYCORRAL) {
+ increp(enemycorralrep, x, y, 3, 3);
+ value = OBSTACLE;
}
+ super.add(value, x, y);
}
@Override
public void remove(int value, int x, int y) {
@@ -136,13 +140,21 @@
public int getObsRep(int x, int y) {
return obsrep[x][y];
}
+ public int getEnemyCorralRep(int x, int y) {
+ return enemycorralrep[x][y];
+ }
private void increp(int[][] m, int x, int y, int maxr, int value) {
- for (int r = 1; r <= maxr; r++)
+ System.out.println("in for "+x+" "+y+" "+value);
+ for (int r = 1; r <= maxr; r++) {
+ System.out.println(" "+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;
+ if (inGrid(c,l)) {
+ System.out.println(" "+c+" "+l+" +"+value);
+ m[c][l] += value;
+ }
+ }
}
// occupied means the places that can not be considered as nearFree
Modified: trunk/applications/jason-team/src/java/env/WorldModel.java
===================================================================
--- trunk/applications/jason-team/src/java/env/WorldModel.java 2008-05-25 18:46:25 UTC (rev 1308)
+++ trunk/applications/jason-team/src/java/env/WorldModel.java 2008-05-26 13:54:15 UTC (rev 1309)
@@ -18,6 +18,7 @@
public static final int ENEMY = 64;
public static final int TARGET = 128; // one agent target location
public static final int FORPLACE = 256; // a place in a formation
+ public static final int ENEMYCORRAL = 512; // a place in a formation
public static final int nbActions = 8;
Modified: trunk/applications/jason-team/src/java/jia/Search.java
===================================================================
--- trunk/applications/jason-team/src/java/jia/Search.java 2008-05-25 18:46:25 UTC (rev 1308)
+++ trunk/applications/jason-team/src/java/jia/Search.java 2008-05-26 13:54:15 UTC (rev 1309)
@@ -28,6 +28,7 @@
final boolean considerCorralAsObstacles;
final boolean considerCowsAsObstacles;
final boolean considerRepulsionForCows;
+ final boolean considerEnemyCorralRepulsion;
int maxDistFromCluster;
WorldModel.Move[] actionsOrder;
@@ -52,6 +53,7 @@
boolean considerCorralAsObstacles,
boolean considerCowsAsObstacles,
boolean considerRepulsionForCows,
+ boolean considerEnemyCorralRepulsion,
AgArch agArch) {
this.model = m;
@@ -61,6 +63,7 @@
this.considerCorralAsObstacles = considerCorralAsObstacles;
this.considerCowsAsObstacles = considerCowsAsObstacles;
this.considerRepulsionForCows = considerRepulsionForCows;
+ this.considerEnemyCorralRepulsion = considerEnemyCorralRepulsion;
this.agArch = agArch;
if (actions != null)
this.actionsOrder = actions;
@@ -78,7 +81,7 @@
/** used normally to discover the distance from 'from' to 'to' (or if there is path to) */
Search(LocalWorldModel m, Location from, Location to, AgArch agArch) {
- this(m,from,to,null,false, false, false, false, agArch);
+ this(m,from,to,null,false, false, false, false, false, agArch);
}
public Nodo search() throws Exception {
@@ -176,17 +179,25 @@
public int custo() {
if (isRoot)
return 0;
+
+ int c = 1;
+
if (ia.considerCowsAsObstacles)
- return ia.model.getCowsRep(pos.x, pos.y)+1;
+ c += ia.model.getCowsRep(pos.x, pos.y);
if (ia.considerRepulsionForCows) {
// consider the cost of agents only if they are near
- int c = ia.model.getObsRep(pos.x, pos.y) + 1;
+ c += ia.model.getObsRep(pos.x, pos.y);
if (ia.from.maxBorder(pos) <= ia.maxDistFromCluster)
c += ia.model.getAgsRep(pos.x, pos.y);
- return c;
}
- return 1;
+
+ if (ia.considerEnemyCorralRepulsion) {
+ System.out.println("using "+ia.model.getEnemyCorralRep(pos.x, pos.y)+" for "+pos);
+ c += ia.model.getEnemyCorralRep(pos.x, pos.y);
+ }
+
+ return c;
}
public boolean ehMeta() {
Modified: trunk/applications/jason-team/src/java/jia/direction.java
===================================================================
--- trunk/applications/jason-team/src/java/jia/direction.java 2008-05-25 18:46:25 UTC (rev 1308)
+++ trunk/applications/jason-team/src/java/jia/direction.java 2008-05-26 13:54:15 UTC (rev 1309)
@@ -61,7 +61,7 @@
actionsOrder[i2] = actionsOrder[i1];
actionsOrder[i1] = temp;
- Search astar = new Search(model, from, to, actionsOrder, true, false, true, false, arch);
+ Search astar = new Search(model, from, to, actionsOrder, true, false, true, false, false, arch);
Nodo solution = astar.search();
if (solution == null) {
@@ -79,7 +79,7 @@
}
// run A* again
- astar = new Search(model, from, to, actionsOrder, true, false, true, false, arch);
+ astar = new Search(model, from, to, actionsOrder, true, false, true, false, false, arch);
solution = astar.search();
}
Modified: trunk/applications/jason-team/src/java/jia/herd_position.java
===================================================================
--- trunk/applications/jason-team/src/java/jia/herd_position.java 2008-05-25 18:46:25 UTC (rev 1308)
+++ trunk/applications/jason-team/src/java/jia/herd_position.java 2008-05-26 13:54:15 UTC (rev 1309)
@@ -159,7 +159,7 @@
//Vec max = Vec.max(cows);
// run A* to see the cluster target in n steps
- Search s = new Search(model, mean.getLocation(model), model.getCorralCenter(), null, false, false, false, true, null);
+ Search s = new Search(model, mean.getLocation(model), model.getCorralCenter(), null, false, false, false, true, true, null);
s.setMaxDistFromCluster(stepsFromCenter+Search.DIST_FOR_AG_OBSTACLE);
List<Nodo> np = s.normalPath(s.search());
int n = Math.min(stepsFromCenter, np.size());
Modified: trunk/applications/jason-team/src/java/test/TestBasicHerding.java
===================================================================
--- trunk/applications/jason-team/src/java/test/TestBasicHerding.java 2008-05-25 18:46:25 UTC (rev 1308)
+++ trunk/applications/jason-team/src/java/test/TestBasicHerding.java 2008-05-26 13:54:15 UTC (rev 1309)
@@ -167,12 +167,24 @@
@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);
+ Search s = new Search(model, cowboy.getLocation(model), new Location(8,37), null, true, true, true, false, false, null);
Nodo path = s.search();
assertEquals(15, s.normalPath(path).size());
}
@Test
+ public void testAStarPathInEnemyCorral() throws Exception {
+ scenario3();
+ model.add(WorldModel.ENEMYCORRAL, 10, 7);
+ model.add(WorldModel.ENEMYCORRAL, 11, 7);
+ model.add(WorldModel.ENEMYCORRAL, 12, 7);
+ Search s = new Search(model,new Location(12,12), new Location(10,1), null, true, false, true, false, true, null);
+ Nodo path = s.search();
+ System.out.println(s.normalPath(path));
+ //assertEquals(15, s.normalPath(path).size());
+ }
+
+ @Test
public void moveCows2() throws Exception {
scenario1();
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|