use of com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.api.ExpressionFactor in project aic-praise by aic-sri-international.
the class UAIModelToExpressionFactorNetwork method main.
public static void main(String[] args) {
try {
// Importing the file and reading it
FileReader modelFile = new FileReader(new File("").getAbsolutePath() + "/UAITests/BN_0.uai");
UAIModel model = UAIModelReader.read(modelFile);
// Converting the network
ExpressionFactorNetwork network = convert(model, null);
// Printing the factors
for (IdentityWrapper<Factor> fwrapped : network.getAs()) {
ExpressionFactor f = (ExpressionFactor) fwrapped.getObject();
println(f);
}
/*// This seems to be OK! But when we analyze the connections between the factors:
IdentityWrapper<Factor> f = network.getAs().iterator().next();
println("Printing one of the factors of the network:\n\t "+f);
println("Printing this factor's connections:\n\t" + network.getBsOfA(f));
println("This shows that there is something wrong\n"
+ "In fact the connections in the graph are made based on the 'freeVariables' of a factor");
println("freeVariables of f: "+Expressions.freeVariables((ExpressionFactor)f.getObject(),((ExpressionFactor)f.getObject()).getContext()));
println("\nWe can check that those 'abnomalies' are indeed variables on the network:");
for(Variable v:network.getBs()) {
System.out.print(v + ", ");
}*/
} catch (IOException e) {
e.printStackTrace();
}
}
use of com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.api.ExpressionFactor in project aic-praise by aic-sri-international.
the class TestCases method gridModelWithRandomFactors.
/**
* An Ising model is a N dimensional lattice (like a N-dimensional grid), where each node interact with its nearest neighbors.
* Each node can assume the values +1 or -1, and has an index "i" associated to its position. We usually represent the node
* at position i by <math>\sigma_i</math>. The indexes are usually given so that sigma 0 is in teh center of the hyper-cube
* <p>
* If N = 2, the Ising model is simply a squared grid. Below we represent a 3X3 s dimension Ising model
* <p>
*
* <table style="width:10%">
* <tr>
* <th>sig2</th><th>--</th><th>sig3 </th><th>--</th><th>sig4</th>
* </tr>
* <tr>
* <th>|</th><th> </th><th>|</th><th> </th><th>|</th>
* </tr>
* <tr>
* <th>sig-1</th><th>--</th><th>sig0</th><th>--</th><th>sig1</th>
* </tr>
* <tr>
* <th>|</th><th> </th><th>|</th><th> </th><th>|</th>
* </tr>
* <tr>
* <th>sig-4</th><th>--</th><th>sig-3</th><th>--</th><th>sig-2</th>
* </tr>
* </table>
*
* <p>
* If N = 1, the model is a line (sig1 -- sig2 -- sig3 -- sig4 -- sig5 ...)<p>
*
* we define <math>\sigma = (\sigma_1,\sigma_2,...,\sigma_n)</math>.<p>
*
* The Ising model is represented by the following equation:<p>
*
* :<math>\tilde{P}(\sigma) = exp(-\beta H(\sigma)) </math><p>
*
* Where beta is the POTENTIAL<p>
* :<math>H(\sigma) = - \sum_{\langle i~j\rangle} J_{ij} \sigma_i \sigma_j -\mu \sum_{j} h_j\sigma_j</math>
*
* Simplifications usually consider <math> J_{ij} = \mu_j = 1 </math>. That way, the grid model can be represented in the following way: <p>
*
* :<math>\tilde{P}(\sigma) = (\prod_{<ij>}\phi(\sigma_i,\sigma_j))(\prod_i\phi'(\simga_i)) </math>
*
* Where <ij> mean the set of (i,j) that are directly neighbors, and the factors are defined as follows:<p>
*
* :<math>\phi(X,Y)= exp(\beta X Y),\phi'(X) = exp(h X) </math>
*
* If we don't consider the simplification, the network correspond to a markov random field grid with arbitrary factors
*
* <p>-----------------------------------------------------------------------------------<p>
* Important results about the Ising model:<p>
*
* - Suppose we take as evidence that all the nodes in the frontier of the lattice (surface of the hypercube)
* are equal +1. There exists a <math>\beta_c</math>, such that,
* <math>P(\sigma_0 = True) > \alpha > 0.5</math> for ARBITRARYLY LARGE number of odes on the lattice <p>
*
* - This means that AEBP is going to converge to an interval of size 2*alpha, and then suddenly drops to \ero in the frontier;
*
* @param gridSize : gridSize X gridSize is the dimension of the grid
* @param potential : Beta, Inverse temperature
* @param weight: Theta
* @return
*/
public static List<? extends Factor> gridModelWithRandomFactors(int gridSize, boolean TableOrExpression) {
Random randomGenerator = new Random();
BiFunction<Pair<Integer, Integer>, Pair<Integer, Integer>, ArrayList<Double>> entries = (i, j) -> arrayList(0.001 * randomGenerator.nextInt(1000), 0.001 * randomGenerator.nextInt(1000), 0.001 * randomGenerator.nextInt(1000), 0.001 * randomGenerator.nextInt(1000));
if (TableOrExpression) {
ArrayList<TableFactor> result = tableFactorIsingModel(gridSize, entries, (i) -> null);
return result;
} else {
ArrayList<ExpressionFactor> result = expressionFactorIsingModel(gridSize, entries, (i) -> null);
return result;
}
}
use of com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.api.ExpressionFactor in project aic-praise by aic-sri-international.
the class TestCases method isingModelGridWithWeigthsAndPotetialNormalyDistributed.
/*tilde(P)(\sigma) = \frac{1}{Z} exp(\sum_{i}\theta_i \sigma_i + \sum_{<< i j >>}J_{i,j}\sigma_i\sigma_j),
* <p>
* where \sigma_i \in \{+1,-1\}, J_{i,j},\theta{i} ~ N(0,\beta^2)
* @param beta
*/
public static List<? extends Factor> isingModelGridWithWeigthsAndPotetialNormalyDistributed(int gridSize, double beta, boolean TableOrExpression) {
Function<Double, ArrayList<Double>> JPotentialEntries = (J) -> arrayList(exp(J), exp(-1. * J), exp(-1. * J), exp(J));
Function<Double, ArrayList<Double>> thetaPotentialEntries = (theta) -> arrayList(exp(theta), exp(-1. * theta), exp(-1. * theta), exp(theta));
Random gen = new Random();
BiFunction<Pair<Integer, Integer>, Pair<Integer, Integer>, ArrayList<Double>> parwiseEntries = (i, j) -> JPotentialEntries.apply(gen.nextGaussian() * beta);
Function<Pair<Integer, Integer>, ArrayList<Double>> singleEntries = (i) -> thetaPotentialEntries.apply(gen.nextGaussian());
if (TableOrExpression) {
ArrayList<TableFactor> result = tableFactorIsingModel(gridSize, parwiseEntries, singleEntries);
return result;
} else {
ArrayList<ExpressionFactor> result = expressionFactorIsingModel(gridSize, parwiseEntries, singleEntries);
return result;
}
}
use of com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.api.ExpressionFactor in project aic-praise by aic-sri-international.
the class TestCases method expressionFactorIsingModel.
private static ArrayList<ExpressionFactor> expressionFactorIsingModel(int gridSize, BiFunction<Pair<Integer, Integer>, Pair<Integer, Integer>, ArrayList<Double>> pairwiseFactorentries, Function<Pair<Integer, Integer>, ArrayList<Double>> singleVariableFactorEntries) {
ArrayList<ArrayList<ExpressionVariable>> variables = new ArrayList<>();
Context context = new TrueContext(new CommonTheory());
for (int i = 0; i < gridSize; i++) {
ArrayList<ExpressionVariable> col = new ArrayList<>();
variables.add(col);
for (int j = 0; j < gridSize; j++) {
ExpressionVariable v = new DefaultExpressionVariable(makeSymbol("A_" + i + "_" + j));
col.add(j, v);
makeSymbol("A_" + i + "_" + j);
context = context.extendWithSymbolsAndTypes(v, parse("Boolean"));
}
}
ArrayList<ExpressionFactor> result = new ArrayList<>();
for (int i = 0; i < gridSize - 1; i++) {
for (int j = 0; j < gridSize; j++) {
// Expression entry1 = Expressions.makeSymbol(randomGenerator.nextInt(10000)*1./10000.);
ArrayList<Double> entryList = pairwiseFactorentries.apply(new Pair<>(i, j), new Pair<>(i + 1, j));
Expression entry1 = makeSymbol(entryList.get(0));
Expression entry2 = makeSymbol(entryList.get(1));
Expression entry3 = makeSymbol(entryList.get(2));
Expression entry4 = makeSymbol(entryList.get(3));
Expression X = variables.get(i).get(j);
Expression Y = variables.get(i + 1).get(j);
result.add(new DefaultExpressionFactor(apply(IF_THEN_ELSE, apply(EQUAL, X, makeSymbol("true")), apply(IF_THEN_ELSE, apply(EQUAL, Y, makeSymbol("true")), entry1, entry2), apply(IF_THEN_ELSE, apply(EQUAL, Y, makeSymbol("true")), entry3, entry4)), context));
}
}
for (int i = 0; i < gridSize; i++) {
for (int j = 0; j < gridSize - 1; j++) {
ArrayList<Double> entryList = pairwiseFactorentries.apply(new Pair<>(i, j), new Pair<>(i, j + 1));
Expression entry1 = makeSymbol(entryList.get(0));
Expression entry2 = makeSymbol(entryList.get(1));
Expression entry3 = makeSymbol(entryList.get(2));
Expression entry4 = makeSymbol(entryList.get(3));
Expression X = variables.get(i).get(j);
Expression Y = variables.get(i).get(j + 1);
result.add(new DefaultExpressionFactor(apply(IF_THEN_ELSE, apply(EQUAL, X, makeSymbol("true")), apply(IF_THEN_ELSE, apply(EQUAL, Y, makeSymbol("true")), entry1, entry2), apply(IF_THEN_ELSE, apply(EQUAL, Y, makeSymbol("true")), entry3, entry4)), context));
}
}
if (!(singleVariableFactorEntries.apply(new Pair<>(0, 0)) == null)) {
for (int i = 0; i < gridSize; i++) {
for (int j = 0; j < gridSize; j++) {
ArrayList<Double> entryList = singleVariableFactorEntries.apply(new Pair<>(i, j));
Expression entry1 = makeSymbol(entryList.get(0));
Expression entry2 = makeSymbol(entryList.get(1));
Expression X = variables.get(i).get(j);
result.add(new DefaultExpressionFactor(apply(IF_THEN_ELSE, apply(EQUAL, X, makeSymbol("true")), entry1, entry2), context));
}
}
}
return result;
}
use of com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.api.ExpressionFactor in project aic-praise by aic-sri-international.
the class TestCases method uaiModelToListOfExpressionFactors.
public static ArrayList<ExpressionFactor> uaiModelToListOfExpressionFactors(UAIModel model) {
ExpressionFactorNetwork net = UAIModelToExpressionFactorNetwork.convert(model);
ArrayList<ExpressionFactor> result = mapIntoArrayList(net.getAs(), (fwrapper) -> (ExpressionFactor) fwrapper.getObject());
return result;
}
Aggregations