-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDemo.java
More file actions
91 lines (82 loc) · 3.96 KB
/
Copy pathDemo.java
File metadata and controls
91 lines (82 loc) · 3.96 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
import io.github.pmctools.umbj.UMBException;
import io.github.pmctools.umbj.UMBIndex;
import io.github.pmctools.umbj.UMBReader;
import io.github.pmctools.umbj.UMBType;
import it.unimi.dsi.fastutil.doubles.DoubleArrayList;
import it.unimi.dsi.fastutil.doubles.DoubleList;
import it.unimi.dsi.fastutil.ints.IntArrayList;
import it.unimi.dsi.fastutil.ints.IntList;
import java.io.File;
import java.util.ArrayList;
/**
* A simple demo of using UMBReader to read an MDP from a UMB file.
*/
public class Demo
{
public static void main(String[] args)
{
try {
// Load an example UMB file
// (wget https://github.com/pmc-tools/umb/raw/refs/heads/main/example/mdp.umb)
UMBReader umbReader = new UMBReader(new File("mdp.umb"));
UMBIndex umbIndex = umbReader.getUMBIndex();
// Check model type
UMBIndex.ModelType modelType = umbIndex.getModelType();
UMBType branchType = umbIndex.getBranchProbabilityType();
if (modelType != UMBIndex.ModelType.MDP || branchType.type != UMBType.Type.DOUBLE) {
System.out.println("Unexpected model type: " + modelType + "(" + branchType + ")");
System.exit(1);
}
// Extract model stats
int numStates = (int) umbIndex.getNumStates();
int numChoices = (int) umbIndex.getNumChoices();
int numBranches = (int) umbIndex.getNumBranches();
System.out.println("States: " + numStates + ", choices: " + numChoices + ", transitions: " + numBranches);
// Extract action strings
ArrayList<String> actionStrings = null;
if (umbReader.hasChoiceActionStrings()) {
actionStrings = new ArrayList<>();
umbReader.extractChoiceActionStrings(actionStrings::add);
System.out.println("Actions: " + actionStrings);
}
// Extract transition function data
IntList stateChoiceOffsets = new IntArrayList(numStates + 1);
umbReader.extractStateChoiceOffsets(l -> stateChoiceOffsets.add((int) l));
IntList choiceBranchOffsets = new IntArrayList(numChoices + 1);
umbReader.extractChoiceBranchOffsets(l -> choiceBranchOffsets.add((int) l));
IntList branchSuccessors = new IntArrayList(numBranches);
umbReader.extractBranchTargets(l -> branchSuccessors.add((int) l));
DoubleList branchProbabilities = new DoubleArrayList(numBranches);
umbReader.extractBranchProbabilities(d -> branchProbabilities.add((double) d));
// Extract action info
IntList choiceActionIndices = null;
if (umbReader.hasChoiceActionIndices()) {
choiceActionIndices = new IntArrayList(numChoices);
umbReader.extractChoiceActionIndices(choiceActionIndices::add);
}
boolean hasActions = choiceActionIndices != null && actionStrings != null;
// Extract transition function from CSR
int iLo = 0, iHi = 0;
int jLo = 0, jHi = 0;
for (int s = 0; s < numStates; s++) {
iLo = iHi;
iHi = stateChoiceOffsets.getInt(s + 1);
int iCount = 0;
for (int i = iLo; i < iHi; i++) {
String action = hasActions ? actionStrings.get(choiceActionIndices.getInt(i)) : "?";
System.out.print(s + " -" + action + "->");
jLo = jHi;
jHi = choiceBranchOffsets.getInt(i + 1);
for (int j = jLo; j < jHi; j++) {
System.out.print(" " + branchProbabilities.getDouble(j) + ":" + branchSuccessors.getInt(j));
}
iCount++;
System.out.println();
}
}
} catch (UMBException e) {
System.out.println("UMB error: " + e.getMessage());
System.exit(1);
}
}
}