|
|
@ -26,8 +26,12 @@ |
|
|
|
|
|
|
|
|
package explicit; |
|
|
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.Map.Entry; |
|
|
|
|
|
import java.util.Set; |
|
|
|
|
|
|
|
|
import prism.PrismUtils; |
|
|
import prism.PrismUtils; |
|
|
|
|
|
|
|
|
@ -35,9 +39,9 @@ import prism.PrismUtils; |
|
|
* Explicit representation of a probability distribution. |
|
|
* Explicit representation of a probability distribution. |
|
|
* Basically, a mapping from (integer-valued) state indices to (non-zero, double-valued) probabilities. |
|
|
* 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. |
|
|
* Create an empty distribution. |
|
|
@ -53,9 +57,9 @@ public class Distribution implements Iterable<Entry<Integer,Double>> |
|
|
public Distribution(Distribution distr) |
|
|
public Distribution(Distribution distr) |
|
|
{ |
|
|
{ |
|
|
this(); |
|
|
this(); |
|
|
Iterator<Entry<Integer,Double>> i = distr.iterator(); |
|
|
|
|
|
|
|
|
Iterator<Entry<Integer, Double>> i = distr.iterator(); |
|
|
while (i.hasNext()) { |
|
|
while (i.hasNext()) { |
|
|
Map.Entry<Integer,Double> e = i.next(); |
|
|
|
|
|
|
|
|
Map.Entry<Integer, Double> e = i.next(); |
|
|
add(e.getKey(), e.getValue()); |
|
|
add(e.getKey(), e.getValue()); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
@ -69,9 +73,9 @@ public class Distribution implements Iterable<Entry<Integer,Double>> |
|
|
public Distribution(Distribution distr, int permut[]) |
|
|
public Distribution(Distribution distr, int permut[]) |
|
|
{ |
|
|
{ |
|
|
this(); |
|
|
this(); |
|
|
Iterator<Entry<Integer,Double>> i = distr.iterator(); |
|
|
|
|
|
|
|
|
Iterator<Entry<Integer, Double>> i = distr.iterator(); |
|
|
while (i.hasNext()) { |
|
|
while (i.hasNext()) { |
|
|
Map.Entry<Integer,Double> e = i.next(); |
|
|
|
|
|
|
|
|
Map.Entry<Integer, Double> e = i.next(); |
|
|
add(permut[e.getKey()], e.getValue()); |
|
|
add(permut[e.getKey()], e.getValue()); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
@ -81,7 +85,7 @@ public class Distribution implements Iterable<Entry<Integer,Double>> |
|
|
*/ |
|
|
*/ |
|
|
public void clear() |
|
|
public void clear() |
|
|
{ |
|
|
{ |
|
|
map = new HashMap<Integer,Double>(); |
|
|
|
|
|
|
|
|
map = new HashMap<Integer, Double>(); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
@ -119,7 +123,7 @@ public class Distribution implements Iterable<Entry<Integer,Double>> |
|
|
{ |
|
|
{ |
|
|
Double d; |
|
|
Double d; |
|
|
d = (Double) map.get(j); |
|
|
d = (Double) map.get(j); |
|
|
return d==null ? 0.0 : d.doubleValue(); |
|
|
|
|
|
|
|
|
return d == null ? 0.0 : d.doubleValue(); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
@ -135,9 +139,9 @@ public class Distribution implements Iterable<Entry<Integer,Double>> |
|
|
*/ |
|
|
*/ |
|
|
public boolean isSubsetOf(BitSet set) |
|
|
public boolean isSubsetOf(BitSet set) |
|
|
{ |
|
|
{ |
|
|
Iterator<Entry<Integer,Double>> i = iterator(); |
|
|
|
|
|
|
|
|
Iterator<Entry<Integer, Double>> i = iterator(); |
|
|
while (i.hasNext()) { |
|
|
while (i.hasNext()) { |
|
|
Map.Entry<Integer,Double> e = i.next(); |
|
|
|
|
|
|
|
|
Map.Entry<Integer, Double> e = i.next(); |
|
|
if (!set.get((Integer) e.getKey())) |
|
|
if (!set.get((Integer) e.getKey())) |
|
|
return false; |
|
|
return false; |
|
|
} |
|
|
} |
|
|
@ -149,9 +153,9 @@ public class Distribution implements Iterable<Entry<Integer,Double>> |
|
|
*/ |
|
|
*/ |
|
|
public boolean containsOneOf(BitSet set) |
|
|
public boolean containsOneOf(BitSet set) |
|
|
{ |
|
|
{ |
|
|
Iterator<Entry<Integer,Double>> i = iterator(); |
|
|
|
|
|
|
|
|
Iterator<Entry<Integer, Double>> i = iterator(); |
|
|
while (i.hasNext()) { |
|
|
while (i.hasNext()) { |
|
|
Map.Entry<Integer,Double> e = i.next(); |
|
|
|
|
|
|
|
|
Map.Entry<Integer, Double> e = i.next(); |
|
|
if (set.get((Integer) e.getKey())) |
|
|
if (set.get((Integer) e.getKey())) |
|
|
return true; |
|
|
return true; |
|
|
} |
|
|
} |
|
|
@ -169,7 +173,7 @@ public class Distribution implements Iterable<Entry<Integer,Double>> |
|
|
/** |
|
|
/** |
|
|
* Get an iterator over the entries of the map defining the distribution. |
|
|
* 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(); |
|
|
return map.entrySet().iterator(); |
|
|
} |
|
|
} |
|
|
@ -196,9 +200,9 @@ public class Distribution implements Iterable<Entry<Integer,Double>> |
|
|
public double sum() |
|
|
public double sum() |
|
|
{ |
|
|
{ |
|
|
double d = 0.0; |
|
|
double d = 0.0; |
|
|
Iterator<Entry<Integer,Double>> i = iterator(); |
|
|
|
|
|
|
|
|
Iterator<Entry<Integer, Double>> i = iterator(); |
|
|
while (i.hasNext()) { |
|
|
while (i.hasNext()) { |
|
|
Map.Entry<Integer,Double> e = i.next(); |
|
|
|
|
|
|
|
|
Map.Entry<Integer, Double> e = i.next(); |
|
|
d += e.getValue(); |
|
|
d += e.getValue(); |
|
|
} |
|
|
} |
|
|
return d; |
|
|
return d; |
|
|
@ -210,9 +214,9 @@ public class Distribution implements Iterable<Entry<Integer,Double>> |
|
|
public double sumAllBut(int j) |
|
|
public double sumAllBut(int j) |
|
|
{ |
|
|
{ |
|
|
double d = 0.0; |
|
|
double d = 0.0; |
|
|
Iterator<Entry<Integer,Double>> i = iterator(); |
|
|
|
|
|
|
|
|
Iterator<Entry<Integer, Double>> i = iterator(); |
|
|
while (i.hasNext()) { |
|
|
while (i.hasNext()) { |
|
|
Map.Entry<Integer,Double> e = i.next(); |
|
|
|
|
|
|
|
|
Map.Entry<Integer, Double> e = i.next(); |
|
|
if (e.getKey() != j) |
|
|
if (e.getKey() != j) |
|
|
d += e.getValue(); |
|
|
d += e.getValue(); |
|
|
} |
|
|
} |
|
|
@ -226,9 +230,9 @@ public class Distribution implements Iterable<Entry<Integer,Double>> |
|
|
public Distribution map(int map[]) |
|
|
public Distribution map(int map[]) |
|
|
{ |
|
|
{ |
|
|
Distribution distrNew = new Distribution(); |
|
|
Distribution distrNew = new Distribution(); |
|
|
Iterator<Entry<Integer,Double>> i = iterator(); |
|
|
|
|
|
|
|
|
Iterator<Entry<Integer, Double>> i = iterator(); |
|
|
while (i.hasNext()) { |
|
|
while (i.hasNext()) { |
|
|
Map.Entry<Integer,Double> e = i.next(); |
|
|
|
|
|
|
|
|
Map.Entry<Integer, Double> e = i.next(); |
|
|
distrNew.add(map[e.getKey()], e.getValue()); |
|
|
distrNew.add(map[e.getKey()], e.getValue()); |
|
|
} |
|
|
} |
|
|
return distrNew; |
|
|
return distrNew; |
|
|
@ -241,9 +245,9 @@ public class Distribution implements Iterable<Entry<Integer,Double>> |
|
|
Distribution d = (Distribution) o; |
|
|
Distribution d = (Distribution) o; |
|
|
if (d.size() != size()) |
|
|
if (d.size() != size()) |
|
|
return false; |
|
|
return false; |
|
|
Iterator<Entry<Integer,Double>> i = iterator(); |
|
|
|
|
|
|
|
|
Iterator<Entry<Integer, Double>> i = iterator(); |
|
|
while (i.hasNext()) { |
|
|
while (i.hasNext()) { |
|
|
Map.Entry<Integer,Double> e = i.next(); |
|
|
|
|
|
|
|
|
Map.Entry<Integer, Double> e = i.next(); |
|
|
d1 = e.getValue(); |
|
|
d1 = e.getValue(); |
|
|
d2 = d.map.get(e.getKey()); |
|
|
d2 = d.map.get(e.getKey()); |
|
|
if (d2 == null || !PrismUtils.doublesAreClose(d1, d2, 1e-12, false)) |
|
|
if (d2 == null || !PrismUtils.doublesAreClose(d1, d2, 1e-12, false)) |
|
|
|