use of com.sri.ai.praise.core.representation.interfacebased.factor.core.table.TableFactor in project aic-praise by aic-sri-international.
the class RandomTableFactorMaker method makeRandomTableFactor.
public static TableFactor makeRandomTableFactor(SpecsForRandomTableFactorGeneration specs, Function<Integer, String> fromVariableIndexToName, Random random) {
ArrayList<TableVariable> variables = makeVariables(specs.cardinalities, fromVariableIndexToName);
ArrayList<Double> entries = makeUniformlyDistributedRandomEntries(specs, random);
TableFactor tableFactor = new TableFactor(variables, entries);
return tableFactor;
}
use of com.sri.ai.praise.core.representation.interfacebased.factor.core.table.TableFactor 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.table.TableFactor 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.table.TableFactor in project aic-praise by aic-sri-international.
the class TestCases method main.
public static void main(String[] args) {
List<TableFactor> fact = treeWithFixedEntries(5, 2, arrayList(1., 2., 3., 4., 5., 6., 7., 8.), arrayList(1., 1.));
for (TableFactor f : fact) {
println(f);
}
fact = treeWithGaussianRandomEntries(5, 2, 2, 0., 3.);
for (TableFactor f : fact) {
println(f);
}
/*File file = retrieveUAIFilesInFolder("promedas").get(0);
String name = file.getName();
println(name);
ArrayList<TableFactor> factors = getListOfTableFactors("promedas", name);
for(TableFactor f : factors) {
println(f);
}
try {
FileReader modelFile = new FileReader(file);
UAIModel model = UAIModelReader.read(modelFile);
println(model.getEvidence());
ArrayList<TableFactor> factors2 = uaiModelToListOfTableFactors(model );
for(TableFactor f : factors2) {
println(f);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
println("ed");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
println("ed");
}
*/
}
use of com.sri.ai.praise.core.representation.interfacebased.factor.core.table.TableFactor in project aic-praise by aic-sri-international.
the class TestCases method tableFactorIsingModel.
private static ArrayList<TableFactor> tableFactorIsingModel(int gridSize, BiFunction<Pair<Integer, Integer>, Pair<Integer, Integer>, ArrayList<Double>> pairwiseFactorentries, Function<Pair<Integer, Integer>, ArrayList<Double>> singleVariableFactorEntries) {
ArrayList<ArrayList<TableVariable>> variables = new ArrayList<>();
for (int i = 0; i < gridSize; i++) {
ArrayList<TableVariable> col = new ArrayList<>();
variables.add(col);
for (int j = 0; j < gridSize; j++) {
col.add(j, new TableVariable("A_" + i + "_" + j, 2));
}
}
ArrayList<TableFactor> result = new ArrayList<>();
for (int i = 0; i < gridSize - 1; i++) {
for (int j = 0; j < gridSize; j++) {
result.add(new TableFactor(arrayList(variables.get(i).get(j), variables.get(i + 1).get(j)), pairwiseFactorentries.apply(new Pair<>(i, j), new Pair<>(i + 1, j))));
}
}
for (int i = 0; i < gridSize; i++) {
for (int j = 0; j < gridSize - 1; j++) {
result.add(new TableFactor(arrayList(variables.get(i).get(j), variables.get(i).get(j + 1)), pairwiseFactorentries.apply(new Pair<>(i, j), new Pair<>(i, j + 1))));
}
}
if (!(singleVariableFactorEntries.apply(new Pair<>(0, 0)) == null)) {
for (int i = 0; i < gridSize; i++) {
for (int j = 0; j < gridSize; j++) {
result.add(new TableFactor(arrayList(variables.get(i).get(j)), singleVariableFactorEntries.apply(new Pair<>(i, j))));
}
}
}
return result;
}
Aggregations