Search in sources :

Example 21 with ExpressionFactor

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();
    }
}
Also used : ExpressionFactor(com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.api.ExpressionFactor) UAIModel(com.sri.ai.praise.core.representation.classbased.table.core.uai.UAIModel) ExpressionFactor(com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.api.ExpressionFactor) Factor(com.sri.ai.praise.core.representation.interfacebased.factor.api.Factor) FileReader(java.io.FileReader) IOException(java.io.IOException) File(java.io.File)

Example 22 with ExpressionFactor

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;
    }
}
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 23 with ExpressionFactor

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;
    }
}
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 24 with ExpressionFactor

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;
}
Also used : TrueContext(com.sri.ai.grinder.core.TrueContext) Context(com.sri.ai.grinder.api.Context) DefaultExpressionVariable(com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.core.DefaultExpressionVariable) DefaultExpressionFactor(com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.core.DefaultExpressionFactor) Util.mapIntoArrayList(com.sri.ai.util.Util.mapIntoArrayList) ArrayList(java.util.ArrayList) TrueContext(com.sri.ai.grinder.core.TrueContext) CommonTheory(com.sri.ai.grinder.application.CommonTheory) 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) Expression(com.sri.ai.expresso.api.Expression) DefaultExpressionVariable(com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.core.DefaultExpressionVariable) ExpressionVariable(com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.api.ExpressionVariable) Pair(com.sri.ai.util.base.Pair)

Example 25 with ExpressionFactor

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

Aggregations

ExpressionFactor (com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.api.ExpressionFactor)26 DefaultExpressionFactor (com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.core.DefaultExpressionFactor)21 Expression (com.sri.ai.expresso.api.Expression)18 Factor (com.sri.ai.praise.core.representation.interfacebased.factor.api.Factor)18 Context (com.sri.ai.grinder.api.Context)17 TrueContext (com.sri.ai.grinder.core.TrueContext)17 CommonTheory (com.sri.ai.grinder.application.CommonTheory)15 Test (org.junit.Test)12 ConstantFactor (com.sri.ai.praise.core.representation.interfacebased.factor.core.ConstantFactor)11 Theory (com.sri.ai.grinder.api.Theory)9 CompoundTheory (com.sri.ai.grinder.theory.compound.CompoundTheory)9 DifferenceArithmeticTheory (com.sri.ai.grinder.theory.differencearithmetic.DifferenceArithmeticTheory)9 EqualityTheory (com.sri.ai.grinder.theory.equality.EqualityTheory)9 LinearRealArithmeticTheory (com.sri.ai.grinder.theory.linearrealarithmetic.LinearRealArithmeticTheory)9 PropositionalTheory (com.sri.ai.grinder.theory.propositional.PropositionalTheory)9 TupleTheory (com.sri.ai.grinder.theory.tuple.TupleTheory)9 ExpressionVariable (com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.api.ExpressionVariable)5 DefaultExpressionVariable (com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.core.DefaultExpressionVariable)5 ArrayList (java.util.ArrayList)5 ExpressionFactorNetwork (com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.core.ExpressionFactorNetwork)4