Revision 189:a3cebf5802f4

View differences:

src/main/java/cz/agents/dimaptools/heuristic/relaxed/LocalHeuristicRequest.java
2 2

  
3 3
import cz.agents.dimaptools.heuristic.HeuristicInterface.HeuristicComputedCallback;
4 4
import cz.agents.dimaptools.model.State;
5
import cz.agents.dimaptools.search.SearchState;
5 6

  
6
public class LocalHeuristicRequest {
7
public class LocalHeuristicRequest implements Comparable<LocalHeuristicRequest> {
7 8

  
8
	public final State state;
9
	public final SearchState state;
9 10
	public final HeuristicComputedCallback callback;
10 11

  
11 12
	public LocalHeuristicRequest(State state, HeuristicComputedCallback callback) {
12 13
		super();
13
		this.state = state;
14
		this.state = (SearchState)state;	//TODO: no no no!
14 15
		this.callback = callback;
15 16
	}
16 17

  
18
	@Override
19
	public int compareTo(LocalHeuristicRequest o) {
20
		return state.compareTo(o.state);
21
	}
22

  
17 23

  
18 24

  
19 25
}
src/main/java/cz/agents/dimaptools/heuristic/relaxed/RecursiveDistributedBFRelaxationRequestHeuristic.java
1
package cz.agents.dimaptools.heuristic.relaxed;
2

  
3
import java.util.PriorityQueue;
4

  
5
import cz.agents.dimaptools.DIMAPWorldInterface;
6
import cz.agents.dimaptools.heuristic.HeuristicInterface;
7
import cz.agents.dimaptools.heuristic.HeuristicResult;
8
import cz.agents.dimaptools.heuristic.relaxed.evaluator.EvaluatorInterface;
9
import cz.agents.dimaptools.model.State;
10

  
11
public class RecursiveDistributedBFRelaxationRequestHeuristic extends RecursiveDistributedRelaxationRequestHeuristic {
12

  
13
    public RecursiveDistributedBFRelaxationRequestHeuristic(
14
			DIMAPWorldInterface world, EvaluatorInterface evaluator,
15
			int maxRecursionDepth) {
16
		super(world, evaluator, maxRecursionDepth);
17
		// TODO Auto-generated constructor stub
18
	}
19

  
20

  
21

  
22
    
23

  
24
    /**
25
     * Queue requests from the search which cannot be computed at the moment
26
     */
27
    private PriorityQueue<LocalHeuristicRequest> localRequests = new PriorityQueue<LocalHeuristicRequest>();
28

  
29

  
30

  
31
    @Override
32
    public void getHeuristic(State state, HeuristicComputedCallback callback) {
33
        
34
//		LOGGER.info(domain.agent + "("+id+")" + "Get H(" + (heuristicCounter ++) + "): requestHandler.queueSize():"+requestHandler.queueSize()+", replyHandler.queueSize():"+replyHandler.queueSize()+", requests.size():"+requests.size());
35

  
36
        //if waiting for some replies, queue the local request
37
        if(requests.size() > 0){
38
            localRequests.add(new LocalHeuristicRequest(state, callback));
39
//			LOGGER.info(agentName + "("+id+") localRequests: " + localRequests.size());
40
            return;
41
        }
42

  
43
        //build the EQ
44
//        LOGGER.info(agentName + "("+id+") compute h for: " + state.hashCode());
45

  
46
        currentCallback = callback;
47
        currentState = state;
48
        requestedActions.clear();
49

  
50
        buildGoalPropositions(problem.goalSuperState);
51
        setupExplorationQueue();
52
        setupExplorationQueueState(state);
53
        relaxedExploration();
54

  
55
        //if not waiting for replies, finish the heuristic
56
        if(requests.size() == 0){
57
//			LOGGER.info(domain.agent + "("+id+")" + " Computed H(" + (heuristicCounter --) + "): requestHandler.queueSize():"+requestHandler.queueSize()+", replyHandler.queueSize():"+replyHandler.queueSize()+", requests.size():"+requests.size());
58
            int totalCost = evaluator.getTotalCost(goalPropositions);
59
            HelpfulActions ha = totalCost < HeuristicInterface.LARGE_HEURISTIC ? evaluator.getHelpfulActions(state) : new HelpfulActions();
60
            callback.heuristicComputed(new HeuristicResult(totalCost,ha));
61
        }
62
    }
63

  
64

  
65

  
66
    /**
67
     * This method used to process messages, but now is used only to periodically check
68
     * if there are any local requests to process
69
     */
70
    @Override
71
    public void processMessages() {
72

  
73
        while(requests.size() == 0 && localRequests.size() > 0){
74
            LocalHeuristicRequest lr = localRequests.poll();
75
            
76
            getHeuristic(lr.state, lr.callback);
77
        }
78

  
79
    }
80

  
81

  
82

  
83

  
84
}
src/main/java/cz/agents/dimaptools/heuristic/relaxed/RecursiveDistributedRelaxationRequestHeuristic.java
37 37
     * Sent requests waiting for the reply
38 38
     */
39 39
    protected TIntObjectHashMap requests = new TIntObjectHashMap();
40
    private HeuristicComputedCallback currentCallback;
40
    protected HeuristicComputedCallback currentCallback;
41 41

  
42 42
    /**
43 43
     * Queue requests from the search which cannot be computed at the moment
src/test/java/cz/agents/dimaptools/relaxed/TestRecursiveDistributedBFHeuristic.java
1
package cz.agents.dimaptools.relaxed;
2

  
3
import org.junit.Test;
4

  
5
import cz.agents.alite.configurator.MapConfiguration;
6
import cz.agents.dimaptools.DIMAPWorldInterface;
7
import cz.agents.dimaptools.heuristic.relaxed.RecursiveDistributedBFRelaxationRequestHeuristic;
8
import cz.agents.dimaptools.heuristic.relaxed.RecursiveDistributedRelaxationReplyHeuristic;
9
import cz.agents.dimaptools.heuristic.relaxed.RecursiveDistributedRelaxationRequestHeuristic;
10
import cz.agents.dimaptools.heuristic.relaxed.evaluator.AddEvaluator;
11
import cz.agents.dimaptools.search.AbstractDistributedAStarTest;
12
import cz.agents.dimaptools.search.DistributedBestFirstSearch;
13

  
14
public class TestRecursiveDistributedBFHeuristic extends AbstractDistributedAStarTest {
15

  
16
	@Test
17
	public void test() {
18
		testProblem("truck-crane-a2");
19
//		testProblem("truck-crane-factory-a3");
20
//		testProblem("logistics-a2");
21
//		testProblem("logistics-a4");
22
//		testProblem("deconfliction-a4");
23
//		testProblem("rovers-a4");
24
//		testProblem("sokoban-a1");
25
//		testProblem("sokoban-a2");
26
	}
27

  
28
	@Override
29
	public void runSearch(DIMAPWorldInterface world){
30
		DistributedBestFirstSearch search = new DistributedBestFirstSearch(world);
31
//		AStar search = new AStar(problem);
32

  
33
		RecursiveDistributedRelaxationRequestHeuristic req = new RecursiveDistributedBFRelaxationRequestHeuristic(world, new AddEvaluator(world.getProblem()),60);
34
		RecursiveDistributedRelaxationReplyHeuristic rep = new RecursiveDistributedRelaxationReplyHeuristic(world, new AddEvaluator(world.getProblem()),req.getRequestProtocol());
35
		req.setReplyProtocol(rep.getReplyProtocol());
36
		
37

  
38
		search.plan(new MapConfiguration("heuristic",req,"requestHeuristic",rep), searchCallback);
39
	}
40

  
41
}

Also available in: Unified diff