Created
February 20, 2019 18:33
-
-
Save aidancbrady/c071b8013044268149d3d870d63c488f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Arrays; | |
import java.util.Random; | |
import dist.DiscreteDependencyTree; | |
import dist.DiscretePermutationDistribution; | |
import dist.DiscreteUniformDistribution; | |
import dist.Distribution; | |
import opt.DiscreteChangeOneNeighbor; | |
import opt.EvaluationFunction; | |
import opt.GenericHillClimbingProblem; | |
import opt.HillClimbingProblem; | |
import opt.NeighborFunction; | |
import opt.RandomizedHillClimbing; | |
import opt.SimulatedAnnealing; | |
import opt.SwapNeighbor; | |
import opt.example.*; | |
import opt.ga.CrossoverFunction; | |
import opt.ga.DiscreteChangeOneMutation; | |
import opt.ga.SingleCrossOver; | |
import opt.ga.GenericGeneticAlgorithmProblem; | |
import opt.ga.GeneticAlgorithmProblem; | |
import opt.ga.MutationFunction; | |
import opt.ga.StandardGeneticAlgorithm; | |
import opt.ga.SwapMutation; | |
import opt.ga.UniformCrossOver; | |
import opt.prob.GenericProbabilisticOptimizationProblem; | |
import opt.prob.MIMIC; | |
import opt.prob.ProbabilisticOptimizationProblem; | |
import shared.FixedIterationTrainer; | |
/** | |
* A test using the flip flop evaluation function | |
* @author Andrew Guillory gtg008g@mail.gatech.edu | |
* @version 1.0 | |
*/ | |
public class IterationTest { | |
/** Random number generator */ | |
private static final Random random = new Random(); | |
/** The number of items */ | |
private static final int NUM_ITEMS = 40; | |
/** The number of copies each */ | |
private static final int COPIES_EACH = 4; | |
/** The maximum value for a single element */ | |
private static final double MAX_VALUE = 50; | |
/** The maximum weight for a single element */ | |
private static final double MAX_WEIGHT = 50; | |
/** The maximum weight for the knapsack */ | |
private static final double MAX_KNAPSACK_WEIGHT = | |
MAX_WEIGHT * NUM_ITEMS * COPIES_EACH * .4; | |
public static void main(String[] args) { | |
int[] copies = new int[NUM_ITEMS]; | |
Arrays.fill(copies, COPIES_EACH); | |
double[] values = new double[NUM_ITEMS]; | |
double[] weights = new double[NUM_ITEMS]; | |
for (int i = 0; i < NUM_ITEMS; i++) { | |
values[i] = random.nextDouble() * MAX_VALUE; | |
weights[i] = random.nextDouble() * MAX_WEIGHT; | |
} | |
int[] ranges = new int[NUM_ITEMS]; | |
Arrays.fill(ranges, COPIES_EACH + 1); | |
EvaluationFunction ef = new KnapsackEvaluationFunction(values, weights, MAX_KNAPSACK_WEIGHT, copies); | |
Distribution odd = new DiscreteUniformDistribution(ranges); | |
NeighborFunction nf = new DiscreteChangeOneNeighbor(ranges); | |
MutationFunction mf = new DiscreteChangeOneMutation(ranges); | |
CrossoverFunction cf = new UniformCrossOver(); | |
Distribution df = new DiscreteDependencyTree(.1, ranges); | |
HillClimbingProblem hcp = new GenericHillClimbingProblem(ef, odd, nf); | |
GeneticAlgorithmProblem gap = new GenericGeneticAlgorithmProblem(ef, odd, mf, cf); | |
ProbabilisticOptimizationProblem pop = new GenericProbabilisticOptimizationProblem(ef, odd, df); | |
/*RandomizedHillClimbing rhc = new RandomizedHillClimbing(hcp); | |
for(int i = 0; i < 5000; i ++) { | |
rhc.train(); | |
System.out.println(ef.value(rhc.getOptimal())); | |
}*/ | |
/* SimulatedAnnealing sa = new SimulatedAnnealing(100, .95, hcp); | |
for(int i = 0; i < 5000; i ++) { | |
sa.train(); | |
System.out.println(ef.value(sa.getOptimal())); | |
}*/ | |
StandardGeneticAlgorithm ga = new StandardGeneticAlgorithm(200, 150, 25, gap); | |
for(int i = 0; i < 5000; i ++) { | |
ga.train(); | |
System.out.println(ef.value(ga.getOptimal())); | |
} | |
/* MIMIC mimic = new MIMIC(200, 100, pop); | |
for(int i = 0; i < 5000; i ++) { | |
mimic.train(); | |
System.out.println(ef.value(mimic.getOptimal())); | |
}*/ | |
} | |
private static long timestamp; | |
public static void startRecording(String s) { | |
timestamp = System.currentTimeMillis(); | |
System.out.println("Recording time for: " + s); | |
} | |
public static void stopRecording() { | |
long diff = System.currentTimeMillis()-timestamp; | |
System.out.println("Time elapsed: " + diff); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment