Revision 183:f4bb026e1e1b

View differences:

src/main/java/cz/agents/dimaptools/DIMAPWorldInterface.java
22 22
	
23 23
	public Domain getDomain();
24 24

  
25
	public int getNumberOfAgents();
26

  
25 27
}
src/main/java/cz/agents/dimaptools/DefaultDIMAPWorld.java
18 18
	public final EncoderInterface encoder;
19 19
	public final Problem problem;
20 20
	public final Domain domain;
21
	public final int totalNumberOfAgents;
21 22
	
22 23
	
23 24
	
......
28 29
			CommunicationPerformer commPerformer, 
29 30
			EncoderInterface encoder,
30 31
			Problem problem, 
31
			Domain domain) {
32
			Domain domain,
33
			int totalNumberOfAgents) {
32 34
		this.name = name;
33 35
		this.communicator = communicator;
34 36
		this.commPerformer = commPerformer;
35 37
		this.encoder = encoder;
36 38
		this.problem = problem;
37 39
		this.domain = domain;
40
		this.totalNumberOfAgents = totalNumberOfAgents;
38 41
		
39 42
		id = currentID++;
40 43
	}
......
44 47
			PerformerCommunicator communicator,
45 48
			EncoderInterface encoder,
46 49
			Problem problem, 
47
			Domain domain) {
50
			Domain domain,
51
			int totalNumberOfAgents) {
48 52
		this.name = name;
49 53
		this.communicator = communicator;
50 54
		this.commPerformer = communicator;
51 55
		this.encoder = encoder;
52 56
		this.problem = problem;
53 57
		this.domain = domain;
58
		this.totalNumberOfAgents = totalNumberOfAgents;
54 59
		
55 60
		id = currentID++;
56 61
	}
......
59 64
			String name, 
60 65
			PerformerCommunicator communicator,
61 66
			EncoderInterface encoder,
62
			Problem problem) {
67
			Problem problem,
68
			int totalNumberOfAgents) {
63 69
		this.name = name;
64 70
		this.communicator = communicator;
65 71
		this.commPerformer = communicator;
66 72
		this.encoder = encoder;
67 73
		this.problem = problem;
68 74
		this.domain = null;
75
		this.totalNumberOfAgents = totalNumberOfAgents;
69 76
		
70 77
		id = currentID++;
71 78
	}
......
105 112
		return id;
106 113
	}
107 114

  
115
	@Override
116
	public int getNumberOfAgents() {
117
		return totalNumberOfAgents;
118
	}
119

  
108 120
}
src/main/java/cz/agents/dimaptools/communication/message/HeuristicReplyWithPublicActionsMessage.java
68 68
	}
69 69

  
70 70
	@Override
71
	public void visit(MessageVisitorInterface visitor) {
72
		visitor.process(this);
71
	public void visit(MessageVisitorInterface visitor, String sender) {
72
		visitor.process(this, sender);
73 73
	}
74 74

  
75 75

  
src/main/java/cz/agents/dimaptools/communication/message/HeuristicRequestMessage.java
76 76
	}
77 77
	
78 78
	@Override
79
	public void visit(MessageVisitorInterface visitor) {
80
		visitor.process(this);
79
	public void visit(MessageVisitorInterface visitor, String sender) {
80
		visitor.process(this, sender);
81 81
	}
82 82

  
83 83
}
src/main/java/cz/agents/dimaptools/communication/message/MessageVisitorInterface.java
3 3

  
4 4
public interface MessageVisitorInterface {
5 5
	
6
	public void process(HeuristicReplyWithPublicActionsMessage msg);
6
	public void process(HeuristicReplyWithPublicActionsMessage msg, String sender);
7 7
	
8
	public void process(HeuristicRequestMessage msg);
8
	public void process(HeuristicRequestMessage msg, String sender);
9 9
		
10
	public void process(PlanningFinishedMessage msg);
10
	public void process(PlanningFinishedMessage msg, String sender);
11 11
	
12
	public void process(ReconstructPlanMessage msg);
12
	public void process(ReconstructPlanMessage msg, String sender);
13 13
	
14
	public void process(StateMessage msg);
14
	public void process(StateMessage msg, String sender);
15 15
	
16
	
16
	public void process(SharedProblemInfoMessage msg, String sender);
17 17

  
18 18
}
src/main/java/cz/agents/dimaptools/communication/message/PlanningFinishedMessage.java
10 10
	}
11 11

  
12 12
	@Override
13
	public void visit(MessageVisitorInterface visitor) {
14
		visitor.process(this);
13
	public void visit(MessageVisitorInterface visitor, String sender) {
14
		visitor.process(this, sender);
15 15
	}
16 16

  
17 17
}
src/main/java/cz/agents/dimaptools/communication/message/ReconstructPlanMessage.java
61 61

  
62 62

  
63 63
	@Override
64
	public void visit(MessageVisitorInterface visitor) {
65
		visitor.process(this);
64
	public void visit(MessageVisitorInterface visitor, String sender) {
65
		visitor.process(this, sender);
66 66
	}
67 67

  
68 68

  
src/main/java/cz/agents/dimaptools/communication/message/SharedProblemInfoMessage.java
1
package cz.agents.dimaptools.communication.message;
2

  
3

  
4
public class SharedProblemInfoMessage  implements VisitableMessage{
5

  
6
	private final int couplingEstimate;
7

  
8
	public SharedProblemInfoMessage(int couplingEstimate) {
9
		super();
10
		this.couplingEstimate = couplingEstimate;
11
	}
12

  
13

  
14
	public int getCouplingEstimate() {
15
		return couplingEstimate;
16
	}
17

  
18

  
19
	@Override
20
	public String toString() {
21
		return "SharedProblemInfoMessage [couplingEstimate="+couplingEstimate+"]";
22
	}
23

  
24

  
25
	public int getBytes() {
26
		return 4;
27
	}
28
	
29
	@Override
30
	public void visit(MessageVisitorInterface visitor, String sender) {
31
		visitor.process(this, sender);
32
	}
33

  
34
}
src/main/java/cz/agents/dimaptools/communication/message/StateMessage.java
77 77
    }
78 78
    
79 79
    @Override
80
	public void visit(MessageVisitorInterface visitor) {
81
		visitor.process(this);
80
	public void visit(MessageVisitorInterface visitor, String sender) {
81
		visitor.process(this, sender);
82 82
	}
83 83

  
84 84
}
src/main/java/cz/agents/dimaptools/communication/message/VisitableMessage.java
2 2

  
3 3
public interface VisitableMessage {
4 4
	
5
	public void visit(MessageVisitorInterface visitor);
5
	public void visit(MessageVisitorInterface visitor, String sender);
6 6

  
7 7
}
src/main/java/cz/agents/dimaptools/communication/protocol/DefaultEncoder.java
5 5
import cz.agents.dimaptools.communication.message.HeuristicRequestMessage;
6 6
import cz.agents.dimaptools.communication.message.PlanningFinishedMessage;
7 7
import cz.agents.dimaptools.communication.message.ReconstructPlanMessage;
8
import cz.agents.dimaptools.communication.message.SharedProblemInfoMessage;
8 9
import cz.agents.dimaptools.communication.message.StateMessage;
9 10

  
10 11
public class DefaultEncoder implements EncoderInterface {
......
30 31
	}
31 32

  
32 33
	@Override
33
	public Content encodeHeuristicReplyWithPublicActionsMessage(
34
			HeuristicReplyWithPublicActionsMessage re) {
34
	public Content encodeHeuristicReplyWithPublicActionsMessage(HeuristicReplyWithPublicActionsMessage re) {
35 35
		return new Content(re);
36 36
	}
37 37
	
38
	
38
	@Override
39
	public Content encodeSharedProblemInfoMessage(SharedProblemInfoMessage msg) {
40
		return new Content(msg);
41
	}
39 42
	
40 43
	
41 44

  
......
46 49

  
47 50
	
48 51

  
52
	
53

  
49 54
}
src/main/java/cz/agents/dimaptools/communication/protocol/DistributedProblemInfoSharingProtocol.java
1
package cz.agents.dimaptools.communication.protocol;
2

  
3
import org.apache.log4j.Logger;
4

  
5
import cz.agents.alite.communication.Communicator;
6
import cz.agents.alite.communication.Message;
7
import cz.agents.alite.communication.MessageHandler;
8
import cz.agents.alite.communication.QueuedCommunicator;
9
import cz.agents.alite.communication.channel.CommunicationChannelBroadcast;
10
import cz.agents.alite.communication.protocol.DefaultProtocol;
11
import cz.agents.dimaptools.communication.message.HeuristicReplyWithPublicActionsMessage;
12
import cz.agents.dimaptools.communication.message.HeuristicRequestMessage;
13
import cz.agents.dimaptools.communication.message.MessageVisitorInterface;
14
import cz.agents.dimaptools.communication.message.PlanningFinishedMessage;
15
import cz.agents.dimaptools.communication.message.ReconstructPlanMessage;
16
import cz.agents.dimaptools.communication.message.SharedProblemInfoMessage;
17
import cz.agents.dimaptools.communication.message.StateMessage;
18
import cz.agents.dimaptools.communication.message.VisitableMessage;
19

  
20
public abstract class DistributedProblemInfoSharingProtocol extends DefaultProtocol implements MessageVisitorInterface {
21
	
22
	private static final Logger LOGGER = Logger.getLogger(DistributedProblemInfoSharingProtocol.class);
23

  
24
	private final EncoderInterface encoder;
25

  
26
	public DistributedProblemInfoSharingProtocol(Communicator communicator, final String name, final EncoderInterface encoder) {
27
		super(communicator, name);
28

  
29
		this.encoder = encoder;
30
		
31
		communicator.addMessageHandler(new MessageHandler() {
32
			
33
			@Override
34
			public void notify(Message message) {
35
				if(message.getSender().equals(name))return;
36
				
37
				
38
				Object data = null;
39
				
40
				try{
41
					data = encoder.decode(message.getContent());
42
				}catch(Exception e){
43
					e.printStackTrace();
44
					return;
45
				}
46
				
47
				if(data == null){
48
					LOGGER.warn("received empty data: " + message);
49
				}
50
				
51
				((VisitableMessage)data).visit(DistributedProblemInfoSharingProtocol.this, message.getSender());
52
				
53
				
54
			}
55
		});
56
	}
57
	
58
	
59
	
60
	
61
	public void sendSharedProblemInfoMessage(SharedProblemInfoMessage msg){
62
		if(LOGGER.isDebugEnabled())LOGGER.debug(communicator.getAddress() + " send SharedProblemInfoMessage: " + msg);
63
		Message message = communicator.createMessage(encoder.encodeSharedProblemInfoMessage(msg));
64
        message.addReceiver(CommunicationChannelBroadcast.BROADCAST_ADDRESS);
65
        communicator.sendMessage(message);
66
	}
67
	
68
	
69
	public static void registerClasses(QueuedCommunicator comm){
70
		comm.handleMessageClass(SharedProblemInfoMessage.class);
71
	}
72

  
73

  
74

  
75

  
76
	@Override
77
	public void process(HeuristicReplyWithPublicActionsMessage msg, String sender) {
78
		
79
	}
80

  
81
	@Override
82
	public void process(HeuristicRequestMessage msg, String sender) {
83
		
84
	}
85

  
86
	@Override
87
	public void process(PlanningFinishedMessage msg, String sender) {
88
		
89
	}
90

  
91
	@Override
92
	public void process(ReconstructPlanMessage msg, String sender) {
93
		
94
	}
95

  
96
	@Override
97
	public void process(StateMessage msg, String sender) {
98
		
99
	}
100

  
101
}
src/main/java/cz/agents/dimaptools/communication/protocol/EncoderInterface.java
5 5
import cz.agents.dimaptools.communication.message.HeuristicRequestMessage;
6 6
import cz.agents.dimaptools.communication.message.PlanningFinishedMessage;
7 7
import cz.agents.dimaptools.communication.message.ReconstructPlanMessage;
8
import cz.agents.dimaptools.communication.message.SharedProblemInfoMessage;
8 9
import cz.agents.dimaptools.communication.message.StateMessage;
9 10

  
10 11
public interface EncoderInterface {
......
21 22

  
22 23
	public Content encodeHeuristicReplyWithPublicActionsMessage(HeuristicReplyWithPublicActionsMessage re);
23 24
	
24
	
25
	
25
	//other
26
	public Content encodeSharedProblemInfoMessage(SharedProblemInfoMessage msg);
26 27

  
27 28
	public Object decode(Content content);
28 29

  
src/main/java/cz/agents/dimaptools/util/SharedProblemInfoProvider.java
1
package cz.agents.dimaptools.util;
2

  
3
import java.util.HashMap;
4
import java.util.Map;
5
import java.util.Set;
6

  
7
import org.apache.log4j.Logger;
8

  
9
import cz.agents.dimaptools.DIMAPWorldInterface;
10
import cz.agents.dimaptools.communication.message.SharedProblemInfoMessage;
11
import cz.agents.dimaptools.communication.protocol.DistributedProblemInfoSharingProtocol;
12
import cz.agents.dimaptools.model.Problem;
13

  
14
public class SharedProblemInfoProvider {
15
	
16
	 private static final Logger LOGGER = Logger.getLogger(SharedProblemInfoProvider.class);
17
	
18
	private DistributedProblemInfoSharingProtocol protocol;
19
	
20
	private Map<String,Integer> couplings = new HashMap<>();
21
	private int waitingFor;
22
	
23
	public SharedProblemInfoProvider(final DIMAPWorldInterface world, int totalAgents){
24
		
25
		waitingFor = totalAgents-1;
26
		
27
		protocol = new DistributedProblemInfoSharingProtocol(world.getCommunicator(),world.getAgentName(),world.getEncoder()) {
28
			
29
			@Override
30
			public void process(SharedProblemInfoMessage msg, String sender) {
31
				LOGGER.info(world.getAgentName() + " received send info from " + sender);
32
				couplings.put(sender, msg.getCouplingEstimate());
33
				--waitingFor;
34
			}
35
		};
36
		
37
		LOGGER.info(world.getAgentName() + " send info");
38
		protocol.sendSharedProblemInfoMessage(new SharedProblemInfoMessage(computeMyCoupling(world.getProblem())));
39
		
40
		while(waitingFor > 0){
41
			world.getCommPerformer().performReceive();
42
		}
43
	}
44
	
45
	
46
	private int computeMyCoupling(Problem problem){
47
		return (int)(((double)problem.getMyPublicActions().size()) / ((double)problem.getMyActions().size()) * 100);
48
	}
49
	
50
	public Set<String> getKnownAgents(){
51
		return couplings.keySet();
52
	}
53
	
54
	public int getCoupling(String agent){
55
		return couplings.get(agent);
56
	}
57

  
58
}
src/test/java/cz/agents/dimaptools/search/AbstractDistributedAStarTest.java
24 24
import cz.agents.dimaptools.DefaultDIMAPWorld;
25 25
import cz.agents.dimaptools.communication.protocol.DefaultEncoder;
26 26
import cz.agents.dimaptools.communication.protocol.DistributedHeuristicProtocol;
27
import cz.agents.dimaptools.communication.protocol.DistributedProblemInfoSharingProtocol;
27 28
import cz.agents.dimaptools.communication.protocol.DistributedSearchProtocol;
28 29
import cz.agents.dimaptools.input.addl.ADDLObject;
29 30
import cz.agents.dimaptools.input.addl.ADDLParser;
......
93 94
                    initCommunicator(a),
94 95
                    new DefaultEncoder(),
95 96
                    problems.get(a),
96
                    null);
97
                    null,
98
                    problems.size());
97 99

  
98 100
            Thread thread = new Thread(new Runnable() {
99 101

  
......
162 164

  
163 165
            DistributedSearchProtocol.registerClasses(communicator);
164 166
            DistributedHeuristicProtocol.registerClasses(communicator);
167
            DistributedProblemInfoSharingProtocol.registerClasses(communicator);
165 168

  
166 169
            communicator.addChannel(new DirectCommunicationChannelAsync(communicator, receiverTable, executorService));
167 170
        } catch (CommunicationChannelException e) {
src/test/java/cz/agents/dimaptools/util/TestSharedProblemInfoProvider.java
1
package cz.agents.dimaptools.util;
2

  
3
import org.junit.Test;
4

  
5
import cz.agents.dimaptools.DIMAPWorldInterface;
6
import cz.agents.dimaptools.search.AbstractDistributedAStarTest;
7

  
8
public class TestSharedProblemInfoProvider extends AbstractDistributedAStarTest {
9

  
10
	@Test
11
	public void test() {
12
//		testProblem("truck-crane-a2");
13
//		testProblem("logistics-a2");
14
//		testProblem("logistics-a4");
15
//		testProblem("deconfliction-a4");
16
//		testProblem("rovers-a4");
17
//		testProblem("sokoban-a1");
18
//		testProblem("sokoban-a2");
19
	}
20
	
21
	@Override
22
	public void runSearch(DIMAPWorldInterface world) {
23
		SharedProblemInfoProvider provider = new SharedProblemInfoProvider(world,world.getNumberOfAgents());
24
		
25
		System.out.println(world.getAgentName() + ":");
26
		for(String agent : provider.getKnownAgents()){
27
			System.out.println("  " + agent + ": " + provider.getCoupling(agent));
28
		}
29
		
30
	}
31

  
32
}

Also available in: Unified diff