Browse Source

accumulation: further refactoring of Product

accumulation
Sascha Wunderlich 10 years ago
committed by Sascha Wunderlich
parent
commit
385884a590
  1. 19
      prism/src/explicit/AccumulationProduct.java
  2. 70
      prism/src/explicit/AccumulationProductCounting.java
  3. 24
      prism/src/explicit/AccumulationProductRegular.java

19
prism/src/explicit/AccumulationProduct.java

@ -60,6 +60,7 @@ public abstract class AccumulationProduct<M extends Model,Component> extends Pro
if (!isFinalTrack(track,accexp,mc)) { return false; }
boolean isGood = false;
//TODO: these should be double later on
// Collect the weight linear combination, factor*weight+...
int lhs = 0;
int factorNr = 0;
@ -106,8 +107,6 @@ public abstract class AccumulationProduct<M extends Model,Component> extends Pro
protected AccumulationState<Component> updateAccumulationState(final int modelFromStateId,
final AccumulationState<Component> accstate, final ExpressionAccumulation accexp,
final double[] weights, final ProbModelChecker mc) throws PrismException {
// We have the current accumulation state, the current model id and the accumulation expression.
// Get the old tracker and tracks.
AccumulationTracker<Component> oldTracker = accstate.getTracker(trackers);
ArrayList<AccumulationTrack<Component>> oldTracks = oldTracker.getTracks();
@ -162,6 +161,22 @@ public abstract class AccumulationProduct<M extends Model,Component> extends Pro
return new AccumulationState<>(newTrackerId, newLastRestartNr, numberOfTracks, newGoodTracks);
}
protected int createInitialStateId(int numberOfRewards) {
Component initialComponent = getInitialComponent();
// The initial active track is the first one, all tracks are non-good by default
int initialActiveTrack = 0;
BitSet initialGoodTracks = new BitSet();
// Generate the initial track and product state
AccumulationTracker<Component> initialTracker = new AccumulationTracker<Component>(numberOfTracks, numberOfRewards, initialComponent);
int initialTrackerId = trackers.findOrAdd(initialTracker);
AccumulationState<Component> initialAccState = new AccumulationState<Component>(initialTrackerId, initialActiveTrack, numberOfTracks, initialGoodTracks);
int initialAccStateId = accStates.findOrAdd(initialAccState);
return initialAccStateId;
}
@Override
public String toDot() {
StringBuffer result = new StringBuffer();

70
prism/src/explicit/AccumulationProductCounting.java

@ -1,6 +1,5 @@
package explicit;
import java.util.ArrayList;
import java.util.BitSet;
import java.util.Vector;
@ -170,58 +169,6 @@ public class AccumulationProductCounting<M extends Model> extends AccumulationPr
protected Integer getInitialComponent() {
return 0;
}
protected AccumulationState<Integer> updateAccumulationState(final int modelFromStateId,
final AccumulationState<Integer> accstate, final ExpressionAccumulation accexp,
final double[] weights, final ProbModelChecker mc) throws PrismException {
// We have the current accumulation state, the current model id and the accumulation expression.
// Get the old tracker and tracks.
AccumulationTracker<Integer> oldTracker = accstate.getTracker(trackers);
ArrayList<AccumulationTrack<Integer>> oldTracks = oldTracker.getTracks();
BitSet oldGoodTracks = accstate.getGoodTracks();
BitSet newGoodTracks = (BitSet) oldGoodTracks.clone();
// This restart will be...
int newLastRestartNr = accstate.getNextRestartNr();
//mc.getLog().print(newLastRestartNr);
// Build the new tracks.
ArrayList<AccumulationTrack<Integer>> newTracks = new ArrayList<>();
int trackNr = 0;
for(AccumulationTrack<Integer> oldTrack : oldTracks) {
AccumulationTrack<Integer> newTrack;
// restart or advance
if(trackNr == newLastRestartNr) {
//assert oldTrack == null : "Track " + newLastRestartNr + " is not null!";
newTrack = new AccumulationTrack<Integer>(numberOfWeights, 0); //TODO: off-by-one?
newGoodTracks.clear(trackNr);
} else if (oldTrack == null) {
newTrack = null;
} else {
assert oldTrack != null;
newTrack = updateTrack(modelFromStateId, oldTrack, accexp, weights, mc);
}
// check whether the track is good
if(!newGoodTracks.get(trackNr)) {
newGoodTracks.set(trackNr, isGoodTrack(newTrack, accexp, mc));
}
newTracks.add(newTrack);
trackNr++;
}
AccumulationTracker<Integer> newTracker = new AccumulationTracker<>(newTracks);
int newTrackerId = trackers.findOrAdd(newTracker);
return new AccumulationState<>(newTrackerId, newLastRestartNr, numberOfTracks, newGoodTracks);
}
@Override
protected AccumulationTrack<Integer> updateTrack(Integer modelFromStateId, final AccumulationTrack<Integer> track,
@ -246,23 +193,10 @@ public class AccumulationProductCounting<M extends Model> extends AccumulationPr
return new AccumulationTrack<Integer>(newweights, currentStep+1);
}
protected int createInitialStateId(final int numberOfRewards) {
// The initial active track is the first one, all tracks are non-good by default
int initialActiveTrack = 0;
BitSet initialGoodTracks = new BitSet();
// Generate the initial tracker and product state
AccumulationTracker<Integer> initialTracker = new AccumulationTracker<>(numberOfTracks, numberOfRewards, 0);
int initialTrackerId = trackers.findOrAdd(initialTracker);
AccumulationState<Integer> initialAccState = new AccumulationState<>(initialTrackerId, initialActiveTrack, numberOfTracks, initialGoodTracks);
int initialAccStateId = accStates.findOrAdd(initialAccState);
return initialAccStateId;
}
/**
* Populates fields:
* - numberOfTracks
* - numberOfTracks with the highest relevant integer plus one, and
* - numberOfWeights with the size of the reward vector.
* @param graph
* @param accexp
* @param rewards

24
prism/src/explicit/AccumulationProductRegular.java

@ -215,34 +215,18 @@ public class AccumulationProductRegular<M extends Model> extends AccumulationPro
return new AccumulationTrack<State>(newweights, nextState);
}
protected int createInitialStateId(int numberOfRewards) {
// Find the initial state id (0 or automaton state)
State initialState = automaton.getInitialState();
// The initial active track is the first one, all tracks are non-good by default
int initialActiveTrack = 0;
BitSet initialGoodTracks = new BitSet();
// Generate the initial track and product state
AccumulationTracker<State> initialTracker = new AccumulationTracker<State>(numberOfTracks, numberOfRewards, initialState);
int initialTrackerId = trackers.findOrAdd(initialTracker);
AccumulationState<State> initialAccState = new AccumulationState<State>(initialTrackerId, initialActiveTrack, numberOfTracks, initialGoodTracks);
int initialAccStateId = accStates.findOrAdd(initialAccState);
return initialAccStateId;
}
/**
* Populates fields:
* - automaton with a trimmed deterministic finite automaton and
* - numberOfTracks with the length of its longest path.
* - automaton with a trimmed deterministic finite automaton,
* - numberOfTracks with the length of its longest path plus one, and
* - numberOfWeights with the size of the reward vector.
*
* @param graph
* @param accexp
* @param mc
* @throws PrismException
*/
public void createAuxData(final Model graph, final ExpressionAccumulation accexp, final Vector<? extends Rewards> rewards, final ProbModelChecker mc) throws PrismException {
protected void createAuxData(final Model graph, final ExpressionAccumulation accexp, final Vector<? extends Rewards> rewards, final ProbModelChecker mc) throws PrismException {
// Build labels and DFA
AccumulationModelChecker accMc = new AccumulationModelChecker();
ExpressionRegular reg = (ExpressionRegular)accMc.checkMaximalStateFormulas(mc, graph, accexp.getRegularExpression(), labels);

Loading…
Cancel
Save