Revision 106:40f5f040c85b

View differences:

src/main/java/cz/agents/dimaptools/relaxed/RecursiveDistributedAdditiveHeuristic.java
4 4

  
5 5
import org.apache.log4j.Logger;
6 6

  
7
import cz.agents.alite.communication.Communicator;
8
import cz.agents.dimaptools.domain.Problem;
7
import cz.agents.dimaptools.DIMAPWorldInterface;
9 8

  
10 9
public class RecursiveDistributedAdditiveHeuristic extends RecursiveDistributedRelaxationHeuristic {
11 10

  
12 11
	private final Logger LOGGER;
13 12

  
14
	public RecursiveDistributedAdditiveHeuristic(Problem problem, Communicator comm,boolean receiveRequests) {
15
		super(comm, problem,receiveRequests,Integer.MAX_VALUE);
13
	public RecursiveDistributedAdditiveHeuristic(DIMAPWorldInterface world,boolean receiveRequests) {
14
		super(world,receiveRequests,Integer.MAX_VALUE);
16 15

  
17 16
		LOGGER = Logger.getLogger(problem.agent + "." + RelaxationHeuristic.class);
18 17
	}
19 18

  
20
	public RecursiveDistributedAdditiveHeuristic(Problem problem, Communicator comm,boolean receiveRequests,int maxRecursionDepth) {
21
		super(comm, problem,receiveRequests,maxRecursionDepth);
19
	public RecursiveDistributedAdditiveHeuristic(DIMAPWorldInterface world,boolean receiveRequests,int maxRecursionDepth) {
20
		super(world,receiveRequests,maxRecursionDepth);
22 21

  
23 22
		LOGGER = Logger.getLogger(problem.agent + "." + RelaxationHeuristic.class);
24 23
	}
src/main/java/cz/agents/dimaptools/relaxed/RecursiveDistributedFFHeuristic.java
4 4

  
5 5
import org.apache.log4j.Logger;
6 6

  
7
import cz.agents.alite.communication.Communicator;
8
import cz.agents.dimaptools.domain.Problem;
7
import cz.agents.dimaptools.DIMAPWorldInterface;
9 8
import cz.agents.dimaptools.domain.State;
10 9
import cz.agents.dimaptools.heuristic.HelpfulActions;
11 10
import cz.agents.dimaptools.heuristic.RelaxedPlan;
......
16 15
	
17 16
	private RelaxedPlan rp;
18 17

  
19
	public RecursiveDistributedFFHeuristic(Problem problem, Communicator comm,boolean receiveRequests) {
20
		super(comm, problem,receiveRequests,Integer.MAX_VALUE);
18
	public RecursiveDistributedFFHeuristic(DIMAPWorldInterface world,boolean receiveRequests) {
19
		super(world,receiveRequests,Integer.MAX_VALUE);
21 20

  
22 21
		LOGGER = Logger.getLogger(problem.agent + "." + RelaxationHeuristic.class);
23 22
	}
24 23

  
25
	public RecursiveDistributedFFHeuristic(Problem problem, Communicator comm,boolean receiveRequests,int maxRecursionDepth) {
26
		super(comm, problem,receiveRequests,maxRecursionDepth);
24
	public RecursiveDistributedFFHeuristic(DIMAPWorldInterface world,boolean receiveRequests,int maxRecursionDepth) {
25
		super(world,receiveRequests,maxRecursionDepth);
27 26

  
28 27
		LOGGER = Logger.getLogger(problem.agent + "." + RelaxationHeuristic.class);
29 28
	}
src/main/java/cz/agents/dimaptools/relaxed/RecursiveDistributedMaxHeuristic.java
4 4

  
5 5
import org.apache.log4j.Logger;
6 6

  
7
import cz.agents.alite.communication.Communicator;
8
import cz.agents.dimaptools.domain.Problem;
7
import cz.agents.dimaptools.DIMAPWorldInterface;
9 8

  
10 9
public class RecursiveDistributedMaxHeuristic extends RecursiveDistributedRelaxationHeuristic {
11 10

  
12 11
	private final Logger LOGGER;
13 12

  
14
	public RecursiveDistributedMaxHeuristic(Problem problem, Communicator comm,boolean receiveRequests) {
15
		super(comm, problem,receiveRequests,Integer.MAX_VALUE);
13
	public RecursiveDistributedMaxHeuristic(DIMAPWorldInterface world,boolean receiveRequests) {
14
		super(world,receiveRequests,Integer.MAX_VALUE);
16 15

  
17 16
		LOGGER = Logger.getLogger(problem.agent + "." + RelaxationHeuristic.class);
18 17
	}
19 18

  
20
	public RecursiveDistributedMaxHeuristic(Problem problem, Communicator comm,boolean receiveRequests,int maxRecursionDepth) {
21
		super(comm, problem,receiveRequests,maxRecursionDepth);
19
	public RecursiveDistributedMaxHeuristic(DIMAPWorldInterface world,boolean receiveRequests,int maxRecursionDepth) {
20
		super(world,receiveRequests,maxRecursionDepth);
22 21

  
23 22
		LOGGER = Logger.getLogger(problem.agent + "." + RelaxationHeuristic.class);
24 23
	}
src/main/java/cz/agents/dimaptools/relaxed/RecursiveDistributedRelaxationHeuristic.java
3 3
import gnu.trove.TIntHashSet;
4 4
import gnu.trove.TIntObjectHashMap;
5 5

  
6
import java.util.Arrays;
7 6
import java.util.HashSet;
8 7
import java.util.LinkedList;
9 8
import java.util.Set;
......
11 10
import org.apache.log4j.Logger;
12 11

  
13 12
import cz.agents.alite.communication.Communicator;
14
import cz.agents.alite.communication.Message;
15
import cz.agents.alite.communication.QueuedMessageHandler;
16
import cz.agents.alite.communication.content.Content;
13
import cz.agents.dimaptools.DIMAPWorldInterface;
17 14
import cz.agents.dimaptools.domain.Action;
18
import cz.agents.dimaptools.domain.Problem;
19 15
import cz.agents.dimaptools.domain.State;
20 16
import cz.agents.dimaptools.experiment.DataAccumulator;
21 17
import cz.agents.dimaptools.heuristic.HeuristicResult;
22 18
import cz.agents.dimaptools.message.HeuristicReplyWithPublicActionsMessage;
23 19
import cz.agents.dimaptools.message.HeuristicRequestMessage;
20
import cz.agents.dimaptools.protocol.DistributedHeuristicProtocol;
24 21

  
25 22
public abstract class RecursiveDistributedRelaxationHeuristic extends RelaxationHeuristic {
26 23

  
27 24
	private final Logger LOGGER;
25
	
26
	private final DistributedHeuristicProtocol protocol;
28 27

  
29 28
	private final Communicator comm;
30
	private QueuedMessageHandler requestHandler = new QueuedMessageHandler(HeuristicRequestMessage.class);
31
    private QueuedMessageHandler replyHandler = new QueuedMessageHandler(HeuristicReplyWithPublicActionsMessage.class);
32

  
29
	
33 30
    private TIntObjectHashMap requests = new TIntObjectHashMap();
34 31
    private HeuristicComputedCallback currentCallback;
35 32

  
......
45 42
	private TIntHashSet requestedActions = new TIntHashSet();
46 43

  
47 44

  
48
	public RecursiveDistributedRelaxationHeuristic(Communicator comm, Problem problem,boolean receiveRequests) {
49
		this(comm,problem,receiveRequests,Integer.MAX_VALUE);
45
	public RecursiveDistributedRelaxationHeuristic(DIMAPWorldInterface world,boolean receiveRequests) {
46
		this(world,receiveRequests,Integer.MAX_VALUE);
50 47
	}
51 48

  
52
	public RecursiveDistributedRelaxationHeuristic(Communicator comm, Problem problem,boolean receiveRequests,int maxRecursionDepth) {
53
		super(problem,true);
49
	public RecursiveDistributedRelaxationHeuristic(DIMAPWorldInterface world, boolean receiveRequests,int maxRecursionDepth) {
50
		super(world.getProblem(),true);
51
		
52
		comm = world.getCommunicator();
53
		this.maxRecursionDepth = maxRecursionDepth;
54 54

  
55 55
		LOGGER = Logger.getLogger(problem.agent + "." + RelaxationHeuristic.class);
56 56

  
57
		this.comm = comm;
58
		this.maxRecursionDepth = maxRecursionDepth;
57
		protocol = new DistributedHeuristicProtocol(
58
				world.getCommunicator(),
59
				world.getAgentName(),
60
				world.getEncoder(),
61
				receiveRequests){
62

  
63
			@Override
64
			public void receiveHeuristicRequestMessage(HeuristicRequestMessage req, String sender) {
65
				if(LOGGER.isDebugEnabled())LOGGER.debug(protocol.getAddress() + "("+id+")" + " handle request from " + sender + ": " + req.humanize(problem.getDomain()));
66
            	processRequest(req,sender);
67
			}
68

  
69
			@Override
70
			public void receiveHeuristicReplyWithPublicActionsMessage(HeuristicReplyWithPublicActionsMessage re, String sender) {
71
            	if(LOGGER.isDebugEnabled())LOGGER.debug(protocol.getAddress() + "("+id+")" + " handle reply from " + sender + ": " + re);
72
            	processReply(re,sender);
73
			}
74
			
75
		};
59 76

  
60 77
		if(receiveRequests){
61 78
			id = "receive";
62
			comm.addMessageHandler(requestHandler);
79
//			comm.addMessageHandler(requestHandler);
63 80
		}else{
64 81
			id = "send";
65
			comm.addMessageHandler(replyHandler);
82
//			comm.addMessageHandler(replyHandler);
66 83
		}
67 84

  
68 85
		sendRequests = !receiveRequests;
......
212 229
		DataAccumulator.getAccumulator().heuristicRequestMessages ++;
213 230
		DataAccumulator.getAccumulator().totalBytes += reqm.getBytes();
214 231

  
215
		Message message = comm.createMessage(new Content(reqm));
216
        message.addReceiver(agent);
217
        comm.sendMessage(message);
232
		protocol.sendHeuristicRequestMessage(reqm, agent);
218 233
	}
219 234

  
220 235

  
......
223 238

  
224 239
	@Override
225 240
	public void processMessages() {
226
		if(!initialized){
227
			LOGGER.warn("NOT INITIALIZED YET!");
228
		}
229

  
230

  
231

  
232
//		LOGGER.info(domain.agent + "("+id+")" + " requestHandler.queueSize():"+requestHandler.queueSize()+", replyHandler.queueSize():"+replyHandler.queueSize()+", requests.size():"+requests.size());
233
		boolean log = requestHandler.queueSize() > 0 || replyHandler.queueSize() > 0;
234

  
235
		if(log)if(LOGGER.isDebugEnabled())LOGGER.debug(comm.getAddress() + "("+id+")" + " process messages " +  requestHandler.queueSize() + "/" + replyHandler.queueSize() + " ...");
236

  
237

  
238

  
239
		while(requestHandler.hasMessage()){
240
            final Message message = requestHandler.pullMessage();
241

  
242
            	HeuristicRequestMessage req = (HeuristicRequestMessage)message.getContent().getData();
243

  
244
            	if(LOGGER.isDebugEnabled())LOGGER.debug(comm.getAddress() + "("+id+")" + " handle request from " + message.getSender() + ": " + req.humanize(problem.getDomain()));
245

  
246
            	processRequest(req,message.getSender());
247

  
248

  
249
		}
250

  
251
		while(replyHandler.hasMessage()){
252
            final Message message = replyHandler.pullMessage();
253

  
254
            	HeuristicReplyWithPublicActionsMessage re = (HeuristicReplyWithPublicActionsMessage)message.getContent().getData();
255

  
256
            	if(LOGGER.isDebugEnabled())LOGGER.debug(comm.getAddress() + "("+id+")" + " handle reply from " + message.getSender() + ": " + re);
257

  
258
            	processReply(re,message.getSender());
259

  
260
		}
261

  
262

  
263

  
264
//		LOGGER.info(domain.agent + "("+id+")" + "DONE: requestHandler.queueSize():"+requestHandler.queueSize()+", replyHandler.queueSize():"+replyHandler.queueSize()+", requests.size():"+requests.size());
265

  
266
		if(log)if(LOGGER.isDebugEnabled())LOGGER.debug(comm.getAddress() + "("+id+")" + " process messages done");
267

  
241
		
268 242
		while(requests.size() == 0 && localRequests.size() > 0){
269 243
			LocalHeuristicRequest lr = localRequests.pollLast();
270 244
//			LOGGER.info(comm.getAddress() + "("+id+") localRequests: " + localRequests.size());
......
320 294

  
321 295
//		LOGGER.info(domain.agent + "("+id+")" + " sendReply("+re.getRequestHash() +")");
322 296

  
323
		Message message = comm.createMessage(new Content(re));
324
        message.addReceiver(agent);
325
        comm.sendMessage(message);
297
		protocol.sendHeuristicReplyWithPublicActionsMessage(re,agent);
326 298
	}
327 299

  
328 300
	public void processReply(final HeuristicReplyWithPublicActionsMessage re, String sender){
src/test/java/cz/agents/dimaptools/relaxed/TestRecursiveDistributedAdditiveHeuristic.java
18 18
//		testProblem("truck-crane-factory-a3");
19 19
//		testProblem("logistics-a2");
20 20
//		testProblem("logistics-a4");
21
//		testProblem("deconfliction-a4");
21
		testProblem("deconfliction-a4");
22 22
//		testProblem("rovers-a4");
23 23
//		testProblem("sokoban-a1");
24 24
//		testProblem("sokoban-a2");
......
29 29
		DistributedAStar search = new DistributedAStar(world);
30 30
//		AStar search = new AStar(problem);
31 31

  
32
		HeuristicInterface heuristic = new RecursiveDistributedAdditiveHeuristic(world.getProblem(),world.getCommunicator(),false);
33
		HeuristicInterface reqHeuristic = new RecursiveDistributedAdditiveHeuristic(world.getProblem(),world.getCommunicator(),true);
32
		HeuristicInterface heuristic = new RecursiveDistributedAdditiveHeuristic(world,false);
33
		HeuristicInterface reqHeuristic = new RecursiveDistributedAdditiveHeuristic(world,true);
34 34

  
35 35
		search.plan(new MapConfiguration("heuristic",heuristic,"requestHeuristic",reqHeuristic), searchCallback);
36 36
	}
src/test/java/cz/agents/dimaptools/relaxed/TestRecursiveDistributedFFHeuristic.java
17 17
//		testProblem("truck-crane-a2");
18 18
//		testProblem("truck-crane-factory-a3");
19 19
//		testProblem("logistics-a2");
20
//		testProblem("logistics-a4");
20
		testProblem("logistics-a4");
21 21
//		testProblem("deconfliction-a4");
22 22
//		testProblem("rovers-a4");
23 23
//		testProblem("sokoban-a1");
......
29 29
		DistributedAStar search = new DistributedAStar(world);
30 30
//		AStar search = new AStar(problem);
31 31

  
32
		HeuristicInterface heuristic = new RecursiveDistributedFFHeuristic(world.getProblem(),world.getCommunicator(),false);
33
		HeuristicInterface reqHeuristic = new RecursiveDistributedFFHeuristic(world.getProblem(),world.getCommunicator(),true);
32
		HeuristicInterface heuristic = new RecursiveDistributedFFHeuristic(world,false);
33
		HeuristicInterface reqHeuristic = new RecursiveDistributedFFHeuristic(world,true);
34 34

  
35 35
		search.plan(new MapConfiguration("heuristic",heuristic,"requestHeuristic",reqHeuristic), searchCallback);
36 36
	}
src/test/java/cz/agents/dimaptools/relaxed/TestRecursiveDistributedMaxHeuristic.java
29 29
		DistributedAStar search = new DistributedAStar(world);
30 30
//		AStar search = new AStar(problem);
31 31

  
32
		HeuristicInterface heuristic = new RecursiveDistributedMaxHeuristic(world.getProblem(),world.getCommunicator(),false,1);
33
		HeuristicInterface reqHeuristic = new RecursiveDistributedMaxHeuristic(world.getProblem(),world.getCommunicator(),true,1);
32
		HeuristicInterface heuristic = new RecursiveDistributedMaxHeuristic(world,false,1);
33
		HeuristicInterface reqHeuristic = new RecursiveDistributedMaxHeuristic(world,true,1);
34 34

  
35 35
		search.plan(new MapConfiguration("heuristic",heuristic,"requestHeuristic",reqHeuristic), searchCallback);
36 36
	}

Also available in: Unified diff