public void testCurrentValueFor() { MixedRadixNumber mrn; // mrn = new MixedRadixNumber(0, new int[] { 3, 3, 2, 2 }); Assert.AreEqual(0, mrn.GetCurrentValueFor(new int[] { 0, 0, 0, 0 })); // mrn = new MixedRadixNumber(35, new int[] { 3, 3, 2, 2 }); Assert.AreEqual(35, mrn.GetCurrentValueFor(new int[] { 2, 2, 1, 1 })); // mrn = new MixedRadixNumber(25, new int[] { 3, 3, 2, 2 }); Assert.AreEqual(25, mrn.GetCurrentValueFor(new int[] { 1, 2, 0, 1 })); // mrn = new MixedRadixNumber(17, new int[] { 3, 3, 2, 2 }); Assert.AreEqual(17, mrn.GetCurrentValueFor(new int[] { 2, 2, 1, 0 })); // mrn = new MixedRadixNumber(8, new int[] { 3, 3, 2, 2 }); Assert.AreEqual(8, mrn.GetCurrentValueFor(new int[] { 2, 2, 0, 0 })); // mrn = new MixedRadixNumber(359, new int[] { 3, 4, 5, 6 }); Assert.AreEqual(359, mrn.GetCurrentValueFor(new int[] { 2, 3, 4, 5 })); // mrn = new MixedRadixNumber(359, new int[] { 6, 5, 4, 3 }); Assert.AreEqual(359, mrn.GetCurrentValueFor(new int[] { 5, 4, 3, 2 })); }
public ProbabilityTable(double[] vals, params IRandomVariable[] vars) { if (null == vals) { throw new IllegalArgumentException("Values must be specified"); } if (vals.Length != ProbUtil.expectedSizeOfProbabilityTable(vars)) { throw new IllegalArgumentException("ProbabilityTable of length " + vals.Length + " is not the correct size, should be " + ProbUtil.expectedSizeOfProbabilityTable(vars) + " in order to represent all possible combinations."); } if (null != vars) { foreach (IRandomVariable rv in vars) { // Track index information relevant to each variable. randomVarInfo.Put(rv, new RVInfo(rv)); } } values = new double[vals.Length]; System.Array.Copy(vals, 0, values, 0, vals.Length); radices = createRadixs(randomVarInfo); if (radices.Length > 0) { queryMRN = new MixedRadixNumber(0, radices); } }
public ProbabilityTableIteratorImp2(ProbabilityTable quotient, int[] qRVs, MixedRadixNumber qMRN, MixedRadixNumber dMRN, IMap <IRandomVariable, RVInfo> diff, ProbabilityTable probabilityTable) { this.quotient = quotient; this.qRVs = qRVs; this.qMRN = qMRN; this.dMRN = dMRN; this.diff = diff; this.probabilityTable = probabilityTable; }
public void testIncrement() { MixedRadixNumber mrn = new MixedRadixNumber(0, new int[] { 3, 2 }); int i = 0; while (mrn.Increment()) { ++i; } Assert.AreEqual(i, mrn.GetMaxAllowedValue()); }
public void testDecrement() { MixedRadixNumber mrn = new MixedRadixNumber(5, new int[] { 3, 2 }); int i = 0; while (mrn.decrement()) { i++; } Assert.AreEqual(i, mrn.getMaxAllowedValue()); }
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); }
// END-Factor // /** * Iterate over all the possible value assignments for the Random Variables * comprising this ProbabilityTable. * * @param pti * the ProbabilityTable Iterator to iterate. */ public void iterateOverTable(IIterator pti) { IMap <IRandomVariable, object> possibleWorld = CollectionFactory.CreateInsertionOrderedMap <IRandomVariable, object>(); MixedRadixNumber mrn = new MixedRadixNumber(0, radices); do { foreach (RVInfo rvInfo in randomVarInfo.GetValues()) { possibleWorld.Put(rvInfo.getVariable(), rvInfo .getDomainValueAt(mrn.GetCurrentNumeralValue(rvInfo .getRadixIdx()))); } pti.iterate(possibleWorld, values[mrn.IntValue()]); } while (mrn.Increment()); }
public void testCurrentNumberalValue() { MixedRadixNumber mrn; // mrn = new MixedRadixNumber(0, new int[] { 3, 3, 2, 2 }); Assert.AreEqual(0, mrn.GetCurrentNumeralValue(0)); Assert.AreEqual(0, mrn.GetCurrentNumeralValue(1)); Assert.AreEqual(0, mrn.GetCurrentNumeralValue(2)); Assert.AreEqual(0, mrn.GetCurrentNumeralValue(3)); // mrn = new MixedRadixNumber(35, new int[] { 3, 3, 2, 2 }); Assert.AreEqual(2, mrn.GetCurrentNumeralValue(0)); Assert.AreEqual(2, mrn.GetCurrentNumeralValue(1)); Assert.AreEqual(1, mrn.GetCurrentNumeralValue(2)); Assert.AreEqual(1, mrn.GetCurrentNumeralValue(3)); // mrn = new MixedRadixNumber(25, new int[] { 3, 3, 2, 2 }); Assert.AreEqual(1, mrn.GetCurrentNumeralValue(0)); Assert.AreEqual(2, mrn.GetCurrentNumeralValue(1)); Assert.AreEqual(0, mrn.GetCurrentNumeralValue(2)); Assert.AreEqual(1, mrn.GetCurrentNumeralValue(3)); // mrn = new MixedRadixNumber(17, new int[] { 3, 3, 2, 2 }); Assert.AreEqual(2, mrn.GetCurrentNumeralValue(0)); Assert.AreEqual(2, mrn.GetCurrentNumeralValue(1)); Assert.AreEqual(1, mrn.GetCurrentNumeralValue(2)); Assert.AreEqual(0, mrn.GetCurrentNumeralValue(3)); // mrn = new MixedRadixNumber(8, new int[] { 3, 3, 2, 2 }); Assert.AreEqual(2, mrn.GetCurrentNumeralValue(0)); Assert.AreEqual(2, mrn.GetCurrentNumeralValue(1)); Assert.AreEqual(0, mrn.GetCurrentNumeralValue(2)); Assert.AreEqual(0, mrn.GetCurrentNumeralValue(3)); // mrn = new MixedRadixNumber(359, new int[] { 3, 4, 5, 6 }); Assert.AreEqual(2, mrn.GetCurrentNumeralValue(0)); Assert.AreEqual(3, mrn.GetCurrentNumeralValue(1)); Assert.AreEqual(4, mrn.GetCurrentNumeralValue(2)); Assert.AreEqual(5, mrn.GetCurrentNumeralValue(3)); // mrn = new MixedRadixNumber(359, new int[] { 6, 5, 4, 3 }); Assert.AreEqual(5, mrn.GetCurrentNumeralValue(0)); Assert.AreEqual(4, mrn.GetCurrentNumeralValue(1)); Assert.AreEqual(3, mrn.GetCurrentNumeralValue(2)); Assert.AreEqual(2, mrn.GetCurrentNumeralValue(3)); }
// END-Factor // /** * Iterate over all the possible value assignments for the Random Variables * comprising this ProbabilityTable. * * @param pti * the ProbabilityTable Iterator to iterate. */ public void iterateOverTable(Factor.Iterator pti) { Map <RandomVariable, Object> possibleWorld = new LinkedHashMap <RandomVariable, Object>(); MixedRadixNumber mrn = new MixedRadixNumber(0, radices); do { foreach (RVInfo rvInfo in randomVarInfo.Values) { possibleWorld.put(rvInfo.getVariable(), rvInfo .getDomainValueAt(mrn.getCurrentNumeralValue(rvInfo . getRadixIdx ()))); } pti.iterate(possibleWorld, values[mrn.intValue()]); } while (mrn.increment()); }
/** * 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()); } }
private bool checkSubsumes(Clause othC, Dictionary <String, List <Literal> > thisToTry, Dictionary <String, List <Literal> > othCToTry) { bool subsumes = false; List <Term> thisTerms = new List <Term>(); List <Term> othCTerms = new List <Term>(); // Want to track possible number of permuations List <int> radixs = new List <int>(); foreach (String literalName in thisToTry.Keys) { int sizeT = thisToTry[literalName].Count; int sizeO = othCToTry[literalName].Count; if (sizeO > 1) { // The following is being used to // track the number of permutations // that can be mapped from the // other clauses like literals to this // clauses like literals. // i.e. n!/(n-r)! // where n=sizeO and r =sizeT for (int i = 0; i < sizeT; i++) { int r = sizeO - i; if (r > 1) { radixs.Add(r); } } } // Track the terms for this clause foreach (Literal tl in thisToTry[literalName]) { List <FOLNode> folNodes = tl.getAtomicSentence().getArgs(); foreach (FOLNode n in folNodes) { thisTerms.Add((Term)n); } } } MixedRadixNumber permutation = null; long numPermutations = 1L; if (radixs.Count > 0) { permutation = new MixedRadixNumber(0, radixs); numPermutations = permutation.getMaxAllowedValue() + 1; } // Want to ensure none of the othCVariables are // part of the key set of a unification as // this indicates it is not a legal subsumption. List <Variable> othCVariables = _variableCollector .collectAllVariables(othC); Dictionary <Variable, Term> theta = new Dictionary <Variable, Term>(); List <Literal> literalPermuations = new List <Literal>(); for (long l = 0L; l < numPermutations; l++) { // Track the other clause's terms for this // permutation. othCTerms.Clear(); int radixIdx = 0; foreach (String literalName in thisToTry.Keys) { int sizeT = thisToTry[literalName].Count; literalPermuations.Clear(); literalPermuations.AddRange(othCToTry[literalName]); int sizeO = literalPermuations.Count; if (sizeO > 1) { for (int i = 0; i < sizeT; i++) { int r = sizeO - i; if (r > 1) { // If not a 1 to 1 mapping then you need // to use the correct permuation int numPos = permutation .getCurrentNumeralValue(radixIdx); Literal lit = literalPermuations[numPos]; literalPermuations.Remove(lit); foreach (FOLNode arg in lit.getAtomicSentence().getArgs()) { othCTerms.Add((Term)arg); } radixIdx++; } else { // is the last mapping, therefore // won't be on the radix foreach (FOLNode arg in literalPermuations[0].getAtomicSentence().getArgs()) { othCTerms.Add((Term)arg); } } } } else { // a 1 to 1 mapping foreach (FOLNode arg in literalPermuations[0].getAtomicSentence().getArgs()) { othCTerms.Add((Term)arg); } } } // Note: on unifier // unifier.unify(P(w, x), P(y, z)))={w=y, x=z} // unifier.unify(P(y, z), P(w, x)))={y=w, z=x} // Therefore want this clause to be the first // so can do the othCVariables check for an invalid // subsumes. theta.Clear(); List <FOLNode> termNodes = new List <FOLNode>(); foreach (Term t in thisTerms) { termNodes.Add((FOLNode)t); } List <FOLNode> othCNodes = new List <FOLNode>(); foreach (Term t in othCTerms) { othCNodes.Add((FOLNode)t); } if (null != _unifier.unify(termNodes, othCNodes, theta)) { bool containsAny = false; foreach (Variable v in theta.Keys) { if (othCVariables.Contains(v)) { containsAny = true; break; } } if (!containsAny) { subsumes = true; break; } } // If there is more than 1 mapping // keep track of where I am in the // possible number of mapping permutations. if (null != permutation) { permutation.increment(); } } return(subsumes); }
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); }
private bool checkSubsumes(Clause othC, IMap <string, ICollection <Literal> > thisToTry, IMap <string, ICollection <Literal> > othCToTry) { bool subsumes = false; ICollection <Term> thisTerms = CollectionFactory.CreateQueue <Term>(); ICollection <Term> othCTerms = CollectionFactory.CreateQueue <Term>(); // Want to track possible number of permuations ICollection <int> radices = CollectionFactory.CreateQueue <int>(); foreach (string literalName in thisToTry.GetKeys()) { int sizeT = thisToTry.Get(literalName).Size(); int sizeO = othCToTry.Get(literalName).Size(); if (sizeO > 1) { // The following is being used to // track the number of permutations // that can be mapped from the // other clauses like literals to this // clauses like literals. // i.e. n!/(n-r)! // where n=sizeO and r =sizeT for (int i = 0; i < sizeT; ++i) { int r = sizeO - i; if (r > 1) { radices.Add(r); } } } // Track the terms for this clause foreach (Literal tl in thisToTry.Get(literalName)) { thisTerms.AddAll(tl.getAtomicSentence().getArgs()); } } MixedRadixNumber permutation = null; long numPermutations = 1L; if (radices.Size() > 0) { permutation = new MixedRadixNumber(0, radices); numPermutations = permutation.GetMaxAllowedValue() + 1; } // Want to ensure none of the othCVariables are // part of the key set of a unification as // this indicates it is not a legal subsumption. ISet <Variable> othCVariables = _variableCollector .collectAllVariables(othC); IMap <Variable, Term> theta = CollectionFactory.CreateInsertionOrderedMap <Variable, Term>(); ICollection <Literal> literalPermuations = CollectionFactory.CreateQueue <Literal>(); for (long l = 0L; l < numPermutations; l++) { // Track the other clause's terms for this // permutation. othCTerms.Clear(); int radixIdx = 0; foreach (string literalName in thisToTry.GetKeys()) { int sizeT = thisToTry.Get(literalName).Size(); literalPermuations.Clear(); literalPermuations.AddAll(othCToTry.Get(literalName)); int sizeO = literalPermuations.Size(); if (sizeO > 1) { for (int i = 0; i < sizeT; ++i) { int r = sizeO - i; if (r > 1) { // If not a 1 to 1 mapping then you need // to use the correct permuation int numPos = permutation.GetCurrentNumeralValue(radixIdx); Literal lit = literalPermuations.Get(numPos); literalPermuations.Remove(lit); othCTerms.AddAll(lit.getAtomicSentence().getArgs()); radixIdx++; } else { // is the last mapping, therefore // won't be on the radix othCTerms.AddAll(literalPermuations.Get(0).getAtomicSentence().getArgs()); } } } else { // a 1 to 1 mapping othCTerms.AddAll(literalPermuations.Get(0) .getAtomicSentence().getArgs()); } } // Note: on unifier // unifier.unify(P(w, x), P(y, z)))={w=y, x=z} // unifier.unify(P(y, z), P(w, x)))={y=w, z=x} // Therefore want this clause to be the first // so can do the othCVariables check for an invalid // subsumes. theta.Clear(); if (null != _unifier.unify(thisTerms, othCTerms, theta)) { bool containsAny = false; foreach (Variable v in theta.GetKeys()) { if (othCVariables.Contains(v)) { containsAny = true; break; } } if (!containsAny) { subsumes = true; break; } } // If there is more than 1 mapping // keep track of where I am in the // possible number of mapping permutations. if (null != permutation) { permutation.Increment(); } } return(subsumes); }
public void testSetCurrentValueFor() { MixedRadixNumber mrn; // mrn = new MixedRadixNumber(0, new int[] { 3, 3, 2, 2 }); mrn.SetCurrentValueFor(new int[] { 0, 0, 0, 0 }); Assert.AreEqual(0, mrn.IntValue()); Assert.AreEqual(0, mrn.GetCurrentNumeralValue(0)); Assert.AreEqual(0, mrn.GetCurrentNumeralValue(1)); Assert.AreEqual(0, mrn.GetCurrentNumeralValue(2)); Assert.AreEqual(0, mrn.GetCurrentNumeralValue(3)); // mrn = new MixedRadixNumber(0, new int[] { 3, 3, 2, 2 }); mrn.SetCurrentValueFor(new int[] { 2, 2, 1, 1 }); Assert.AreEqual(35, mrn.IntValue()); Assert.AreEqual(2, mrn.GetCurrentNumeralValue(0)); Assert.AreEqual(2, mrn.GetCurrentNumeralValue(1)); Assert.AreEqual(1, mrn.GetCurrentNumeralValue(2)); Assert.AreEqual(1, mrn.GetCurrentNumeralValue(3)); // mrn = new MixedRadixNumber(0, new int[] { 3, 3, 2, 2 }); mrn.SetCurrentValueFor(new int[] { 1, 2, 0, 1 }); Assert.AreEqual(25, mrn.IntValue()); Assert.AreEqual(1, mrn.GetCurrentNumeralValue(0)); Assert.AreEqual(2, mrn.GetCurrentNumeralValue(1)); Assert.AreEqual(0, mrn.GetCurrentNumeralValue(2)); Assert.AreEqual(1, mrn.GetCurrentNumeralValue(3)); // mrn = new MixedRadixNumber(0, new int[] { 3, 3, 2, 2 }); mrn.SetCurrentValueFor(new int[] { 2, 2, 1, 0 }); Assert.AreEqual(17, mrn.IntValue()); Assert.AreEqual(2, mrn.GetCurrentNumeralValue(0)); Assert.AreEqual(2, mrn.GetCurrentNumeralValue(1)); Assert.AreEqual(1, mrn.GetCurrentNumeralValue(2)); Assert.AreEqual(0, mrn.GetCurrentNumeralValue(3)); // mrn = new MixedRadixNumber(0, new int[] { 3, 3, 2, 2 }); mrn.SetCurrentValueFor(new int[] { 2, 2, 0, 0 }); Assert.AreEqual(8, mrn.IntValue()); Assert.AreEqual(2, mrn.GetCurrentNumeralValue(0)); Assert.AreEqual(2, mrn.GetCurrentNumeralValue(1)); Assert.AreEqual(0, mrn.GetCurrentNumeralValue(2)); Assert.AreEqual(0, mrn.GetCurrentNumeralValue(3)); // mrn = new MixedRadixNumber(0, new int[] { 3, 4, 5, 6 }); mrn.SetCurrentValueFor(new int[] { 2, 3, 4, 5 }); Assert.AreEqual(359, mrn.IntValue()); Assert.AreEqual(2, mrn.GetCurrentNumeralValue(0)); Assert.AreEqual(3, mrn.GetCurrentNumeralValue(1)); Assert.AreEqual(4, mrn.GetCurrentNumeralValue(2)); Assert.AreEqual(5, mrn.GetCurrentNumeralValue(3)); // mrn = new MixedRadixNumber(0, new int[] { 6, 5, 4, 3 }); mrn.SetCurrentValueFor(new int[] { 5, 4, 3, 2 }); Assert.AreEqual(359, mrn.IntValue()); Assert.AreEqual(5, mrn.GetCurrentNumeralValue(0)); Assert.AreEqual(4, mrn.GetCurrentNumeralValue(1)); Assert.AreEqual(3, mrn.GetCurrentNumeralValue(2)); Assert.AreEqual(2, mrn.GetCurrentNumeralValue(3)); }
private bool CheckSubsumes(Clause othC, IDictionary <string, IList <Literal> > thisToTry, IDictionary <string, IList <Literal> > othCToTry) { bool subsumes = false; var thisTerms = new List <ITerm>(); var othCTerms = new List <ITerm>(); // Want to track possible number of permuations var radixs = new List <int>(); foreach (string literalName in thisToTry.Keys) { int sizeT = thisToTry[literalName].Count; int sizeO = othCToTry[literalName].Count; if (sizeO > 1) { // The following is being used to // track the number of permutations // that can be mapped from the // other clauses like literals to this // clauses like literals. // i.e. n!/(n-r)! // where n=sizeO and r =sizeT for (int i = 0; i < sizeT; i++) { int r = sizeO - i; if (r > 1) { radixs.Add(r); } } } // Track the terms for this clause foreach (var tl in thisToTry[literalName]) { thisTerms.AddRange(tl.AtomicSentence.GetArgs().Cast <ITerm>()); } } MixedRadixNumber permutation = null; long numPermutations = 1L; if (radixs.Count > 0) { permutation = new MixedRadixNumber(0, radixs); numPermutations = permutation.GetMaxAllowedValue() + 1; } // Want to ensure none of the othCVariables are // part of the key set of a unification as // this indicates it is not a legal subsumption. var othCVariables = _variableCollector.CollectAllVariables(othC); var theta = new Dictionary <Variable, ITerm>(); var literalPermuations = new List <Literal>(); for (var l = 0L; l < numPermutations; l++) { // Track the other clause's terms for this // permutation. othCTerms.Clear(); var radixIdx = 0; foreach (string literalName in thisToTry.Keys) { var sizeT = thisToTry[literalName].Count; literalPermuations.Clear(); literalPermuations.AddRange(othCToTry[literalName]); var sizeO = literalPermuations.Count; if (sizeO > 1) { for (var i = 0; i < sizeT; i++) { var r = sizeO - i; if (r > 1) { // If not a 1 to 1 mapping then you need // to use the correct permuation int numPos = permutation.GetCurrentNumeralValue(radixIdx); othCTerms.AddRange(literalPermuations[numPos].AtomicSentence.GetArgs().Cast <ITerm>()); literalPermuations.RemoveAt(numPos); radixIdx++; } else { // is the last mapping, therefore // won't be on the radix othCTerms.AddRange(literalPermuations[0].AtomicSentence.GetArgs().Cast <ITerm>()); } } } else { // a 1 to 1 mapping othCTerms.AddRange(literalPermuations[0].AtomicSentence.GetArgs().Cast <ITerm>()); } } // Note: on unifier // unifier.unify(P(w, x), P(y, z)))={w=y, x=z} // unifier.unify(P(y, z), P(w, x)))={y=w, z=x} // Therefore want this clause to be the first // so can do the othCVariables check for an invalid // subsumes. theta.Clear(); if (null != _unifier.Unify((IFOLNode)thisTerms, (IFOLNode)othCTerms, theta)) { var containsAny = theta.Keys.Any(othCVariables.Contains); if (!containsAny) { subsumes = true; break; } } // If there is more than 1 mapping // keep track of where I am in the // possible number of mapping permutations. if (null != permutation) { permutation.Increment(); } } return(subsumes); }