Revision 186:0fd0585fa7fd

View differences:

src/main/java/cz/agents/dimaptools/heuristic/relaxed/RecursiveDistributedRelaxationRestrictedReplyHeuristic.java
1
package cz.agents.dimaptools.heuristic.relaxed;
2

  
3
import gnu.trove.TIntHashSet;
4
import cz.agents.dimaptools.DIMAPWorldInterface;
5
import cz.agents.dimaptools.communication.protocol.DistributedHeuristicRequestProtocol;
6
import cz.agents.dimaptools.heuristic.relaxed.evaluator.EvaluatorInterface;
7

  
8
public class RecursiveDistributedRelaxationRestrictedReplyHeuristic extends RecursiveDistributedRelaxationReplyHeuristic {
9

  
10
//	private final Logger LOGGER = Logger.getLogger(RecursiveDistributedRelaxationRestrictedReplyHeuristic.class);
11

  
12

  
13
	
14
    public RecursiveDistributedRelaxationRestrictedReplyHeuristic(DIMAPWorldInterface world, EvaluatorInterface evaluator,DistributedHeuristicRequestProtocol requestProtocol) {
15
		super(world, evaluator, requestProtocol);
16
	}
17

  
18

  
19

  
20
    /**
21
     * Determine which projected actions were used to achieve a proposition (i.e. public RP)
22
     * @param goal
23
     * @param publicRP
24
     */
25
    public void markPublic(Proposition goal, TIntHashSet publicRP){
26
        if(!goal.markedPub){
27
            goal.markedPub = true;
28
            UnaryOperator op = goal.reachedBy;
29
            if(op!=null){
30
                for(Proposition p : op.precondition){
31
                    markPublic(p,publicRP);
32
                }
33

  
34
                if(op.cost==op.baseCost && problem.getAction(op.actionHash).isProjection()){
35
                    publicRP.add(op.operatorsIndex);
36
                }
37

  
38
            }
39
        }
40

  
41
    }
42

  
43

  
44

  
45

  
46
}
src/main/java/cz/agents/dimaptools/heuristic/relaxed/RecursiveDistributedRelaxationRestrictedRequestHeuristic.java
1
package cz.agents.dimaptools.heuristic.relaxed;
2

  
3
import org.apache.log4j.Logger;
4

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

  
11
public class RecursiveDistributedRelaxationRestrictedRequestHeuristic extends RecursiveDistributedRelaxationRequestHeuristic {
12

  
13
    
14
	private final Logger LOGGER = Logger.getLogger(RecursiveDistributedRelaxationRestrictedRequestHeuristic.class);
15

  
16

  
17

  
18

  
19

  
20

  
21

  
22
	public RecursiveDistributedRelaxationRestrictedRequestHeuristic(
23
			DIMAPWorldInterface world, EvaluatorInterface evaluator,int maxRecursionDepth) {
24
		super(world, evaluator, maxRecursionDepth);
25
	}
26

  
27

  
28

  
29

  
30

  
31

  
32

  
33
	
34

  
35

  
36

  
37

  
38

  
39

  
40
    /**
41
     * Override the default enqueue operation
42
     * @param p
43
     * @param cost
44
     * @param op
45
     **/
46
    public void enqueueIfNecessary(final Proposition p, int cost, final UnaryOperator op){
47
//		if(LOGGER.isDebugEnabled())LOGGER.debug(domain.agent + "("+id+")" + " enqueue if necessary ");
48

  
49

  
50
        //XXX: recursion depth is effectively never checked
51
        //final int recursionDepth = 0;
52

  
53
//		if(op != null)LOGGER.info(domain.agent + " ENQUEUE " + op.operatorsIndex + ", action:" + problem.getAction(op.actionHash));
54

  
55
        if(cost >= HeuristicInterface.LARGE_HEURISTIC){
56
            LOGGER.warn("IMPOSSIBLE!");
57
        }
58
        
59
        if(p.cost == -1 || p.cost > cost){
60

  
61
            //if operator is owned by other agent (maybe isProjection check should suffice)
62
            if(
63
            		op != null && 					//i.e. initial state
64
            		op.shouldRequest && 			//is projection and not pure
65
            		maxRecursionDepth > 0 &&		//should send any requests at all
66
            		cost==op.baseCost				//operator is in the first RPG layer
67
            		){
68
                if(LOGGER.isDebugEnabled())LOGGER.debug(domain.agent + "("+id+")" + " enqueue public operator: " + problem.getAction(op.actionHash));
69

  
70
                final RelaxationHeuristicRequest req = new RelaxationHeuristicRequest(agentID, new HeuristicComputedCallback() {
71

  
72
                    @Override
73
                    public void heuristicComputed(HeuristicResult result) {
74

  
75
                        //the request and all its sub-requests are finished - add the proposition to the EQ
76
                        if(result.getValue() < LARGE_HEURISTIC){
77
                            p.distance = result.getValue();
78
                            p.cost = result.getValue();
79
                            p.reachedBy = op;
80
                            explorationQueue.add(p);
81
                        }
82

  
83
                        //finish EQ with the new proposition
84
                        finishExplorationQueue();
85
                    }
86

  
87
                });
88

  
89
                req.waitForReply();
90
                Trace.it("increase", "'" + agentName, null, id, req.hashCode(), 1, currentState.hashCode(), req.waitingFor());
91

  
92
                sendRequest(op.operatorsIndex,cost,req,op,1);
93
            }else{
94
                //if the operator is public, consider it requested, so it is not requested when received from other agent
95
                if(op != null && problem.getAction(op.actionHash).isPublic()){
96
                    requestedActions.add(op.operatorsIndex);
97
                }
98

  
99
                //enqueue normally
100
                p.cost = cost;
101
                p.distance = cost;
102
                p.reachedBy = op;
103
                explorationQueue.add(p);
104
            }
105
        }
106
    }
107

  
108

  
109
}
src/test/java/cz/agents/dimaptools/relaxed/TestRecursiveDistributedPersonalizedHeuristic.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");
src/test/java/cz/agents/dimaptools/relaxed/TestRecursiveDistributedRestrictedHeuristic.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.RecursiveDistributedRelaxationRestrictedReplyHeuristic;
8
import cz.agents.dimaptools.heuristic.relaxed.RecursiveDistributedRelaxationRestrictedRequestHeuristic;
9
import cz.agents.dimaptools.heuristic.relaxed.evaluator.AddEvaluator;
10
import cz.agents.dimaptools.search.AbstractDistributedAStarTest;
11
import cz.agents.dimaptools.search.DistributedBestFirstSearch;
12

  
13
public class TestRecursiveDistributedRestrictedHeuristic extends AbstractDistributedAStarTest {
14

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

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

  
32
		RecursiveDistributedRelaxationRestrictedRequestHeuristic req = new RecursiveDistributedRelaxationRestrictedRequestHeuristic(world, new AddEvaluator(world.getProblem()),60);
33
		RecursiveDistributedRelaxationRestrictedReplyHeuristic rep = new RecursiveDistributedRelaxationRestrictedReplyHeuristic(world, new AddEvaluator(world.getProblem()),req.getRequestProtocol());
34
		req.setReplyProtocol(rep.getReplyProtocol());
35
		
36

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

  
40
}

Also available in: Unified diff