Search in sources :

Example 46 with TableFactor

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;
}
Also used : TableFactor(com.sri.ai.praise.core.representation.interfacebased.factor.core.table.TableFactor) TableVariable(com.sri.ai.praise.core.representation.interfacebased.factor.core.table.TableVariable)

Example 47 with 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;
    }
}
Also used : BiFunction(java.util.function.BiFunction) ExpressionFactor(com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.api.ExpressionFactor) Util.mapIntoArrayList(com.sri.ai.util.Util.mapIntoArrayList) Random(java.util.Random) Expression(com.sri.ai.expresso.api.Expression) Factor(com.sri.ai.praise.core.representation.interfacebased.factor.api.Factor) ArrayList(java.util.ArrayList) EQUAL(com.sri.ai.grinder.library.FunctorConstants.EQUAL) UAIModel(com.sri.ai.praise.core.representation.classbased.table.core.uai.UAIModel) TableFactorNetwork(com.sri.ai.praise.core.representation.interfacebased.factor.core.table.TableFactorNetwork) Expressions.apply(com.sri.ai.expresso.helper.Expressions.apply) TrueContext(com.sri.ai.grinder.core.TrueContext) DefaultExpressionVariable(com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.core.DefaultExpressionVariable) Math.round(java.lang.Math.round) Expressions.parse(com.sri.ai.expresso.helper.Expressions.parse) Math.log(java.lang.Math.log) Context(com.sri.ai.grinder.api.Context) BigInteger(java.math.BigInteger) Double.max(java.lang.Double.max) Util.arrayList(com.sri.ai.util.Util.arrayList) ExpressionVariable(com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.api.ExpressionVariable) Pair(com.sri.ai.util.base.Pair) Math.exp(java.lang.Math.exp) CommonTheory(com.sri.ai.grinder.application.CommonTheory) Function(com.google.common.base.Function) UAIEvidenceReading(com.sri.ai.praise.core.representation.classbased.table.core.uai.parsing.UAIEvidenceReading) TableFactor(com.sri.ai.praise.core.representation.interfacebased.factor.core.table.TableFactor) IOException(java.io.IOException) IF_THEN_ELSE(com.sri.ai.grinder.library.FunctorConstants.IF_THEN_ELSE) File(java.io.File) FileNotFoundException(java.io.FileNotFoundException) TableVariable(com.sri.ai.praise.core.representation.interfacebased.factor.core.table.TableVariable) List(java.util.List) DefaultExpressionFactor(com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.core.DefaultExpressionFactor) Expressions.makeSymbol(com.sri.ai.expresso.helper.Expressions.makeSymbol) UAIModelReader(com.sri.ai.praise.core.representation.classbased.table.core.uai.parsing.UAIModelReader) Util(com.sri.ai.util.Util) ExpressionFactorNetwork(com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.core.ExpressionFactorNetwork) Util.println(com.sri.ai.util.Util.println) FileReader(java.io.FileReader) UAIModelToExpressionFactorNetwork(com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.core.UAIModelToExpressionFactorNetwork) ExpressionFactor(com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.api.ExpressionFactor) DefaultExpressionFactor(com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.core.DefaultExpressionFactor) TableFactor(com.sri.ai.praise.core.representation.interfacebased.factor.core.table.TableFactor) Random(java.util.Random) Util.mapIntoArrayList(com.sri.ai.util.Util.mapIntoArrayList) ArrayList(java.util.ArrayList) Pair(com.sri.ai.util.base.Pair)

Example 48 with 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 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;
    }
}
Also used : BiFunction(java.util.function.BiFunction) ExpressionFactor(com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.api.ExpressionFactor) Util.mapIntoArrayList(com.sri.ai.util.Util.mapIntoArrayList) Random(java.util.Random) Expression(com.sri.ai.expresso.api.Expression) Factor(com.sri.ai.praise.core.representation.interfacebased.factor.api.Factor) ArrayList(java.util.ArrayList) EQUAL(com.sri.ai.grinder.library.FunctorConstants.EQUAL) UAIModel(com.sri.ai.praise.core.representation.classbased.table.core.uai.UAIModel) TableFactorNetwork(com.sri.ai.praise.core.representation.interfacebased.factor.core.table.TableFactorNetwork) Expressions.apply(com.sri.ai.expresso.helper.Expressions.apply) TrueContext(com.sri.ai.grinder.core.TrueContext) DefaultExpressionVariable(com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.core.DefaultExpressionVariable) Math.round(java.lang.Math.round) Expressions.parse(com.sri.ai.expresso.helper.Expressions.parse) Math.log(java.lang.Math.log) Context(com.sri.ai.grinder.api.Context) BigInteger(java.math.BigInteger) Double.max(java.lang.Double.max) Util.arrayList(com.sri.ai.util.Util.arrayList) ExpressionVariable(com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.api.ExpressionVariable) Pair(com.sri.ai.util.base.Pair) Math.exp(java.lang.Math.exp) CommonTheory(com.sri.ai.grinder.application.CommonTheory) Function(com.google.common.base.Function) UAIEvidenceReading(com.sri.ai.praise.core.representation.classbased.table.core.uai.parsing.UAIEvidenceReading) TableFactor(com.sri.ai.praise.core.representation.interfacebased.factor.core.table.TableFactor) IOException(java.io.IOException) IF_THEN_ELSE(com.sri.ai.grinder.library.FunctorConstants.IF_THEN_ELSE) File(java.io.File) FileNotFoundException(java.io.FileNotFoundException) TableVariable(com.sri.ai.praise.core.representation.interfacebased.factor.core.table.TableVariable) List(java.util.List) DefaultExpressionFactor(com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.core.DefaultExpressionFactor) Expressions.makeSymbol(com.sri.ai.expresso.helper.Expressions.makeSymbol) UAIModelReader(com.sri.ai.praise.core.representation.classbased.table.core.uai.parsing.UAIModelReader) Util(com.sri.ai.util.Util) ExpressionFactorNetwork(com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.core.ExpressionFactorNetwork) Util.println(com.sri.ai.util.Util.println) FileReader(java.io.FileReader) UAIModelToExpressionFactorNetwork(com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.core.UAIModelToExpressionFactorNetwork) ExpressionFactor(com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.api.ExpressionFactor) DefaultExpressionFactor(com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.core.DefaultExpressionFactor) TableFactor(com.sri.ai.praise.core.representation.interfacebased.factor.core.table.TableFactor) Random(java.util.Random) Util.mapIntoArrayList(com.sri.ai.util.Util.mapIntoArrayList) ArrayList(java.util.ArrayList) Pair(com.sri.ai.util.base.Pair)

Example 49 with 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 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");
		}
		*/
}
Also used : TableFactor(com.sri.ai.praise.core.representation.interfacebased.factor.core.table.TableFactor)

Example 50 with 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 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;
}
Also used : TableFactor(com.sri.ai.praise.core.representation.interfacebased.factor.core.table.TableFactor) Util.mapIntoArrayList(com.sri.ai.util.Util.mapIntoArrayList) ArrayList(java.util.ArrayList) TableVariable(com.sri.ai.praise.core.representation.interfacebased.factor.core.table.TableVariable) Pair(com.sri.ai.util.base.Pair)

Aggregations

TableFactor (com.sri.ai.praise.core.representation.interfacebased.factor.core.table.TableFactor)52 TableVariable (com.sri.ai.praise.core.representation.interfacebased.factor.core.table.TableVariable)36 ArrayList (java.util.ArrayList)19 Test (org.junit.Test)11 Factor (com.sri.ai.praise.core.representation.interfacebased.factor.api.Factor)10 Util.mapIntoArrayList (com.sri.ai.util.Util.mapIntoArrayList)10 List (java.util.List)9 ExpressionFactor (com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.api.ExpressionFactor)8 TableFactor.copyToSubTableFactor (com.sri.ai.praise.core.representation.interfacebased.factor.core.table.TableFactor.copyToSubTableFactor)8 Pair (com.sri.ai.util.base.Pair)7 Variable (com.sri.ai.praise.core.representation.interfacebased.factor.api.Variable)6 TableFactorNetwork (com.sri.ai.praise.core.representation.interfacebased.factor.core.table.TableFactorNetwork)6 LinkedHashMap (java.util.LinkedHashMap)6 Expression (com.sri.ai.expresso.api.Expression)5 DefaultExpressionFactor (com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.core.DefaultExpressionFactor)5 Util (com.sri.ai.util.Util)5 Util.arrayList (com.sri.ai.util.Util.arrayList)5 Util.println (com.sri.ai.util.Util.println)5 BiFunction (java.util.function.BiFunction)5 LinkedList (java.util.LinkedList)4