/*******************************************************************************
* Copyright 2014 Felipe Takiyama
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
******************************************************************************/
package br.usp.poli.takiyama.sandbox;
import java.math.BigDecimal;
import java.util.List;
import org.junit.Test;
import br.usp.poli.takiyama.acfove.AggParfactor.AggParfactorBuilder;
import br.usp.poli.takiyama.cfove.StdParfactor.StdParfactorBuilder;
import br.usp.poli.takiyama.common.Factor;
import br.usp.poli.takiyama.common.Parfactor;
import br.usp.poli.takiyama.prv.LogicalVariable;
import br.usp.poli.takiyama.prv.Or;
import br.usp.poli.takiyama.prv.Prv;
import br.usp.poli.takiyama.prv.StdLogicalVariable;
import br.usp.poli.takiyama.prv.StdPrv;
import br.usp.poli.takiyama.utils.TestUtils;
public class Temp1 {
/**
* This is the 'wrong' way to manually test a inference on the network
* generated by a exists proposition.
* The mistake made here is eliminating b(Y) from a parfactor whose
* logical variable set is {X,Y}.
*/
@Test
public void testInferenceOnExistsNode() {
int n = 2;
LogicalVariable x = StdLogicalVariable.getInstance("X", "x", n);
LogicalVariable y = StdLogicalVariable.getInstance("Y", "x", n);
Prv b = StdPrv.getBooleanInstance("b", y);
Prv r = StdPrv.getBooleanInstance("r", x, y);
Prv and = StdPrv.getBooleanInstance("and", x, y);
Prv exists = StdPrv.getBooleanInstance("exists", x);
List<BigDecimal> fb = TestUtils.toBigDecimalList(0.1, 0.9);
List<BigDecimal> fr = TestUtils.toBigDecimalList(0.2, 0.8);
List<BigDecimal> fand = TestUtils.toBigDecimalList(1, 0, 1, 0, 1, 0, 0, 1);
Parfactor g1 = new StdParfactorBuilder().variables(b).values(fb).build();
Parfactor g2 = new StdParfactorBuilder().variables(r).values(fr).build();
Parfactor g3 = new StdParfactorBuilder().variables(b, r, and).values(fand).build();
Parfactor g4 = new AggParfactorBuilder(and, exists, Or.OR).build();
Parfactor g5 = g2.multiply(g3);
Parfactor g6 = g5.sumOut(r);
Parfactor g7 = g6.multiply(g1);
Parfactor g8 = g7.sumOut(b);
Parfactor g9 = g8.multiply(g4);
Parfactor g10 = g9.sumOut(and);
List<BigDecimal> fand2 = TestUtils.toBigDecimalList(0.28, 0.72);
Parfactor g11 = new StdParfactorBuilder().variables(and).values(fand2).build();
Parfactor g12 = g11.multiply(g4).sumOut(and);
System.out.print(g10);
}
}