Skip to content

Commit c150243

Browse files
authored
[PEx] Revamps tracking unexplored choices, changes schedule choice (#745)
* [PEx] Change schedule choice from PMachine to PMachineId * [PEx] Major revamping of Choice: 1/n Old schedule/data choice changed to schedule/data SearchUnit Added new class for schedule/data Choice, which is just a wrapper around PMachineId/PValue<?> TODO: clean up Schedule with new class structure * Revert "[PEx] Major revamping of Choice: 1/n" This reverts commit 53c5d15. * [PEx] Separates unexplored choices from schedule * [PEx] Cleanup and minor corrections to recent changes to SearchTask * [PEx] Minor correction * [PEx] Corrections to new backtracking logic
1 parent 6cfb438 commit c150243

File tree

17 files changed

+418
-316
lines changed

17 files changed

+418
-316
lines changed

Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/PExplicitGlobal.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import lombok.Setter;
55
import pexplicit.commandline.PExplicitConfig;
66
import pexplicit.runtime.machine.PMachine;
7+
import pexplicit.runtime.machine.PMachineId;
78
import pexplicit.runtime.scheduler.Scheduler;
89
import pexplicit.runtime.scheduler.explicit.strategy.SearchStrategyMode;
910

@@ -60,19 +61,18 @@ public class PExplicitGlobal {
6061
/**
6162
* Get a machine of a given type and index if exists, else return null.
6263
*
63-
* @param type Machine type
64-
* @param idx Machine index
64+
* @param pid Machine pid
6565
* @return Machine
6666
*/
67-
public static PMachine getGlobalMachine(Class<? extends PMachine> type, int idx) {
68-
List<PMachine> machinesOfType = machineListByType.get(type);
67+
public static PMachine getGlobalMachine(PMachineId pid) {
68+
List<PMachine> machinesOfType = machineListByType.get(pid.getType());
6969
if (machinesOfType == null) {
7070
return null;
7171
}
72-
if (idx >= machinesOfType.size()) {
72+
if (pid.getTypeId() >= machinesOfType.size()) {
7373
return null;
7474
}
75-
PMachine result = machineListByType.get(type).get(idx);
75+
PMachine result = machineListByType.get(pid.getType()).get(pid.getTypeId());
7676
assert (machineSet.contains(result));
7777
return result;
7878
}

Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/logger/PExplicitLogger.java

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,14 @@
99
import pexplicit.runtime.PExplicitGlobal;
1010
import pexplicit.runtime.STATUS;
1111
import pexplicit.runtime.machine.PMachine;
12+
import pexplicit.runtime.machine.PMachineId;
1213
import pexplicit.runtime.machine.PMonitor;
1314
import pexplicit.runtime.machine.State;
1415
import pexplicit.runtime.machine.events.PContinuation;
1516
import pexplicit.runtime.scheduler.choice.Choice;
1617
import pexplicit.runtime.scheduler.choice.ScheduleChoice;
18+
import pexplicit.runtime.scheduler.choice.ScheduleSearchUnit;
19+
import pexplicit.runtime.scheduler.choice.SearchUnit;
1720
import pexplicit.runtime.scheduler.explicit.ExplicitSearchScheduler;
1821
import pexplicit.runtime.scheduler.explicit.SearchStatistics;
1922
import pexplicit.runtime.scheduler.explicit.StateCachingMode;
@@ -204,20 +207,21 @@ public static void logFinishedIteration(int step) {
204207
}
205208

206209
/**
207-
* Log when backtracking to a new choice
210+
* Log when backtracking to a search unit
208211
*
209-
* @param choice Choice to which backtracking to
212+
* @param stepNum Step number
213+
* @param choiceNum Choice number
214+
* @param unit Search unit to which backtracking to
210215
*/
211-
public static void logBacktrack(Choice choice) {
216+
public static void logBacktrack(int stepNum, int choiceNum, SearchUnit unit) {
212217
if (verbosity > 1) {
213218
log.info(String.format(" Backtracking to %s choice @%d::%d",
214-
((choice instanceof ScheduleChoice) ? "schedule" : "data"),
215-
choice.getStepNumber(),
216-
choice.getChoiceNumber()));
219+
((unit instanceof ScheduleSearchUnit) ? "schedule" : "data"),
220+
stepNum, choiceNum));
217221
}
218222
}
219223

220-
public static void logNewScheduleChoice(List<PMachine> choices, int step, int idx) {
224+
public static void logNewScheduleChoice(List<PMachineId> choices, int step, int idx) {
221225
if (verbosity > 1) {
222226
log.info(String.format(" @%d::%d new schedule choice: %s", step, idx, choices));
223227
}

Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/machine/PMachine.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public abstract class PMachine implements Serializable, Comparable<PMachine> {
2828
private static final Map<String, PMachine> nameToMachine = new HashMap<>();
2929
protected static int globalMachineId = 1;
3030
@Getter
31-
protected final int typeId;
31+
protected final PMachineId pid;
3232
@Getter
3333
protected final String name;
3434
private final Set<State> states;
@@ -72,7 +72,8 @@ public PMachine(String name, int id, State startState, State... states) {
7272
// initialize name, ids
7373
this.name = name;
7474
this.instanceId = ++globalMachineId;
75-
this.typeId = id;
75+
this.pid = new PMachineId(this.getClass(), id);
76+
this.pid.setName(this.toString());
7677
nameToMachine.put(toString(), this);
7778

7879
// initialize states
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package pexplicit.runtime.machine;
2+
3+
import lombok.Getter;
4+
import lombok.Setter;
5+
6+
@Getter
7+
public class PMachineId {
8+
Class<? extends PMachine> type;
9+
int typeId;
10+
@Setter
11+
String name;
12+
13+
public PMachineId(Class<? extends PMachine> t, int tid) {
14+
type = t;
15+
typeId = tid;
16+
name = null;
17+
}
18+
19+
20+
@Override
21+
public String toString() {
22+
return name;
23+
}
24+
25+
@Override
26+
public boolean equals(Object obj) {
27+
if (obj == this)
28+
return true;
29+
else if (!(obj instanceof PMachineId)) {
30+
return false;
31+
}
32+
PMachineId rhs = (PMachineId) obj;
33+
return this.type == rhs.type && this.typeId == rhs.typeId;
34+
}
35+
36+
}

Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/Schedule.java

Lines changed: 20 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import lombok.Getter;
44
import lombok.Setter;
55
import pexplicit.runtime.PExplicitGlobal;
6-
import pexplicit.runtime.machine.PMachine;
6+
import pexplicit.runtime.machine.PMachineId;
77
import pexplicit.runtime.scheduler.choice.Choice;
88
import pexplicit.runtime.scheduler.choice.DataChoice;
99
import pexplicit.runtime.scheduler.choice.ScheduleChoice;
@@ -48,51 +48,15 @@ public Choice getChoice(int idx) {
4848
return choices.get(idx);
4949
}
5050

51-
/**
52-
* Clear choice at a choice depth
53-
*
54-
* @param idx Choice depth
55-
*/
56-
public void clearChoice(int idx) {
57-
choices.get(idx).clearCurrent();
58-
choices.get(idx).clearUnexplored();
59-
}
60-
6151
/**
6252
* Remove choices after a choice depth
6353
*
6454
* @param choiceNum Choice depth
6555
*/
6656
public void removeChoicesAfter(int choiceNum) {
67-
choices.subList(choiceNum + 1, choices.size()).clear();
68-
}
69-
70-
/**
71-
* Get the number of unexplored choices in this schedule
72-
*
73-
* @return Number of unexplored choices
74-
*/
75-
public int getNumUnexploredChoices() {
76-
int numUnexplored = 0;
77-
for (Choice<?> c : choices) {
78-
numUnexplored += c.getUnexplored().size();
57+
if ((choiceNum + 1) < choices.size()) {
58+
choices.subList(choiceNum + 1, choices.size()).clear();
7959
}
80-
return numUnexplored;
81-
}
82-
83-
/**
84-
* Get the number of unexplored data choices in this schedule
85-
*
86-
* @return Number of unexplored data choices
87-
*/
88-
public int getNumUnexploredDataChoices() {
89-
int numUnexplored = 0;
90-
for (Choice<?> c : choices) {
91-
if (c instanceof DataChoice) {
92-
numUnexplored += c.getUnexplored().size();
93-
}
94-
}
95-
return numUnexplored;
9660
}
9761

9862
/**
@@ -101,19 +65,18 @@ public int getNumUnexploredDataChoices() {
10165
* @param stepNum Step number
10266
* @param choiceNum Choice number
10367
* @param current Machine to set as current schedule choice
104-
* @param unexplored List of machine to set as unexplored schedule choices
10568
*/
106-
public void setScheduleChoice(int stepNum, int choiceNum, PMachine current, List<PMachine> unexplored) {
69+
public void setScheduleChoice(int stepNum, int choiceNum, PMachineId current) {
10770
if (choiceNum == choices.size()) {
10871
choices.add(null);
10972
}
11073
assert (choiceNum < choices.size());
11174
if (PExplicitGlobal.getConfig().isStatefulBacktrackEnabled()
11275
&& stepNum != 0) {
11376
assert (stepBeginState != null);
114-
choices.set(choiceNum, new ScheduleChoice(stepNum, choiceNum, current, unexplored, stepBeginState));
77+
choices.set(choiceNum, new ScheduleChoice(stepNum, choiceNum, current, stepBeginState));
11578
} else {
116-
choices.set(choiceNum, new ScheduleChoice(stepNum, choiceNum, current, unexplored, null));
79+
choices.set(choiceNum, new ScheduleChoice(stepNum, choiceNum, current, null));
11780
}
11881
}
11982

@@ -123,14 +86,13 @@ public void setScheduleChoice(int stepNum, int choiceNum, PMachine current, List
12386
* @param stepNum Step number
12487
* @param choiceNum Choice number
12588
* @param current PValue to set as current schedule choice
126-
* @param unexplored List of PValue to set as unexplored schedule choices
12789
*/
128-
public void setDataChoice(int stepNum, int choiceNum, PValue<?> current, List<PValue<?>> unexplored) {
90+
public void setDataChoice(int stepNum, int choiceNum, PValue<?> current) {
12991
if (choiceNum == choices.size()) {
13092
choices.add(null);
13193
}
13294
assert (choiceNum < choices.size());
133-
choices.set(choiceNum, new DataChoice(stepNum, choiceNum, current, unexplored));
95+
choices.set(choiceNum, new DataChoice(current));
13496
}
13597

13698
/**
@@ -139,7 +101,7 @@ public void setDataChoice(int stepNum, int choiceNum, PValue<?> current, List<PV
139101
* @param idx Choice depth
140102
* @return Current schedule choice
141103
*/
142-
public PMachine getCurrentScheduleChoice(int idx) {
104+
public PMachineId getCurrentScheduleChoice(int idx) {
143105
assert (choices.get(idx) instanceof ScheduleChoice);
144106
return ((ScheduleChoice) choices.get(idx)).getCurrent();
145107
}
@@ -155,57 +117,24 @@ public PValue<?> getCurrentDataChoice(int idx) {
155117
return ((DataChoice) choices.get(idx)).getCurrent();
156118
}
157119

158-
/**
159-
* Get unexplored schedule choices at a choice depth.
160-
*
161-
* @param idx Choice depth
162-
* @return List of machines, or null if index is invalid
163-
*/
164-
public List<PMachine> getUnexploredScheduleChoices(int idx) {
165-
if (idx < size()) {
166-
assert (choices.get(idx) instanceof ScheduleChoice);
167-
return ((ScheduleChoice) choices.get(idx)).getUnexplored();
168-
} else {
169-
return new ArrayList<>();
170-
}
171-
}
172-
173-
/**
174-
* Get unexplored data choices at a choice depth.
175-
*
176-
* @param idx Choice depth
177-
* @return List of PValue, or null if index is invalid
178-
*/
179-
public List<PValue<?>> getUnexploredDataChoices(int idx) {
180-
if (idx < size()) {
181-
assert (choices.get(idx) instanceof DataChoice);
182-
return ((DataChoice) choices.get(idx)).getUnexplored();
183-
} else {
184-
return new ArrayList<>();
185-
}
186-
}
187-
188-
public ScheduleChoice getScheduleChoiceAt(Choice choice) {
189-
if (choice instanceof ScheduleChoice scheduleChoice) {
190-
return scheduleChoice;
191-
} else {
192-
for (int i = choice.getChoiceNumber() - 1; i >= 0; i--) {
193-
Choice c = choices.get(i);
194-
if (c instanceof ScheduleChoice scheduleChoice) {
195-
return scheduleChoice;
196-
}
120+
public ScheduleChoice getScheduleChoiceAt(int choiceNum) {
121+
for (int i = choiceNum; i >= 0; i--) {
122+
if (choiceNum >= choices.size()) {
123+
continue;
124+
}
125+
Choice c = choices.get(i);
126+
if (c instanceof ScheduleChoice scheduleChoice) {
127+
return scheduleChoice;
197128
}
198129
}
199130
return null;
200131
}
201132

202133
/**
203-
* Clear current choices at a choice depth
204-
*
205-
* @param idx Choice depth
134+
* Clear current choices
206135
*/
207-
public void clearCurrent(int idx) {
208-
choices.get(idx).clearCurrent();
136+
public void clear() {
137+
choices.clear();
209138
}
210139

211140
/**

Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/Scheduler.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import pexplicit.runtime.PExplicitGlobal;
66
import pexplicit.runtime.logger.PExplicitLogger;
77
import pexplicit.runtime.machine.PMachine;
8+
import pexplicit.runtime.machine.PMachineId;
89
import pexplicit.runtime.machine.PMonitor;
910
import pexplicit.runtime.scheduler.explicit.StepState;
1011
import pexplicit.utils.exceptions.BugFoundException;
@@ -271,8 +272,8 @@ public PMachine allocateMachine(
271272
Function<Integer, ? extends PMachine> constructor) {
272273
// get machine count for given type from schedule
273274
int machineCount = stepState.getMachineCount(machineType);
274-
275-
PMachine machine = PExplicitGlobal.getGlobalMachine(machineType, machineCount);
275+
PMachineId pid = new PMachineId(machineType, machineCount);
276+
PMachine machine = PExplicitGlobal.getGlobalMachine(pid);
276277
if (machine == null) {
277278
// create a new machine
278279
machine = constructor.apply(machineCount);

0 commit comments

Comments
 (0)