Exemple #1
0
        public DynamicBayesNet(IBayesianNetwork priorNetwork,
                               IMap <IRandomVariable, IRandomVariable> X_0_to_X_1,
                               ISet <IRandomVariable> E_1, params INode[] rootNodes)
            : base(rootNodes)
        {
            foreach (var x0_x1 in X_0_to_X_1)
            {
                IRandomVariable x0 = x0_x1.GetKey();
                IRandomVariable x1 = x0_x1.GetValue();
                this.X_0.Add(x0);
                this.X_1.Add(x1);
                this.X_0_to_X_1.Put(x0, x1);
                this.X_1_to_X_0.Put(x1, x0);
            }
            this.E_1.AddAll(E_1);

            // Assert the X_0, X_1, and E_1 sets are of expected sizes
            ISet <IRandomVariable> combined = CollectionFactory.CreateSet <IRandomVariable>();

            combined.AddAll(X_0);
            combined.AddAll(X_1);
            combined.AddAll(E_1);
            if (SetOps.difference(CollectionFactory.CreateSet <IRandomVariable>(varToNodeMap.GetKeys()), combined).Size() != 0)
            {
                throw new IllegalArgumentException("X_0, X_1, and E_1 do not map correctly to the Nodes describing this Dynamic Bayesian Network.");
            }
            this.priorNetwork = priorNetwork;

            X_1_VariablesInTopologicalOrder.AddAll(GetVariablesInTopologicalOrder());
            X_1_VariablesInTopologicalOrder.RemoveAll(X_0);
            X_1_VariablesInTopologicalOrder.RemoveAll(E_1);
        }
Exemple #2
0
        public List <Symbol> getPurePositiveSymbolsIn(Sentence sentence)
        {
            List <Symbol> allNegatives = getNegativeSymbolsIn(sentence);
            List <Symbol> allPositives = getPositiveSymbolsIn(sentence);

            return(SetOps.difference(allPositives, allNegatives));
        }
Exemple #3
0
        public void testDifference()
        {
            ISet <int> difference = SetOps.difference(s1, s2);

            Assert.AreEqual(3, difference.Size());
            Assert.IsTrue(difference.Contains(1));
            Assert.IsTrue(difference.Contains(2));
            Assert.IsTrue(difference.Contains(3));
        }
Exemple #4
0
        public void testDifference2()
        {
            ISet <int> one = CollectionFactory.CreateSet <int>();
            ISet <int> two = CollectionFactory.CreateSet <int>();

            one.Add(1);
            two.Add(1);
            ISet <int> difference = SetOps.difference(one, two);

            Assert.IsTrue(difference.IsEmpty());
        }
Exemple #5
0
        public void testDifference2()
        {
            List <int> one = new List <int>();
            List <int> two = new List <int>();

            one.Add(1);
            two.Add(1);
            List <int> difference = SetOps.difference(one, two);

            Assert.AreEqual(0, difference.Count);
        }
        public ProbabilityTable divideBy(ProbabilityTable divisor)
        {
            if (!randomVarInfo.GetKeys().ContainsAll(divisor.randomVarInfo.GetKeys()))
            {
                throw new IllegalArgumentException("Divisor must be a subset of the dividend.");
            }

            ProbabilityTable quotient = new ProbabilityTable(randomVarInfo.GetKeys());

            if (1 == divisor.getValues().Length)
            {
                double d = divisor.getValues()[0];
                for (int i = 0; i < quotient.getValues().Length; ++i)
                {
                    if (0 == d)
                    {
                        quotient.getValues()[i] = 0;
                    }
                    else
                    {
                        quotient.getValues()[i] = getValues()[i] / d;
                    }
                }
            }
            else
            {
                ISet <IRandomVariable> dividendDivisorDiff = SetOps
                                                             .difference(
                    CollectionFactory.CreateSet <IRandomVariable>(this.randomVarInfo.GetKeys()),
                    CollectionFactory.CreateSet <IRandomVariable>(divisor.randomVarInfo.GetKeys()));
                IMap <IRandomVariable, RVInfo> tdiff = null;
                MixedRadixNumber tdMRN = null;
                if (dividendDivisorDiff.Size() > 0)
                {
                    tdiff = CollectionFactory.CreateInsertionOrderedMap <IRandomVariable, RVInfo>();
                    foreach (IRandomVariable rv in dividendDivisorDiff)
                    {
                        tdiff.Put(rv, new RVInfo(rv));
                    }
                    tdMRN = new MixedRadixNumber(0, createRadixs(tdiff));
                }
                IMap <IRandomVariable, RVInfo> diff      = tdiff;
                MixedRadixNumber         dMRN            = tdMRN;
                int[]                    qRVs            = new int[quotient.radices.Length];
                MixedRadixNumber         qMRN            = new MixedRadixNumber(0, quotient.radices);
                ProbabilityTableIterator divisorIterator = new ProbabilityTableIteratorImp2(quotient, qRVs, qMRN, dMRN, diff, this);

                divisor.iterateOverTable(divisorIterator);
            }

            return(quotient);
        }
        public DynamicBayesNet(BayesianNetwork priorNetwork,
                               Map <RandomVariable, RandomVariable> X_0_to_X_1,
                               Set <RandomVariable> E_1, params Node[] rootNodes)
            : base(rootNodes)
        {
            foreach (RandomVariable rv in X_0_to_X_1.keySet()
                     )
            {
                RandomVariable x0 = rv;
                RandomVariable x1 = X_0_to_X_1[rv];
                this.X_0.add(x0);
                this.X_1.add(x1);
                this.X_0_to_X_1.put(x0, x1);
                this.X_1_to_X_0.put(x1, x0);
            }
            this.E_1.addAll(new List <RandomVariable>(E_1));

            // Assert the X_0, X_1, and E_1 sets are of expected sizes
            Set <RandomVariable> combined = new Set <RandomVariable>();

            combined.addAll(new List <RandomVariable>(X_0));
            combined.addAll(new List <RandomVariable>(X_1));
            combined.addAll(new List <RandomVariable>(E_1));
            if (
                SetOps.difference(new List <RandomVariable>(varToNodeMap.keySet()), new List <RandomVariable>(combined)).
                Count != 0)
            {
                throw new IllegalArgumentException(
                          "X_0, X_1, and E_1 do not map correctly to the Nodes describing this Dynamic Bayesian Network.");
            }
            this.priorNetwork = priorNetwork;

            X_1_VariablesInTopologicalOrder
            .AddRange(getVariablesInTopologicalOrder());
            X_1_VariablesInTopologicalOrder.RemoveAll(X_0);
            X_1_VariablesInTopologicalOrder.RemoveAll(E_1);
        }
        /**
         * Iterate over all possible values assignments for the Random Variables
         * comprising this ProbabilityTable that are not in the fixed set of values.
         * This allows you to iterate over a subset of possible combinations.
         *
         * @param pti
         *            the ProbabilityTable Iterator to iterate
         * @param fixedValues
         *            Fixed values for a subset of the Random Variables comprising
         *            this Probability Table.
         */
        public void iterateOverTable(IIterator pti, params AssignmentProposition[] fixedValues)
        {
            IMap <IRandomVariable, object> possibleWorld = CollectionFactory.CreateInsertionOrderedMap <IRandomVariable, object>();
            MixedRadixNumber tableMRN = new MixedRadixNumber(0, radices);

            int[] tableRadixValues = new int[radices.Length];

            // Assert that the Random Variables for the fixed values
            // are part of this probability table and assign
            // all the fixed values to the possible world.
            foreach (AssignmentProposition ap in fixedValues)
            {
                if (!randomVarInfo.ContainsKey(ap.getTermVariable()))
                {
                    throw new IllegalArgumentException("Assignment proposition ["
                                                       + ap + "] does not belong to this probability table.");
                }
                possibleWorld.Put(ap.getTermVariable(), ap.getValue());
                RVInfo fixedRVI = randomVarInfo.Get(ap.getTermVariable());
                tableRadixValues[fixedRVI.getRadixIdx()] = fixedRVI
                                                           .getIdxForDomain(ap.getValue());
            }
            // If have assignments for all the random variables
            // in this probability table
            if (fixedValues.Length == randomVarInfo.Size())
            {
                // Then only 1 iteration call is required.
                pti.iterate(possibleWorld, getValue(fixedValues));
            }
            else
            {
                // Else iterate over the non-fixed values
                ISet <IRandomVariable> freeVariables = SetOps.difference(
                    CollectionFactory.CreateSet <IRandomVariable>(this.randomVarInfo.GetKeys()),
                    CollectionFactory.CreateSet <IRandomVariable>(possibleWorld.GetKeys()));
                IMap <IRandomVariable, RVInfo> freeVarInfo = CollectionFactory.CreateInsertionOrderedMap <IRandomVariable, RVInfo>();
                // Remove the fixed Variables
                foreach (IRandomVariable fv in freeVariables)
                {
                    freeVarInfo.Put(fv, new RVInfo(fv));
                }
                int[]            freeRadixValues = createRadixs(freeVarInfo);
                MixedRadixNumber freeMRN         = new MixedRadixNumber(0, freeRadixValues);
                object           fval            = null;
                // Iterate through all combinations of the free variables
                do
                {
                    // Put the current assignments for the free variables
                    // into the possible world and update
                    // the current index in the table MRN
                    foreach (RVInfo freeRVI in freeVarInfo.GetValues())
                    {
                        fval = freeRVI.getDomainValueAt(freeMRN
                                                        .GetCurrentNumeralValue(freeRVI.getRadixIdx()));
                        possibleWorld.Put(freeRVI.getVariable(), fval);

                        tableRadixValues[randomVarInfo.Get(freeRVI.getVariable())
                                         .getRadixIdx()] = freeRVI.getIdxForDomain(fval);
                    }
                    pti.iterate(possibleWorld, values[(int)tableMRN
                                                      .GetCurrentValueFor(tableRadixValues)]);
                } while (freeMRN.Increment());
            }
        }
        public ProbabilityTable divideBy(ProbabilityTable divisor)
        {
            if (!randomVarInfo.keySet().containsAll(divisor.randomVarInfo.keySet()))
            {
                throw new IllegalArgumentException(
                          "Divisor must be a subset of the dividend.");
            }

            ProbabilityTable quotient = new ProbabilityTable(new List <RandomVariable>(randomVarInfo.Keys));

            if (1 == divisor.getValues().Length)
            {
                double d = divisor.getValues()[0];
                for (int i = 0; i < quotient.getValues().Length; i++)
                {
                    if (0 == d)
                    {
                        quotient.getValues()[i] = 0;
                    }
                    else
                    {
                        quotient.getValues()[i] = getValues()[i] / d;
                    }
                }
            }
            else
            {
                Set <RandomVariable> dividendDivisorDiff = SetOps
                                                           .difference(new List <RVInfo>(randomVarInfo.keySet()),
                                                                       new List <RVInfo>(randomVarInfo.keySet()));
                Map <RandomVariable, RVInfo> tdiff = null;
                MixedRadixNumber             tdMRN = null;
                if (dividendDivisorDiff.size() > 0)
                {
                    tdiff = new LinkedHashMap <RandomVariable, RVInfo>();
                    foreach (RandomVariable rv in dividendDivisorDiff)
                    {
                        tdiff.put(rv, new RVInfo(rv));
                    }
                    tdMRN = new MixedRadixNumber(0, createRadixs(tdiff));
                }
                Map <RandomVariable, RVInfo> diff = tdiff;
                MixedRadixNumber             dMRN = tdMRN;
                int[]            qRVs             = new int[quotient.radices.Length];
                MixedRadixNumber qMRN             = new MixedRadixNumber(0,
                                                                         quotient.radices);
                //ProbabilityTable.Iterator divisorIterator = new ProbabilityTable.Iterator() {
                //    public void iterate(Map<RandomVariable, Object> possibleWorld,
                //            double probability) {
                //        foreach (RandomVariable rv in possibleWorld.keySet()) {
                //            RVInfo rvInfo = quotient.randomVarInfo.get(rv);
                //            qRVs[rvInfo.getRadixIdx()] = rvInfo
                //                    .getIdxForDomain(possibleWorld.get(rv));
                //        }
                //        if (null != diff) {
                //            // Start from 0 off the diff
                //            dMRN.setCurrentValueFor(new int[diff.size()]);
                //            do {
                //                for (RandomVariable rv : diff.keySet()) {
                //                    RVInfo drvInfo = diff.get(rv);
                //                    RVInfo qrvInfo = quotient.randomVarInfo.get(rv);
                //                    qRVs[qrvInfo.getRadixIdx()] = dMRN
                //                            .getCurrentNumeralValue(drvInfo
                //                                    .getRadixIdx());
                //                }
                //                updateQuotient(probability);
                //            } while (dMRN.increment());
                //        } else {
                //            updateQuotient(probability);
                //        }
                //    }

                //    //
                //
                //private void updateQuotient(double probability) {
                //    int offset = (int) qMRN.getCurrentValueFor(qRVs);
                //    if (0 == probability) {
                //        quotient.getValues()[offset] = 0;
                //    } else {
                //        quotient.getValues()[offset] += getValues()[offset]
                //                / probability;
                //    }
                //}
                //////  };

                //	divisor.iterateOverTable(divisorIterator);
                // TODO
            }

            return(quotient);
        }
Exemple #10
0
        public SymbolValuePair findPureSymbolValuePair(List <Sentence> clauseList,
                                                       Model model, List <Symbol> symbols)
        {
            List <Sentence> _clausesWithNonTrueValues = clausesWithNonTrueValues(clauseList,
                                                                                 model);
            Sentence nonTrueClauses = LogicUtils.chainWith("AND",
                                                           _clausesWithNonTrueValues);
            // System.Console.WriteLine("Unsatisfied clauses = "
            // + clausesWithNonTrueValues.Count);
            List <Symbol> symbolsAlreadyAssigned = new List <Symbol>(model.getAssignedSymbols());

            // debug
            // List symList = asList(symbolsAlreadyAssigned);
            //
            // System.Console.WriteLine(" assignedSymbols = " + symList.Count);
            // if (symList.Count == 52) {
            // System.Console.WriteLine("untrue clauses = " + clausesWithNonTrueValues);
            // System.Console.WriteLine("model= " + model);
            // }

            // debug
            List <Symbol> purePositiveSymbols = SetOps
                                                .difference(new SymbolClassifier()
                                                            .getPurePositiveSymbolsIn(nonTrueClauses),
                                                            symbolsAlreadyAssigned);

            List <Symbol> pureNegativeSymbols = SetOps
                                                .difference(new SymbolClassifier()
                                                            .getPureNegativeSymbolsIn(nonTrueClauses),
                                                            symbolsAlreadyAssigned);

            // if none found return "not found
            if ((purePositiveSymbols.Count == 0) &&
                (pureNegativeSymbols.Count == 0))
            {
                return(new SymbolValuePair());// automatically set to null values
            }
            else
            {
                if (purePositiveSymbols.Count > 0)
                {
                    Symbol symbol = purePositiveSymbols[0];
                    if (pureNegativeSymbols.Contains(symbol))
                    {
                        throw new ApplicationException("Symbol " + symbol.getValue()
                                                       + "misclassified");
                    }
                    return(new SymbolValuePair(symbol, true));
                }
                else
                {
                    Symbol symbol = new Symbol((pureNegativeSymbols[0])
                                               .getValue());
                    if (purePositiveSymbols.Contains(symbol))
                    {
                        throw new ApplicationException("Symbol " + symbol.getValue()
                                                       + "misclassified");
                    }
                    return(new SymbolValuePair(symbol, false));
                }
            }
        }