01: /*
02: * This file is part of JGAP.
03: *
04: * JGAP offers a dual license model containing the LGPL as well as the MPL.
05: *
06: * For licensing information please see the file license.txt included with JGAP
07: * or have a look at the top of class org.jgap.Chromosome which representatively
08: * includes the JGAP license policy applicable for any file delivered with JGAP.
09: */
10: package examples.supergene;
11:
12: /**
13: * Solve the change problem using force method. This class was used to
14: * verify if the solution exists in general.
15: *
16: * @author Audrius Meskauskas
17: */
18: public final class Force {
19: /** String containing the CVS revision. Read out via reflection!*/
20: private final static String CVS_REVISION = "$Revision: 1.2 $";
21:
22: public static boolean REPORT_ENABLED = true;
23:
24: private Force() {
25:
26: }
27:
28: /**
29: * Check the existence of a soulution.
30: *
31: * @param a_sum the sum needed
32: * @return true if the change can be expressed in coins, satisfying
33: * pennies mod 2 = nickels mod 2
34: *
35: * @author Audrius Meskauskas
36: */
37: public static boolean solve(int a_sum) {
38: for (int q = 0; q < 4; q++) {
39: for (int d = 0; d < 10; d++) {
40: for (int n = 0; n < 20; n++) {
41: for (int p = 0; p < 99; p++) {
42: if (AbstractSupergeneTest.amountOfChange(q, d,
43: n, p) == a_sum) {
44: if (p % 2 == n % 2) {
45: if (REPORT_ENABLED) {
46: System.out.println("Force " + a_sum
47: + ": " + q + " quarters "
48: + d + " dimes " + n
49: + " nickels " + p
50: + " pennies");
51: }
52: return true;
53: }
54: }
55: }
56: }
57: }
58: }
59: if (REPORT_ENABLED) {
60: System.out.println("Force " + a_sum + ": no solution");
61: }
62: return false;
63: }
64:
65: /**
66: * Test the Force method itself.
67: *
68: * @param args ignored
69: */
70: public static void main(String[] args) {
71: for (int i = 1; i <= 100; i++) {
72: solve(i);
73: }
74: }
75:
76: }
|