|
|
|
@ -26,8 +26,12 @@ |
|
|
|
|
|
|
|
package explicit; |
|
|
|
|
|
|
|
import java.util.*; |
|
|
|
import java.util.BitSet; |
|
|
|
import java.util.HashMap; |
|
|
|
import java.util.Iterator; |
|
|
|
import java.util.Map; |
|
|
|
import java.util.Map.Entry; |
|
|
|
import java.util.Set; |
|
|
|
|
|
|
|
import prism.PrismUtils; |
|
|
|
|
|
|
|
@ -35,10 +39,10 @@ import prism.PrismUtils; |
|
|
|
* Explicit representation of a probability distribution. |
|
|
|
* Basically, a mapping from (integer-valued) state indices to (non-zero, double-valued) probabilities. |
|
|
|
*/ |
|
|
|
public class Distribution implements Iterable<Entry<Integer,Double>> |
|
|
|
public class Distribution implements Iterable<Entry<Integer, Double>> |
|
|
|
{ |
|
|
|
private HashMap<Integer,Double> map; |
|
|
|
|
|
|
|
private HashMap<Integer, Double> map; |
|
|
|
|
|
|
|
/** |
|
|
|
* Create an empty distribution. |
|
|
|
*/ |
|
|
|
@ -46,20 +50,20 @@ public class Distribution implements Iterable<Entry<Integer,Double>> |
|
|
|
{ |
|
|
|
clear(); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
* Copy constructor. |
|
|
|
*/ |
|
|
|
public Distribution(Distribution distr) |
|
|
|
{ |
|
|
|
this(); |
|
|
|
Iterator<Entry<Integer,Double>> i = distr.iterator(); |
|
|
|
Iterator<Entry<Integer, Double>> i = distr.iterator(); |
|
|
|
while (i.hasNext()) { |
|
|
|
Map.Entry<Integer,Double> e = i.next(); |
|
|
|
Map.Entry<Integer, Double> e = i.next(); |
|
|
|
add(e.getKey(), e.getValue()); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
* Construct a distribution from an existing one and a state index permutation, |
|
|
|
* i.e. in which state index i becomes index permut[i]. |
|
|
|
@ -69,21 +73,21 @@ public class Distribution implements Iterable<Entry<Integer,Double>> |
|
|
|
public Distribution(Distribution distr, int permut[]) |
|
|
|
{ |
|
|
|
this(); |
|
|
|
Iterator<Entry<Integer,Double>> i = distr.iterator(); |
|
|
|
Iterator<Entry<Integer, Double>> i = distr.iterator(); |
|
|
|
while (i.hasNext()) { |
|
|
|
Map.Entry<Integer,Double> e = i.next(); |
|
|
|
Map.Entry<Integer, Double> e = i.next(); |
|
|
|
add(permut[e.getKey()], e.getValue()); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
* Clear all entries of the distribution. |
|
|
|
*/ |
|
|
|
public void clear() |
|
|
|
{ |
|
|
|
map = new HashMap<Integer,Double>(); |
|
|
|
map = new HashMap<Integer, Double>(); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
* Add 'prob' to the probability for state 'j'. |
|
|
|
* Return boolean indicating whether or not there was already |
|
|
|
@ -119,9 +123,9 @@ public class Distribution implements Iterable<Entry<Integer,Double>> |
|
|
|
{ |
|
|
|
Double d; |
|
|
|
d = (Double) map.get(j); |
|
|
|
return d==null ? 0.0 : d.doubleValue(); |
|
|
|
return d == null ? 0.0 : d.doubleValue(); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
* Returns true if index j is in the support of the distribution. |
|
|
|
*/ |
|
|
|
@ -129,35 +133,35 @@ public class Distribution implements Iterable<Entry<Integer,Double>> |
|
|
|
{ |
|
|
|
return map.get(j) != null; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
* Returns true if all indices in the support of the distribution are in the set. |
|
|
|
*/ |
|
|
|
public boolean isSubsetOf(BitSet set) |
|
|
|
{ |
|
|
|
Iterator<Entry<Integer,Double>> i = iterator(); |
|
|
|
Iterator<Entry<Integer, Double>> i = iterator(); |
|
|
|
while (i.hasNext()) { |
|
|
|
Map.Entry<Integer,Double> e = i.next(); |
|
|
|
Map.Entry<Integer, Double> e = i.next(); |
|
|
|
if (!set.get((Integer) e.getKey())) |
|
|
|
return false; |
|
|
|
} |
|
|
|
return true; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
* Returns true if at least one index in the support of the distribution is in the set. |
|
|
|
*/ |
|
|
|
public boolean containsOneOf(BitSet set) |
|
|
|
{ |
|
|
|
Iterator<Entry<Integer,Double>> i = iterator(); |
|
|
|
Iterator<Entry<Integer, Double>> i = iterator(); |
|
|
|
while (i.hasNext()) { |
|
|
|
Map.Entry<Integer,Double> e = i.next(); |
|
|
|
Map.Entry<Integer, Double> e = i.next(); |
|
|
|
if (set.get((Integer) e.getKey())) |
|
|
|
return true; |
|
|
|
} |
|
|
|
return false; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
* Get the support of the distribution. |
|
|
|
*/ |
|
|
|
@ -165,11 +169,11 @@ public class Distribution implements Iterable<Entry<Integer,Double>> |
|
|
|
{ |
|
|
|
return map.keySet(); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
* Get an iterator over the entries of the map defining the distribution. |
|
|
|
*/ |
|
|
|
public Iterator<Entry<Integer,Double>> iterator() |
|
|
|
public Iterator<Entry<Integer, Double>> iterator() |
|
|
|
{ |
|
|
|
return map.entrySet().iterator(); |
|
|
|
} |
|
|
|
@ -181,7 +185,7 @@ public class Distribution implements Iterable<Entry<Integer,Double>> |
|
|
|
{ |
|
|
|
return map.isEmpty(); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
* Get the size of the support of the distribution. |
|
|
|
*/ |
|
|
|
@ -189,36 +193,36 @@ public class Distribution implements Iterable<Entry<Integer,Double>> |
|
|
|
{ |
|
|
|
return map.size(); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
* Get the sum of the probabilities in the distribution. |
|
|
|
*/ |
|
|
|
public double sum() |
|
|
|
{ |
|
|
|
double d = 0.0; |
|
|
|
Iterator<Entry<Integer,Double>> i = iterator(); |
|
|
|
Iterator<Entry<Integer, Double>> i = iterator(); |
|
|
|
while (i.hasNext()) { |
|
|
|
Map.Entry<Integer,Double> e = i.next(); |
|
|
|
Map.Entry<Integer, Double> e = i.next(); |
|
|
|
d += e.getValue(); |
|
|
|
} |
|
|
|
return d; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
* Get the sum of all the probabilities in the distribution except for state j. |
|
|
|
*/ |
|
|
|
public double sumAllBut(int j) |
|
|
|
{ |
|
|
|
double d = 0.0; |
|
|
|
Iterator<Entry<Integer,Double>> i = iterator(); |
|
|
|
Iterator<Entry<Integer, Double>> i = iterator(); |
|
|
|
while (i.hasNext()) { |
|
|
|
Map.Entry<Integer,Double> e = i.next(); |
|
|
|
Map.Entry<Integer, Double> e = i.next(); |
|
|
|
if (e.getKey() != j) |
|
|
|
d += e.getValue(); |
|
|
|
} |
|
|
|
return d; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
* Create a new distribution, based on a mapping from the state indices |
|
|
|
* used in this distribution to a different set of state indices. |
|
|
|
@ -226,14 +230,14 @@ public class Distribution implements Iterable<Entry<Integer,Double>> |
|
|
|
public Distribution map(int map[]) |
|
|
|
{ |
|
|
|
Distribution distrNew = new Distribution(); |
|
|
|
Iterator<Entry<Integer,Double>> i = iterator(); |
|
|
|
Iterator<Entry<Integer, Double>> i = iterator(); |
|
|
|
while (i.hasNext()) { |
|
|
|
Map.Entry<Integer,Double> e = i.next(); |
|
|
|
Map.Entry<Integer, Double> e = i.next(); |
|
|
|
distrNew.add(map[e.getKey()], e.getValue()); |
|
|
|
} |
|
|
|
return distrNew; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
public boolean equals(Object o) |
|
|
|
{ |
|
|
|
@ -241,9 +245,9 @@ public class Distribution implements Iterable<Entry<Integer,Double>> |
|
|
|
Distribution d = (Distribution) o; |
|
|
|
if (d.size() != size()) |
|
|
|
return false; |
|
|
|
Iterator<Entry<Integer,Double>> i = iterator(); |
|
|
|
Iterator<Entry<Integer, Double>> i = iterator(); |
|
|
|
while (i.hasNext()) { |
|
|
|
Map.Entry<Integer,Double> e = i.next(); |
|
|
|
Map.Entry<Integer, Double> e = i.next(); |
|
|
|
d1 = e.getValue(); |
|
|
|
d2 = d.map.get(e.getKey()); |
|
|
|
if (d2 == null || !PrismUtils.doublesAreClose(d1, d2, 1e-12, false)) |
|
|
|
@ -251,7 +255,7 @@ public class Distribution implements Iterable<Entry<Integer,Double>> |
|
|
|
} |
|
|
|
return true; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
public int hashCode() |
|
|
|
{ |
|
|
|
|