Revision 105:2ae44225ac0a

View differences:

src/main/java/cz/agents/dimaptools/search/MultiheuristicDistributedAStar.java
4 4
import gnu.trove.TIntObjectHashMap;
5 5

  
6 6
import java.util.ArrayList;
7
import java.util.Arrays;
7 8
import java.util.LinkedList;
8 9
import java.util.List;
9 10

  
10 11
import org.apache.log4j.Logger;
11 12

  
13
import cz.agents.alite.communication.CommunicationPerformer;
12 14
import cz.agents.alite.communication.Communicator;
13
import cz.agents.alite.communication.DefaultMessageHandler;
14
import cz.agents.alite.communication.Message;
15 15
import cz.agents.alite.communication.QueuedMessageHandler;
16
import cz.agents.alite.communication.channel.CommunicationChannelBroadcast;
17
import cz.agents.alite.communication.content.Content;
18 16
import cz.agents.alite.configurator.ConfigurationInterface;
17
import cz.agents.dimaptools.DIMAPWorldInterface;
19 18
import cz.agents.dimaptools.domain.Action;
20 19
import cz.agents.dimaptools.domain.Problem;
21 20
import cz.agents.dimaptools.experiment.DataAccumulator;
......
25 24
import cz.agents.dimaptools.message.PlanningFinishedMessage;
26 25
import cz.agents.dimaptools.message.ReconstructPlanMessage;
27 26
import cz.agents.dimaptools.message.StateMessage;
27
import cz.agents.dimaptools.protocol.DistributedSearchProtocol;
28 28

  
29 29
/**
30 30
 * Simple implementation of distributed A* (or Best-First Search depending on used heuristic) with deferred heuristic evaluation.
......
46 46

  
47 47

  
48 48
    private final Communicator comm;
49
    private final CommunicationPerformer commPerformer;
49 50
    private final TIntObjectHashMap sentStates = new TIntObjectHashMap();
50 51

  
51 52
    private int currentOpenList = 0;
......
53 54
    private boolean run = true;
54 55
    private boolean search = true;
55 56
    private long timeLimitMs = Long.MAX_VALUE;
57
    
58
    private final DistributedSearchProtocol protocol;
56 59

  
57 60

  
58
    public MultiheuristicDistributedAStar(final Communicator comm,final Problem problem) {
59
    	this(comm,problem,Long.MAX_VALUE);
61
    public MultiheuristicDistributedAStar(DIMAPWorldInterface world) {
62
    	this(world,Long.MAX_VALUE);
60 63
    }
61 64

  
62
	public MultiheuristicDistributedAStar(final Communicator comm,final Problem problem,long timeLimitMs) {
63
		this.problem = problem;
64
		this.comm = comm;
65
    public MultiheuristicDistributedAStar(DIMAPWorldInterface world,long timeLimitMs) {
66
		this.problem = world.getProblem();
67
		this.comm = world.getCommunicator();
68
		commPerformer = world.getCommPerformer();
65 69
		this.timeLimitMs = timeLimitMs;
66 70

  
67
		//message handler - handles states and plan reconstruction requests
68
		comm.addMessageHandler(searchHandler);
69
		comm.addMessageHandler(new DefaultMessageHandler() {
71
		protocol = new DistributedSearchProtocol(comm, world.getAgentName(), world.getEncoder()) {
72
			
73
			@Override
74
			public void receiveStateMessage(StateMessage sm, String sender) {
75
				if (!sender.equals(comm.getAddress())) { //TODO get rid of this expensive and stupid check!
70 76

  
71
            @Override
72
            public void handleMessage(Message message) {
73
//                if (!message.getSender().equals(comm.getAddress())) {
74
                    if(message.getContent().getData() instanceof ReconstructPlanMessage){
77
					LOGGER.info(comm.getAddress() + " receive state " + Arrays.toString(sm.getValues()));
75 78

  
79
	            	for(HeuristicOpenList open : openLists){
80
	            		SearchState newState = new SearchState(problem.initState.getDomain(),sm,sender); 
81
	            		open.add(newState,sm.isPreferred());
82
	            	}
83
	            }
84
			}
85
			
86
			@Override
87
			public void receiveReconstructPlanMessage(ReconstructPlanMessage rpm) {
88
				SearchState state = (SearchState)sentStates.get(rpm.getLastStateHash());
76 89

  
90
//            	LOGGER.info(comm.getAddress() + " receive reconstruct msg " + state.hashCode() );
77 91

  
78
                    	ReconstructPlanMessage rpm = (ReconstructPlanMessage)message.getContent().getData();
92
            	reconstructPlan(state, rpm.getPlan());
79 93

  
80
                    	SearchState state = (SearchState)sentStates.get(rpm.getLastStateHash());
94
//            	run = false;
95
            	search = false;
96
			}
97
			
98
			@Override
99
			public void receivePlanningFinishedMessage(PlanningFinishedMessage msg) {
100
				run = false;
101
				
102
			}
103
		};
81 104

  
82
//                    	System.out.println(comm.getAddress() + " receive reconstruct msg " + state.hashCode() );
83

  
84
                    	reconstructPlan(state, rpm.getPlan());
85

  
86
//                    	run = false;
87
                    	search = false;
88
                    }else if(message.getContent().getData() instanceof PlanningFinishedMessage){
89

  
90
                    	run = false;
91
                    }
92
//                }
93
            }
94

  
95
        });
105
		
96 106
	}
97 107

  
98 108

  
......
242 252
			}
243 253

  
244 254
//			LOGGER.info(comm.getAddress() + " open:"+open.size());
255
			
256
			commPerformer.performReceive();
245 257

  
246 258
			openLists.get(currentOpenList).processMessages();
247 259

  
248
//			try {
249
//				Thread.sleep(1000);
250
//			} catch (InterruptedException e) {
251
//				// TODO Auto-generated catch block
252
//				e.printStackTrace();
253
//			}
254

  
255
			while(searchHandler.hasMessage()){
256
	            final Message message = searchHandler.pullMessage();
257
	            if (!message.getSender().equals(comm.getAddress())) {
258

  
259
	            	StateMessage sm = (StateMessage)message.getContent().getData();
260
	            	
261
	            	for(HeuristicOpenList open : openLists){
262
	            		SearchState newState = new SearchState(problem.initState.getDomain(),sm,message.getSender()); 
263
	            		open.add(newState,sm.isPreferred());
264
	            	}
265
	            }
266
			}
267

  
268
//			LOGGER.info(comm.getAddress() + " open after processing msgs:"+open.size());
269

  
270
//			if(open.size() == 0){
271
//				LOGGER.warn("OPEN EMPTY!!");
272
//			}
273
			
274 260
			currentOpenList = (currentOpenList + 1) % openLists.size();
275 261

  
276 262
		}while(run);
......
285 271
//		System.out.println(comm.getAddress() + " reconstruct " + state.hashCode() + " - " + plan);
286 272
		SearchState lastState = state.reconstructPlan(plan);
287 273
        if(lastState.getParentActionOwner() == null){
288
//        	System.out.println(comm.getAddress() + " plan found " + state.hashCode() + " - " + plan);
274
        	LOGGER.info(comm.getAddress() + " plan found " + state.hashCode() + " - " + plan);
275
        	
289 276
        	planCallback.planFound(plan);
277
        	protocol.sendPlanningFinishedMessage();
290 278

  
291
        	Message message = comm.createMessage(new Content(new PlanningFinishedMessage()));
292
            message.addReceiver(CommunicationChannelBroadcast.BROADCAST_ADDRESS);
293
            comm.sendMessage(message);
294 279

  
295 280
        }else{
296
//        	System.out.println(comm.getAddress() + " send reconstruct msg " + state.hashCode() + " to " + lastState.getParentActionOwner());
297
        	Message message = comm.createMessage(new Content(new ReconstructPlanMessage(plan,lastState.hashCode())));
298
            message.addReceiver(lastState.getParentActionOwner());
299
            comm.sendMessage(message);
281
        	
282
//        	LOGGER.info(comm.getAddress() + " send reconstruct msg " + state.hashCode() + " to " + lastState.getParentActionOwner());
283
        	protocol.sendReconstructPlanMessage(new ReconstructPlanMessage(plan,lastState.hashCode()), lastState.getParentActionOwner());
300 284
        }
301 285
	}
302 286

  
......
306 290
	 */
307 291
	public void sendState(final SearchState state){
308 292

  
293
		if(sentStates.containsKey(state.hashCode()))return;
309 294
		sentStates.put(state.hashCode(), state);
310 295

  
311
		StateMessage msg = new StateMessage(state.getValues(), state.getG(), state.getHeuristic(),state.wasCreatedByPreferredAction());
312

  
296
		StateMessage msg = new StateMessage(state.getValues(), state.getG(), state.getHeuristic());
297
		
313 298
		DataAccumulator.getAccumulator().searchMessages ++;
314 299
		DataAccumulator.getAccumulator().totalBytes += msg.getBytes();
315 300

  
316
        Message message = comm.createMessage(new Content(msg));
317
        message.addReceiver(CommunicationChannelBroadcast.BROADCAST_ADDRESS);
318
        comm.sendMessage(message);
301
        protocol.sendStateMessage(msg);
319 302
    }
320 303

  
321 304
	
src/test/java/cz/agents/dimaptools/multiheuristic/TestFFHeuristicM.java
26 26

  
27 27
	@Override
28 28
	public void runSearch(DIMAPWorldInterface world){
29
		MultiheuristicDistributedAStar search = new MultiheuristicDistributedAStar(world.getCommunicator(),world.getProblem());
29
		MultiheuristicDistributedAStar search = new MultiheuristicDistributedAStar(world);
30 30

  
31 31
		HeuristicOpenList hFF = new HeuristicOpenList("hFF",true,new FFHeuristic(world.getProblem()));
32 32

  
src/test/java/cz/agents/dimaptools/multiheuristic/TestFFrdFFHeuristicM.java
2 2

  
3 3
import org.junit.Test;
4 4

  
5
import cz.agents.alite.communication.Communicator;
6 5
import cz.agents.alite.configurator.MapConfiguration;
7 6
import cz.agents.dimaptools.DIMAPWorldInterface;
8
import cz.agents.dimaptools.domain.Problem;
9 7
import cz.agents.dimaptools.relaxed.AdditiveHeuristic;
10 8
import cz.agents.dimaptools.relaxed.FFHeuristic;
11
import cz.agents.dimaptools.relaxed.RecursiveDistributedFFHeuristic;
12 9
import cz.agents.dimaptools.search.AbstractDistributedAStarTest;
13 10
import cz.agents.dimaptools.search.HeuristicOpenList;
14 11
import cz.agents.dimaptools.search.MultiheuristicDistributedAStar;
......
18 15
	@Test
19 16
	public void test() {
20 17
//		testProblem("truck-crane-a2");
21
		testProblem("logistics-a2");
22
//		testProblem("logistics-a4");
18
//		testProblem("logistics-a2");
19
		testProblem("logistics-a4");
23 20
//		testProblem("deconfliction-a4");
24 21
//		testProblem("rovers-a4");
25 22
//		testProblem("sokoban-a1");
......
28 25

  
29 26
	@Override
30 27
	public void runSearch(DIMAPWorldInterface world){
31
		MultiheuristicDistributedAStar search = new MultiheuristicDistributedAStar(world.getCommunicator(),world.getProblem());
28
		MultiheuristicDistributedAStar search = new MultiheuristicDistributedAStar(world);
32 29

  
33 30
		HeuristicOpenList hFF = new HeuristicOpenList("hFF",true,new FFHeuristic(world.getProblem()));
34 31
		
35
//		HeuristicOpenList hAdd = new HeuristicOpenList("hAdd",false,new AdditiveHeuristic(problem));
32
		HeuristicOpenList hAdd = new HeuristicOpenList("hAdd",false,new AdditiveHeuristic(world.getProblem()));
36 33
		
37
		HeuristicOpenList hrdFF = new HeuristicOpenList("hrdFF",true,new RecursiveDistributedFFHeuristic(world.getProblem(),world.getCommunicator(),false),new RecursiveDistributedFFHeuristic(world.getProblem(),world.getCommunicator(),true));
34
//		HeuristicOpenList hrdFF = new HeuristicOpenList("hrdFF",true,new RecursiveDistributedFFHeuristic(world.getProblem(),world.getCommunicator(),false),new RecursiveDistributedFFHeuristic(world.getProblem(),world.getCommunicator(),true));
38 35
		
39
//		search.plan(new MapConfiguration("hFF",hFF,"hAdd",hAdd), searchCallback);
36
		search.plan(new MapConfiguration("hFF",hFF,"hAdd",hAdd), searchCallback);
40 37
		
41 38
//		search.plan(new MapConfiguration("hFF",hFF), searchCallback);
42 39
//		search.plan(new MapConfiguration("hrdFF",hrdFF), searchCallback);
43
		search.plan(new MapConfiguration("hFF",hFF,"hrdFF",hrdFF), searchCallback);
40
//		search.plan(new MapConfiguration("hFF",hFF,"hrdFF",hrdFF), searchCallback);
44 41
	}
45 42

  
46 43
}

Also available in: Unified diff