-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProphetDecisionEngine.java
More file actions
202 lines (164 loc) · 5.95 KB
/
Copy pathProphetDecisionEngine.java
File metadata and controls
202 lines (164 loc) · 5.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/*
J. P. Denata
*/
package routing;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import core.*;
import routing.DecisionEngineRouter;
import routing.MessageRouter;
import routing.RoutingDecisionEngine;
public class ProphetDecisionEngine implements RoutingDecisionEngine {
protected final static String BETA_SETTING = "beta";
protected final static String P_INIT_SETTING = "initial_p";
protected final static String SECONDS_IN_UNIT_S = "secondsInTimeUnit";
protected static final double DEFAULT_P_INIT = 0.75;
protected static final double GAMMA = 0.92;
protected static final double DEFAULT_BETA = 0.45;
protected static final int DEFAULT_UNIT = 30;
protected double beta;
protected double pinit;
protected double lastAgeUpdate;
protected int secondsInTimeUnit;
private Set<Message> msgStamp;
private Map<DTNHost, Integer> relayed;
private DTNHost meHost;
/**
* delivery predictabilities
*/
private Map<DTNHost, Double> preds;
public ProphetDecisionEngine(Settings s) {
if (s.contains(BETA_SETTING)) {
beta = s.getDouble(BETA_SETTING);
} else {
beta = DEFAULT_BETA;
}
if (s.contains(P_INIT_SETTING)) {
pinit = s.getDouble(P_INIT_SETTING);
} else {
pinit = DEFAULT_P_INIT;
}
if (s.contains(SECONDS_IN_UNIT_S)) {
secondsInTimeUnit = s.getInt(SECONDS_IN_UNIT_S);
} else {
secondsInTimeUnit = DEFAULT_UNIT;
}
preds = new HashMap<DTNHost, Double>();
this.lastAgeUpdate = 0.0;
}
public ProphetDecisionEngine(ProphetDecisionEngine de) {
beta = de.beta;
pinit = de.pinit;
secondsInTimeUnit = de.secondsInTimeUnit;
meHost = de.meHost;
msgStamp = new HashSet<>();
relayed = new HashMap<>();
preds = new HashMap<DTNHost, Double>();
this.lastAgeUpdate = de.lastAgeUpdate;
}
public RoutingDecisionEngine replicate() {
return new ProphetDecisionEngine(this);
}
public void connectionUp(DTNHost thisHost, DTNHost peer) {
}
public void connectionDown(DTNHost thisHost, DTNHost peer) {
}
public void doExchangeForNewConnection(Connection con, DTNHost peer) {
DTNHost myHost = con.getOtherNode(peer);
ProphetDecisionEngine de = getOtherProphetDecisionEngine(peer);
Set<DTNHost> hostSet = new HashSet<DTNHost>(this.preds.size()
+ de.preds.size());
hostSet.addAll(this.preds.keySet());
hostSet.addAll(de.preds.keySet());
this.agePreds();
de.agePreds();
// Update preds for this connection
double myOldValue = this.getPredFor(peer),
peerOldValue = de.getPredFor(myHost),
myPforHost = myOldValue + (1 - myOldValue) * pinit,
peerPforMe = peerOldValue + (1 - peerOldValue) * de.pinit;
preds.put(peer, myPforHost);
de.preds.put(myHost, peerPforMe);
// Update transistivities
for (DTNHost h : hostSet) {
myOldValue = 0.0;
peerOldValue = 0.0;
if (preds.containsKey(h)) {
myOldValue = preds.get(h);
}
if (de.preds.containsKey(h)) {
peerOldValue = de.preds.get(h);
}
if (h != myHost) {
preds.put(h, myOldValue + (1 - myOldValue) * myPforHost * peerOldValue * beta);
}
if (h != peer) {
de.preds.put(h, peerOldValue + (1 - peerOldValue) * peerPforMe * myOldValue * beta);
}
}
}
public boolean newMessage(Message m) {
return true;
}
public boolean isFinalDest(Message m, DTNHost aHost) {
return m.getTo() == aHost;
}
public boolean shouldSaveReceivedMessage(Message m, DTNHost thisHost) {
msgStamp.add(m);
meHost = thisHost;
return m.getTo() != thisHost;
}
public boolean shouldSendMessageToHost(Message m, DTNHost otherHost, DTNHost thisHost) {
if (m.getTo() == otherHost) {
return true;
}
ProphetDecisionEngine de = getOtherProphetDecisionEngine(otherHost);
if (msgStamp.contains(m)) {
relayed.put(meHost, !relayed.containsKey(meHost)? 1 : relayed.get(meHost) + 1);
}
return de.getPredFor(m.getTo()) > this.getPredFor(m.getTo());
}
public boolean shouldDeleteSentMessage(Message m, DTNHost otherHost) {
return false;
}
public boolean shouldDeleteOldMessage(Message m, DTNHost hostReportingOld) {
return m.getTo() == hostReportingOld;
}
private ProphetDecisionEngine getOtherProphetDecisionEngine(DTNHost host) {
MessageRouter otherRouter = host.getRouter();
assert otherRouter instanceof DecisionEngineRouter : "This router only works "
+ " with other routers of same type";
return (ProphetDecisionEngine) ((DecisionEngineRouter) otherRouter).getDecisionEngine();
}
private void agePreds() {
double timeDiff = (SimClock.getTime() - this.lastAgeUpdate)
/ secondsInTimeUnit;
if (timeDiff == 0) {
return;
}
double mult = Math.pow(GAMMA, timeDiff);
for (Map.Entry<DTNHost, Double> e : preds.entrySet()) {
e.setValue(e.getValue() * mult);
}
this.lastAgeUpdate = SimClock.getTime();
}
/**
* Returns the current prediction (P) value for a host or 0 if entry for the
* host doesn't exist.
*
* @param host The host to look the P for
* @return the current P value
*/
private double getPredFor(DTNHost host) {
agePreds(); // make sure preds are updated before getting
if (preds.containsKey(host)) {
return preds.get(host);
} else {
return 0;
}
}
@Override
public void update(DTNHost thisHost) {}
}