Skip to content

Commit d21f4cc

Browse files
author
ctjoreilly
committed
- added utility for handling mixed radix numbers, to be used in subsumption elimination logic.
- increased time allowed for one of the model elimination tests, need to investigate further.
1 parent 5b8a1b3 commit d21f4cc

File tree

5 files changed

+236
-7
lines changed

5 files changed

+236
-7
lines changed

src/aima/test/AllAimaTests.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import aima.test.tvenvironmenttest.TableDrivenVaccumAgentTest;
2323
import aima.test.tvenvironmenttest.TrivialVaccumEnvironmentTest;
2424
import aima.test.utiltest.MeanStDevTests;
25+
import aima.test.utiltest.MixedRadixNumberTest;
2526
import aima.test.utiltest.TableTest;
2627

2728
/**
@@ -34,6 +35,7 @@ public class AllAimaTests {
3435
public static Test suite() {
3536
TestSuite suite = new TestSuite();
3637
suite.addTest(new TestSuite(MeanStDevTests.class));
38+
suite.addTest(new TestSuite(MixedRadixNumberTest.class));
3739
suite.addTest(new TestSuite(ModelBasedTVEVaccumAgentTest.class));
3840
suite.addTest(new TestSuite(PerceptTest.class));
3941
suite.addTest(new TestSuite(PerceptSequenceTest.class));

src/aima/test/logictest/foltest/CommonFOLInferenceProcedureTests.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -170,11 +170,11 @@ protected void testHornClauseKBRingOfThievesQuerySkisXReturnsNancyRedBertDrew(
170170
assertFalse(answer.isUnknownDueToTimeout());
171171
// DB can expand infinitely so is only partial.
172172
assertTrue(answer.isPartialResultDueToTimeout());
173-
assertTrue(4 == answer.getProofs().size());
174-
assertTrue(1 == answer.getProofs().get(0).getAnswerBindings().size());
175-
assertTrue(1 == answer.getProofs().get(1).getAnswerBindings().size());
176-
assertTrue(1 == answer.getProofs().get(2).getAnswerBindings().size());
177-
assertTrue(1 == answer.getProofs().get(3).getAnswerBindings().size());
173+
assertEquals(4, answer.getProofs().size());
174+
assertEquals(1, answer.getProofs().get(0).getAnswerBindings().size());
175+
assertEquals(1, answer.getProofs().get(1).getAnswerBindings().size());
176+
assertEquals(1, answer.getProofs().get(2).getAnswerBindings().size());
177+
assertEquals(1, answer.getProofs().get(3).getAnswerBindings().size());
178178

179179
List<Constant> expected = new ArrayList<Constant>();
180180
expected.add(new Constant("Nancy"));

src/aima/test/logictest/foltest/FOLTFMResolutionTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,10 @@ public void testDefiniteClauseKBWeaponsQueryCriminalXReturnsWestSucceeds() {
3434

3535
public void testHornClauseKBRingOfThievesQuerySkisXReturnsNancyRedBertDrew() {
3636
// The clauses in this KB can keep creating resolvents infinitely,
37-
// therefore give it 10 seconds to find the 4 answers to this, should
37+
// therefore give it 15 seconds to find the 4 answers to this, should
3838
// be more than enough.
3939
testHornClauseKBRingOfThievesQuerySkisXReturnsNancyRedBertDrew(new FOLTFMResolution(
40-
10 * 1000));
40+
15 * 1000));
4141
}
4242

4343
public void testFullFOLKBLovesAnimalQueryKillsCuriosityTunaSucceeds() {
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
package aima.test.utiltest;
2+
3+
import aima.util.MixedRadixNumber;
4+
import junit.framework.TestCase;
5+
6+
/**
7+
* @author Ciaran O'Reilly
8+
*
9+
*/
10+
public class MixedRadixNumberTest extends TestCase {
11+
12+
public void testInvalidRadixs() {
13+
try {
14+
new MixedRadixNumber(100, new int[] {1, 0, -1});
15+
16+
fail("Should have thrown an Illegal Argument Exception");
17+
} catch (IllegalArgumentException iae) {
18+
// Expected
19+
}
20+
}
21+
22+
public void testInvalidMaxValue() {
23+
try {
24+
new MixedRadixNumber(100, new int[] {3, 3, 3});
25+
26+
fail("Should have thrown an Illegal Argument Exception");
27+
} catch (IllegalArgumentException iae) {
28+
// Expected
29+
}
30+
}
31+
32+
public void testAllowedMaxValue() {
33+
assertEquals(15, (new MixedRadixNumber(0, new int[] {2, 2, 2, 2}).getMaxAllowedValue()));
34+
assertEquals(80, (new MixedRadixNumber(0, new int[] {3, 3, 3, 3}).getMaxAllowedValue()));
35+
assertEquals(5, (new MixedRadixNumber(0, new int[] {3, 2}).getMaxAllowedValue()));
36+
assertEquals(35, (new MixedRadixNumber(0, new int[] {3, 3, 2, 2}).getMaxAllowedValue()));
37+
assertEquals(359, (new MixedRadixNumber(0, new int[] {3, 4, 5, 6}).getMaxAllowedValue()));
38+
assertEquals(359, (new MixedRadixNumber(0, new int[] {6, 5, 4, 3}).getMaxAllowedValue()));
39+
}
40+
41+
public void testIncrement() {
42+
MixedRadixNumber mrn = new MixedRadixNumber(0, new int[] {3, 2});
43+
int i = 0;
44+
while (mrn.increment()) {
45+
i++;
46+
}
47+
assertEquals(i, mrn.getMaxAllowedValue());
48+
}
49+
50+
public void testDecrement() {
51+
MixedRadixNumber mrn = new MixedRadixNumber(5, new int[] {3, 2});
52+
int i = 0;
53+
while (mrn.decrement()) {
54+
i++;
55+
}
56+
assertEquals(i, mrn.getMaxAllowedValue());
57+
}
58+
59+
public void testCurrentNumberalValue() {
60+
MixedRadixNumber mrn;
61+
//
62+
mrn = new MixedRadixNumber(0, new int[] {3, 3, 2, 2});
63+
assertEquals(0, mrn.getCurrentNumeralValue(0));
64+
assertEquals(0, mrn.getCurrentNumeralValue(1));
65+
assertEquals(0, mrn.getCurrentNumeralValue(2));
66+
assertEquals(0, mrn.getCurrentNumeralValue(3));
67+
//
68+
mrn = new MixedRadixNumber(35, new int[] {3, 3, 2, 2});
69+
assertEquals(2, mrn.getCurrentNumeralValue(0));
70+
assertEquals(2, mrn.getCurrentNumeralValue(1));
71+
assertEquals(1, mrn.getCurrentNumeralValue(2));
72+
assertEquals(1, mrn.getCurrentNumeralValue(3));
73+
//
74+
mrn = new MixedRadixNumber(25, new int[] {3, 3, 2, 2});
75+
assertEquals(1, mrn.getCurrentNumeralValue(0));
76+
assertEquals(2, mrn.getCurrentNumeralValue(1));
77+
assertEquals(0, mrn.getCurrentNumeralValue(2));
78+
assertEquals(1, mrn.getCurrentNumeralValue(3));
79+
//
80+
mrn = new MixedRadixNumber(17, new int[] {3, 3, 2, 2});
81+
assertEquals(2, mrn.getCurrentNumeralValue(0));
82+
assertEquals(2, mrn.getCurrentNumeralValue(1));
83+
assertEquals(1, mrn.getCurrentNumeralValue(2));
84+
assertEquals(0, mrn.getCurrentNumeralValue(3));
85+
//
86+
mrn = new MixedRadixNumber(8, new int[] {3, 3, 2, 2});
87+
assertEquals(2, mrn.getCurrentNumeralValue(0));
88+
assertEquals(2, mrn.getCurrentNumeralValue(1));
89+
assertEquals(0, mrn.getCurrentNumeralValue(2));
90+
assertEquals(0, mrn.getCurrentNumeralValue(3));
91+
//
92+
mrn = new MixedRadixNumber(359, new int[] {3, 4, 5, 6});
93+
assertEquals(2, mrn.getCurrentNumeralValue(0));
94+
assertEquals(3, mrn.getCurrentNumeralValue(1));
95+
assertEquals(4, mrn.getCurrentNumeralValue(2));
96+
assertEquals(5, mrn.getCurrentNumeralValue(3));
97+
//
98+
mrn = new MixedRadixNumber(359, new int[] {6, 5, 4, 3});
99+
assertEquals(5, mrn.getCurrentNumeralValue(0));
100+
assertEquals(4, mrn.getCurrentNumeralValue(1));
101+
assertEquals(3, mrn.getCurrentNumeralValue(2));
102+
assertEquals(2, mrn.getCurrentNumeralValue(3));
103+
}
104+
}
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
package aima.util;
2+
3+
import java.util.List;
4+
5+
/**
6+
* @author Ciaran O'Reilly
7+
* see: http://demonstrations.wolfram.com/MixedRadixNumberRepresentations/
8+
* for useful example.
9+
*/
10+
public class MixedRadixNumber extends Number {
11+
//
12+
private static final long serialVersionUID = 1L;
13+
//
14+
private long value = 0L;
15+
private long maxValue = 0L;
16+
private int[] radixs = null;
17+
private int[] currentNumeralValue = null;
18+
private boolean recalculate = true;
19+
20+
public MixedRadixNumber(long value, int[] radixs) {
21+
this.value = value;
22+
this.radixs = new int[radixs.length];
23+
System.arraycopy(radixs, 0, this.radixs, 0, radixs.length);
24+
calculateMaxValue();
25+
}
26+
27+
public MixedRadixNumber(long value, List<Integer> radixs) {
28+
this.value = value;
29+
this.radixs = new int[radixs.size()];
30+
System.arraycopy(radixs.toArray(), 0, this.radixs, 0, this.radixs.length);
31+
calculateMaxValue();
32+
}
33+
34+
public long getMaxAllowedValue() {
35+
return maxValue;
36+
}
37+
38+
public boolean increment() {
39+
if (value < maxValue) {
40+
value++;
41+
recalculate = true;
42+
return true;
43+
}
44+
45+
return false;
46+
}
47+
48+
public boolean decrement() {
49+
if (value > 0) {
50+
value--;
51+
recalculate = true;
52+
return true;
53+
}
54+
return false;
55+
}
56+
57+
public int getCurrentNumeralValue(int atPosition) {
58+
if (atPosition >= 0 && atPosition < radixs.length) {
59+
if (recalculate) {
60+
long quotient = value;
61+
for (int i = 0; i < radixs.length; i++) {
62+
if (0 != quotient) {
63+
currentNumeralValue[i] = (int) quotient % radixs[i];
64+
quotient = quotient / radixs[i];
65+
} else {
66+
currentNumeralValue[i] = 0;
67+
}
68+
69+
}
70+
recalculate = false;
71+
}
72+
return currentNumeralValue[atPosition];
73+
}
74+
throw new IllegalArgumentException("Argument atPosition must be >=0 and < "+radixs.length);
75+
}
76+
77+
//
78+
// START-Number
79+
public int intValue() {
80+
return (int) longValue();
81+
}
82+
83+
public long longValue() {
84+
return value;
85+
}
86+
87+
public float floatValue() {
88+
return longValue();
89+
}
90+
91+
public double doubleValue() {
92+
return longValue();
93+
}
94+
// END-Number
95+
//
96+
97+
//
98+
// PRIVATE
99+
//
100+
private void calculateMaxValue() {
101+
if (0 == radixs.length) {
102+
throw new IllegalArgumentException("At least 1 radix must be defined.");
103+
}
104+
for (int i = 0; i < radixs.length; i++) {
105+
if (radixs[i] < 2) {
106+
throw new IllegalArgumentException("Invalid radix, must be >= 2");
107+
}
108+
}
109+
110+
// Calcualte the maxValue allowed
111+
maxValue = radixs[0];
112+
for (int i = 1; i < radixs.length; i++) {
113+
maxValue *= radixs[i];
114+
}
115+
maxValue -= 1;
116+
117+
if (value > maxValue) {
118+
throw new IllegalArgumentException("The value ["+value+"] cannot be represented with the radixs provided, max value is " + maxValue);
119+
}
120+
121+
currentNumeralValue = new int[radixs.length];
122+
}
123+
}

0 commit comments

Comments
 (0)