Skip to content

Commit

Permalink
CH interface #55 and Reduced hull efficient
Browse files Browse the repository at this point in the history
  • Loading branch information
rcabanasdepaz committed Feb 9, 2021
1 parent 4e8d97a commit a5e31e9
Show file tree
Hide file tree
Showing 5 changed files with 1,315 additions and 1,182 deletions.
75 changes: 47 additions & 28 deletions src/main/java/ch/idsia/crema/factor/credal/vertex/VertexFactor.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import ch.idsia.crema.utility.IndexIterator;
import ch.idsia.crema.utility.RandomUtil;
import ch.idsia.crema.utility.SeparateIndexIterator;
import ch.idsia.crema.utility.hull.ConvexHull;
import ch.idsia.crema.utility.hull.LPConvexHull;
import com.google.common.primitives.Doubles;
import com.google.common.primitives.Ints;
Expand Down Expand Up @@ -42,7 +43,7 @@ public class VertexFactor implements CredalFactor, SeparatelySpecified<VertexFac

private final double[][][] data;

public static boolean CONVEX_HULL_MARG = false;
private static ConvexHull.Method CONVEX_HULL_MARG = null;

public VertexFactor(Strides left, Strides right) {
this.separatedDomain = right;
Expand Down Expand Up @@ -297,8 +298,8 @@ public VertexFactor marginalize(int... vars) {

VertexFactor f = new VertexFactor(left, getSeparatingDomain(), target_data);

if (CONVEX_HULL_MARG)
f.applyConvexHull(true);
if (CONVEX_HULL_MARG != null)
f.applyConvexHull(CONVEX_HULL_MARG);

return f;
}
Expand Down Expand Up @@ -415,7 +416,8 @@ public VertexFactor combine(VertexFactor other) {
}
}

return new VertexFactor(left, right, target_data);
VertexFactor f = new VertexFactor(left, right, target_data);
return f;
}

/**
Expand Down Expand Up @@ -505,6 +507,19 @@ public double[][] getVerticesAt(int i) {
return data[i];
}

/**
* Set the convexhull method applied after marginalization. None by default.
*
* @param convexHullMarg
*/
public static void setConvexHullMarg(ConvexHull.Method convexHullMarg) {
CONVEX_HULL_MARG = convexHullMarg;
}

public static ConvexHull.Method getConvexHullMarg() {
return CONVEX_HULL_MARG;
}

/**
* Static method that builds a deterministic factor (values can only be ones or zeros).
* Thus, children variables are determined by the values of the parents
Expand Down Expand Up @@ -600,15 +615,17 @@ public BayesianFactor sampleVertex() {
return new BayesianFactor(newDomain, data);
}

public void applyConvexHull(boolean simplex) {
for (int i = 0; i < this.getSeparatingDomain().getCombinations(); i++) {
data[i] = LPConvexHull.compute(data[i]);
public void applyConvexHull(ConvexHull.Method m) {
if (m != null) {
for (int i = 0; i < this.getSeparatingDomain().getCombinations(); i++) {
data[i] = ConvexHull.as(m).apply(data[i]);
}
}
}

public VertexFactor convexHull(boolean simplex) {
public VertexFactor convexHull(ConvexHull.Method m) {
VertexFactor f = this.copy();
f.applyConvexHull(simplex);
f.applyConvexHull(m);
return f;
}

Expand Down Expand Up @@ -713,22 +730,23 @@ public static GraphicalModel<VertexFactor> buildModel(boolean convexHull, Bayesi
.map(m -> new BayesianToVertex().apply(m.getFactor(v), v))
.toArray(VertexFactor[]::new));
if (convexHull)
f = f.convexHull(true);
f = f.convexHull(ConvexHull.Method.DEFAULT);

vmodel.setFactor(v, f);
}
return vmodel;
}

/**
* Method for generating a random VertexFactor of a conditional credal set.
* @param leftDomain: strides of the conditionated variables.
* @param rightDomain: strides of the conditioning variables.
* @param k: number of vertices
* @param num_decimals: number of decimals in the probability values.
* @param zero_allowed: flag to control if random probabilities can be zero.
* @return
*/
/**
* Method for generating a random VertexFactor of a conditional credal set.
*
* @param leftDomain: strides of the conditionated variables.
* @param rightDomain: strides of the conditioning variables.
* @param k: number of vertices
* @param num_decimals: number of decimals in the probability values.
* @param zero_allowed: flag to control if random probabilities can be zero.
* @return
*/
public static VertexFactor random(Strides leftDomain, Strides rightDomain, int k, int num_decimals, boolean zero_allowed) {
// array for storing the vertices
double data[][][] = new double[rightDomain.getCombinations()][][];
Expand All @@ -739,14 +757,15 @@ public static VertexFactor random(Strides leftDomain, Strides rightDomain, int k
return new VertexFactor(leftDomain, rightDomain, data);
}

/**
* Method for generating a random VertexFactor of a joint credal set.
* @param leftDomain: strides of the variables.
* @param k: number of vertices
* @param num_decimals: number of decimals in the probability values.
* @param zero_allowed: flag to control if random probabilities can be zero.
* @return
*/
/**
* Method for generating a random VertexFactor of a joint credal set.
*
* @param leftDomain: strides of the variables.
* @param k: number of vertices
* @param num_decimals: number of decimals in the probability values.
* @param zero_allowed: flag to control if random probabilities can be zero.
* @return
*/
public static VertexFactor random(Strides leftDomain, int k, int num_decimals, boolean zero_allowed) {

if (leftDomain.getVariables().length > 1)
Expand Down Expand Up @@ -777,7 +796,7 @@ public static VertexFactor random(Strides leftDomain, int k, int num_decimals, b
// merge all the PMFs
out = VertexFactor
.mergeVertices((VertexFactor[]) PMFs.toArray(VertexFactor[]::new))
.convexHull(true);
.convexHull(ConvexHull.Method.DEFAULT);

} while (out.getVerticesAt(0).length < k);
return out;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,62 +6,78 @@
import ch.idsia.crema.model.graphical.GraphicalModel;
import ch.idsia.crema.preprocess.CutObserved;
import ch.idsia.crema.preprocess.RemoveBarren;
import ch.idsia.crema.utility.hull.ConvexHull;
import gnu.trove.map.TIntIntMap;
import gnu.trove.map.hash.TIntIntHashMap;
import org.apache.commons.lang3.ArrayUtils;


public class CredalVariableElimination<M extends GraphicalModel<VertexFactor>> implements Inference<M, VertexFactor> {

private final M model;
private final M model;

public CredalVariableElimination(M model) {
this.model = model;
}
private ConvexHull.Method convexHullMarg = null;

@Override
public M getInferenceModel(int target, TIntIntMap evidence) {
CutObserved cutObserved = new CutObserved();
// run making a copy of the model
M infModel = cutObserved.execute(model, evidence);
public CredalVariableElimination(M model) {
this.model = model;
}

RemoveBarren removeBarren = new RemoveBarren();
// no more need to make a copy of the model
removeBarren.executeInline(infModel, target, evidence);

return infModel;
}
@Override
public M getInferenceModel(int target, TIntIntMap evidence) {
CutObserved cutObserved = new CutObserved();
// run making a copy of the model
M infModel = cutObserved.execute(model, evidence);

/**
* Query K(target|evidence) in the model provided to the constructor
*
* @param target int the target variable
* @param evidence {@link TIntIntMap} a map of evidence in the form variable-state
* @return
*/
public VertexFactor query(int target, TIntIntMap evidence) {
M infModel = getInferenceModel(target, evidence);
RemoveBarren removeBarren = new RemoveBarren();
// no more need to make a copy of the model
removeBarren.executeInline(infModel, target, evidence);

TIntIntMap filteredEvidence = new TIntIntHashMap(evidence);
return infModel;
}

// update the evidence
for (int v : evidence.keys()) {
if (ArrayUtils.contains(infModel.getVariables(), v)) {
filteredEvidence.put(v, evidence.get(v));
}
}
/**
* Query K(target|evidence) in the model provided to the constructor
*
* @param target int the target variable
* @param evidence {@link TIntIntMap} a map of evidence in the form variable-state
* @return
*/
public VertexFactor query(int target, TIntIntMap evidence) {
M infModel = getInferenceModel(target, evidence);

MinFillOrdering minfill = new MinFillOrdering();
int[] order = minfill.apply(infModel);
TIntIntMap filteredEvidence = new TIntIntHashMap(evidence);

FactorVariableElimination<VertexFactor> ve = new FactorVariableElimination<>(order);
ve.setEvidence(filteredEvidence);
ve.setFactors(infModel.getFactors());
ve.setNormalize(false);
// update the evidence
for (int v : evidence.keys()) {
if (ArrayUtils.contains(infModel.getVariables(), v)) {
filteredEvidence.put(v, evidence.get(v));
}
}

VertexFactor output = ve.run(target);
MinFillOrdering minfill = new MinFillOrdering();
int[] order = minfill.apply(infModel);

FactorVariableElimination<VertexFactor> ve = new FactorVariableElimination<>(order);
ve.setEvidence(filteredEvidence);
ve.setFactors(infModel.getFactors());
ve.setNormalize(false);

// Set the convex hull method
ConvexHull.Method old_method = VertexFactor.getConvexHullMarg();
VertexFactor.setConvexHullMarg(convexHullMarg);
// run the query
VertexFactor output = ve.run(target);

// restore the previous convex hull method
VertexFactor.setConvexHullMarg(old_method);

return output.normalize();
}

public void setConvexHullMarg(ConvexHull.Method convexHullMarg) {
this.convexHullMarg = convexHullMarg;
}

return output.normalize();
}

}
Loading

2 comments on commit a5e31e9

@cbonesana
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rcabanasdepaz what happened with src/main/java/ch/idsia/crema/utility/ArraysUtil.java ? Did you rewrite the whole file?!!!

On second thought, it looks like you are writing using spaces instead of tabs. The whole library is using tabs...

@rcabanasdepaz
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In ArraysUtil I just added the method for the symmetric difference. I also run the code auto-style what seems to have changed the tabs thing. I will fix it in the next commit.

Please sign in to comment.