@Test public void testMutation() { Portfolio portfolio = new Portfolio(3); portfolio.getPortfolio().set(0, 0.25); portfolio.getPortfolio().set(1, 0.5); portfolio.getPortfolio().set(2, 0.25); GeneticAlgUtils.mutate(portfolio); System.out.println(portfolio); }
@Test public void testFitnessFunctionCalculation() { GeneticAlgorithmImpl gen = new GeneticAlgorithmImpl(2, 2, 0.2, 0.1, 0.3, dataSource); Portfolio portfolio = new Portfolio(2); portfolio.getPortfolio().set(0, 0.5); portfolio.getPortfolio().set(1, 0.5); System.out.println(gen.calculateFitness(portfolio, 0)); assertEquals(1.0, gen.calculateFitness(portfolio, 0), 0.01); }
@Test public void testCrossover() { Portfolio parentA = new Portfolio(2); parentA.getPortfolio().set(0, 0.5); parentA.getPortfolio().set(1, 0.5); Portfolio parentB = new Portfolio(2); parentB.getPortfolio().set(0, 1.0); parentB.getPortfolio().set(1, 0.0); Portfolio child = GeneticAlgorithmImpl.crossover(parentA, parentB); assertEquals(0.75, child.getPortfolio().get(0), 0.001); assertEquals(0.25, child.getPortfolio().get(1), 0.001); }