Revision 128:bcc20fb444e7

View differences:

src/main/java/cz/agents/dimaptools/message/ReconstructPlanMessage.java
11 11

  
12 12
	private final List<String> plan;
13 13
	private final int lastStateHash;
14
	private final String initiatorID;
15
	private final int solutionCost;
14 16

  
15 17

  
16
	public ReconstructPlanMessage(List<String> plan, int hashCode) {
18
	public ReconstructPlanMessage(List<String> plan, int hashCode, String initiatorID, int solCost) {
17 19
		this.plan = plan;
18 20
		this.lastStateHash = hashCode;
21
		this.initiatorID = initiatorID;
22
		this.solutionCost = solCost;
19 23
	}
20 24

  
21 25

  
......
36 40
		return lastStateHash;
37 41
	}
38 42
	
39
	
40
	
43

  
44
	public String getInitiatorID() {
45
		return initiatorID;
46
	}
47

  
48

  
49
	public int getSolutionCost() {
50
		return solutionCost;
51
	}
52

  
53

  
41 54
	@Override
42 55
	public String toString() {
43 56
		return "ReconstructPlanMessage [plan=" + plan + ", lastStateHash="
44
				+ lastStateHash + "]";
57
				+ lastStateHash + ", initiator="
58
						+ initiatorID + ", solutionCost="
59
								+ solutionCost + "]";
45 60
	}
46 61

  
47 62

  
src/main/java/cz/agents/dimaptools/search/DistributedAStar.java
55 55
    private final DistributedSearchProtocol protocol;
56 56

  
57 57

  
58
    private String bestReconstructedPlanBy = null;
59
    private int bestReconstructedPlanCost;
60
    
61

  
58 62
    private boolean run = true;
59 63
    private boolean search = true;
60 64
    private long timeLimitMs = Long.MAX_VALUE;
65
    
66
    
61 67

  
62 68

  
63 69
    public DistributedAStar(DIMAPWorldInterface world) {
......
81 87

  
82 88
					if(LOGGER.isDebugEnabled())LOGGER.debug(comm.getAddress() + " receive state " + Arrays.toString(sm.getValues()));
83 89

  
84
                    open.add(new SearchState(problem.initState.getDomain(),sm,sender));
90
                    addReceivedState(sm, sender);
85 91

  
86 92
	            }
87 93
			}
......
92 98

  
93 99
//            	LOGGER.info(comm.getAddress() + " receive reconstruct msg " + state.hashCode() );
94 100

  
95
            	reconstructPlan(state, rpm.getPlan());
101
            	reconstructPlan(state, rpm.getPlan(),rpm.getInitiatorID(),rpm.getSolutionCost());
96 102

  
97 103
//            	run = false;
98 104
            	search = false;
......
111 117

  
112 118
		
113 119
	}
120
	
121
	
122
	protected void addReceivedState(StateMessage sm, String sender){
123
		open.add(new SearchState(problem.initState.getDomain(),sm,sender));
124
	}
114 125

  
115 126

  
116 127

  
......
157 168
	                    	LOGGER.warn(" by me ("+problem.agent+") - " + time);
158 169

  
159 170
	                    	List<String> plan = new LinkedList<String>();
160
	                    	reconstructPlan(state, plan);
171
	                    	reconstructPlan(state, plan, problem.agent, state.getG());
161 172

  
162 173
//	                    	return;
163 174
	                    	search = false;
......
228 239
	/**
229 240
	 * Distributed reconstruction of the plan
230 241
	 * @param state
242
	 * @param cost 
243
	 * @param initiator 
231 244
	 * @param plan
232 245
	 */
233
	private void reconstructPlan(SearchState state, List<String> globalPlan){
246
	private void reconstructPlan(SearchState state, List<String> globalPlan, String initiator, int solutionCost){
234 247
//		System.out.println(comm.getAddress() + " reconstruct " + state.hashCode() + " - " + plan);
235 248
		
249
		if(bestReconstructedPlanBy == null){
250
			bestReconstructedPlanBy = initiator;
251
			bestReconstructedPlanCost = solutionCost;
252
		}else{
253
			if(bestReconstructedPlanCost < solutionCost || bestReconstructedPlanBy.compareTo(initiator) < 0){
254
				LOGGER.info(comm.getAddress() +  " plan("+bestReconstructedPlanCost+") already reconstructed by "+initiator+" stop reconstruction of plan("+solutionCost+")");
255
				return;
256
			}
257
		}
258
		
236 259
		List<String> plan = new LinkedList<String>();
237 260
		ParentState lastState = state.reconstructPlan(plan);
238 261
		
239
		planCallback.partialPlanReconstructed(plan);
262
		planCallback.partialPlanReconstructed(plan,initiator,solutionCost);
240 263
		
241 264
		plan.addAll(globalPlan);
242 265
        if(lastState.getParentActionOwner() == null){
......
252 275
        }else{
253 276
        	
254 277
//        	LOGGER.info(comm.getAddress() + " send reconstruct msg " + state.hashCode() + " to " + lastState.getParentActionOwner());
255
        	protocol.sendReconstructPlanMessage(new ReconstructPlanMessage(plan,lastState.hashCode()), lastState.getParentActionOwner());
278
        	protocol.sendReconstructPlanMessage(new ReconstructPlanMessage(plan,lastState.hashCode(),initiator,solutionCost), lastState.getParentActionOwner());
256 279
        }
257 280
	}
258 281

  
......
300 323

  
301 324
	private boolean solutionFound(SearchState state) {
302 325
        if (state.unifiesWith(problem.goalSuperState)) {
303
        	LOGGER.info("SOLUTION FOUND[" + problem.agent + "]: " + Arrays.toString(state.getValues()));
326
        	LOGGER.info("SOLUTION of cost "+state.getG()+" FOUND[" + problem.agent + "]: " + Arrays.toString(state.getValues()));
304 327

  
305 328
            LOGGER.info("OPEN-SIZE[" + problem.agent + "]" + open.size());
306 329
            LOGGER.info("CLOSED-SIZE[" + problem.agent + "]" + closed.size());
src/main/java/cz/agents/dimaptools/search/SearchInterface.java
30 30
		 * Called each time the agent reconstructs part of the plan
31 31
		 * @param plan Part of the plan reconstructed by the agent - contains only the agent's actions
32 32
		 */
33
		public void partialPlanReconstructed(List<String> plan);
33
		public void partialPlanReconstructed(List<String> plan, String initiator, int solutionCost);
34 34

  
35 35
		public void planFoundByOther();
36 36

  

Also available in: Unified diff