examples
Class MinimizingMakeChange
java.lang.Object
examples.MinimizingMakeChange
- public class MinimizingMakeChange
- extends java.lang.Object
This class provides an implementation of the classic "Make change" problem
using a genetic algorithm. The goal of the problem is to provide a
specified amount of change (from a cash purchase) in the fewest coins
possible. This example implementation uses American currency (quarters,
dimes, nickels, and pennies).
This example may be seen as somewhat significant because it demonstrates
the use of a genetic algorithm in a less-than-optimal problem space.
The genetic algorithm does best when there is a smooth slope of fitness
over the problem space towards the optimum solution. This problem exhibits
a more choppy space with more local optima. However, as can be seen from
running this example, the genetic algorithm still will get the correct
answer virtually everytime.
Method Summary |
static void |
main(java.lang.String[] args)
Main method. |
static void |
makeChangeForAmount(int a_targetChangeAmount)
Executes the genetic algorithm to determine the minimum number of
coins necessary to make up the given target amount of change. |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
MinimizingMakeChange
public MinimizingMakeChange()
makeChangeForAmount
public static void makeChangeForAmount(int a_targetChangeAmount)
throws java.lang.Exception
- Executes the genetic algorithm to determine the minimum number of
coins necessary to make up the given target amount of change. The
solution will then be written to System.out.
- Parameters:
a_targetChangeAmount
- The target amount of change for which this
method is attempting to produce the minimum
number of coins.
- Throws:
java.lang.Exception
main
public static void main(java.lang.String[] args)
- Main method. A single command-line argument is expected, which is the
amount of change to create (in other words, 75 would be equal to 75
cents).
- Parameters:
args
- the command-line arguments.