id
stringlengths
36
36
text
stringlengths
1
1.25M
da4f1d93-a8d2-4b89-b77d-dd8854640d96
@Override public InputStream getInputStream() { return inputStream; }
2b412404-9bc6-4863-a3f9-adbdc8d5e4bb
@Override public OutputStream getOutputStream() { return outputStream; }
01821dbd-895d-4c39-8703-fc26f21e8189
@Override public void close() throws IOException { if (port != null) { port.close(); port = null; } }
c87af0bf-28c3-49f2-b10e-0f562021b942
InputStream getInputStream();
e700ddfa-85b9-43d4-a4b6-03ceb6778a64
OutputStream getOutputStream();
70c581b5-c61f-479a-adee-eb3b951e6021
void close() throws IOException;
f66db923-5148-4918-9c8f-9ede124bb6ca
@Before public void setUp() { testChromosomes = new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9}; instance = new Genom(testChromosomes); }
82fac32d-ee0f-4553-961b-6c9fc79e96fd
@Test public void testCreateRandom() { Genom result = Genom.createRandom(7, 250, 250); assertNotNull(result); assertTrue(result.isValid()); }
92fe93c1-541c-4fe3-bca5-b62c56425bc7
@Test public void testGetChromosomes() { int[] result = instance.getChromosomes(); assertEquals(testChromosomes, result); }
e4235ddf-420c-48ea-8360-042e27976522
@Test public void testGetChromosom() { assertEquals(5, instance.getChromosom(4)); }
a15c4306-0056-4cd9-bd61-3de31af735de
@Test public void testSetChromosom() { instance.setChromosom(4, 666); assertEquals(666, instance.getChromosomes()[4]); }
deefd2e9-aee4-483f-a7f4-9223384c0a7a
@Test public void testSetChromosomes() { instance.setChromosomes(testChromosomes); assertTrue(Arrays.equals(testChromosomes, instance.getChromosomes())); }
53027f6d-412a-4b09-8c8d-cabb11611ea4
@Test public void testCrossover() { int[] testChromosomes1 = new int[]{1, 2, 3, 4, 5, 6, 7}; int[] testChromosomes2 = new int[]{10, 20, 30, 40, 50, 60, 70}; Genom parent1 = new GenomWithoutRandomness(testChromosomes1); Genom parent2 = new Genom(testChromosomes2); Genom child = parent1.crossover(parent2); int[] newChromosome = new int[]{1, 2, 3, 4, 50, 60, 70}; assertThat(newChromosome, is(child.getChromosomes())); }
5e5663b6-fa57-4c4d-b358-61894cc25be6
@Test public void testReduceRandomChromosom() { instance = new GenomWithoutRandomness(testChromosomes); assertEquals(5, instance.getChromosom(4)); instance.reduceRandomChromosom(); assertEquals(4, instance.getChromosom(4)); }
84d1ee96-8126-4c22-bd0c-cfc825355edd
@Test public void testIncreaseRandomChromosom() { instance = new GenomWithoutRandomness(testChromosomes); assertEquals(5, instance.getChromosom(4)); instance.increaseRandomChromosom(); assertEquals(6, instance.getChromosom(4)); instance.setChromosom(4, 155); instance.increaseRandomChromosom(); assertTrue(instance.isValid()); }
57288cd9-5f04-43ae-9264-63783485cfef
@Test public void testSwitchRandomChromosomes() { instance = new GenomWithoutRandomness(testChromosomes); instance.switchRandomChromosomes(); int[] expected = new int[] {1,2,3,4,6,5,7,8,9}; assertTrue(Arrays.equals(expected, instance.getChromosomes())); }
08c533b6-9dca-4a26-9b3c-fda4b26fdec4
public GenomWithoutRandomness(int[] chromosomes) { super(chromosomes); }
27801446-e14c-4d71-85d3-2cc00137aa8d
@Override protected int getRandomChromosomPosition() { int result = randomList[position]; position = (position + 1) % randomList.length; return result; }
a56f7c0e-42c4-4f9b-879f-1bf7967599e7
@Before public void setUp() { population = new Population(); individuals = new ArrayList(Arrays.asList(ind1, ind2, ind3, ind4)); population.individuals = individuals; }
edf21254-7e8e-4275-9d09-3bed38c91921
@Test public void testIterator() { Iterator<Individual> referenceIterator = individuals.iterator(); Iterator<Individual> testIterator = population.iterator(); while (testIterator.hasNext() && referenceIterator.hasNext()) { assertThat(testIterator.next(), is(referenceIterator.next())); } assertTrue(!referenceIterator.hasNext() && !testIterator.hasNext()); }
91f83f9a-d9dc-47e9-8773-9942b0e64db5
@Test public void testAddIndividual() { Individual newIndividual = mock(Individual.class); population.addIndividual(newIndividual); assertThat(population.individuals, hasItem(newIndividual)); }
9761e424-65d3-4c06-b1c4-f948c40af22f
@Test public void testSize() { assertThat(population.size(), is(individuals.size())); individuals.remove(2); assertThat(population.size(), is(individuals.size())); }
4d5e7780-ed34-468a-8b41-fecf38daec29
@Test public void testSizeOfNewPopIs0() { population = new Population(); assertThat(population.size(), is(0)); }
d70bc013-21bc-4401-a0fd-592fabf9346b
@Test public void testGetAge() { population = new Population(null, 5); assertThat(population.getAge(), is(5)); }
cae0671e-780f-4af7-a948-f3e3b6018cbb
@Test public void testAgeOfNewPopIs0() { population = new Population(); assertThat(population.getAge(), is(0)); }
6bf8ee4e-5d82-4e9a-9eb1-a08193931fbc
@Test public void testGetIndividuals() { assertThat(population.getIndividuals(), is(individuals)); }
676a87f3-35a4-4ffd-9ad0-1409613b0cd2
@Test public void testAdd() { Individual newIndividual = new Individual(null); assertThat(population.getIndividuals(), is(individuals)); population.add(Arrays.asList(newIndividual)); assertThat(population.individuals, hasItem(newIndividual)); }
d1807e7b-b0a7-4fb1-b45c-f10a4ec39a95
@Before public void setUp() { MockitoAnnotations.initMocks(this); ea = new EvolutionAlgorithm(config); when(config.getRandom()).thenReturn(random); mutations = Arrays.asList(mut1, mut2, mut3, mut4); when(config.getMutations()).thenReturn(mutations); children = Arrays.asList(child1, child2, child3, child4); }
bcf27473-e168-4375-a8b1-db7aec933ac2
@Test public void testChooseMutation() { when(random.getNextInt(anyInt())).thenReturn(0).thenReturn(1).thenReturn(2).thenReturn(3); Mutation result = ea.chooseMutation(); assertThat(result, is(mut1)); result = ea.chooseMutation(); assertThat(result, is(mut2)); result = ea.chooseMutation(); assertThat(result, is(mut3)); result = ea.chooseMutation(); assertThat(result, is(mut4)); }
01305be3-26c6-415b-84f5-0823f3531c7c
@Test public void testChooseMutationRandomIsMutationsSize() { ea.chooseMutation(); ArgumentCaptor<Integer> argument = ArgumentCaptor.forClass(Integer.class); verify(random).getNextInt(argument.capture()); assertThat(argument.getValue(), is(mutations.size())); }
c11f111f-2646-46db-9071-5dbe40bd1585
@Test public void testMutationTakesPlace() { when(random.getNextPercentage()).thenReturn(50); // 100% - immer mutieren when(config.getPropabilityOfMutation()).thenReturn(100); boolean result = ea.mutationTakesPlace(); assertThat(result, is(true)); // 0% - nie mutieren when(config.getPropabilityOfMutation()).thenReturn(0); result = ea.mutationTakesPlace(); assertThat(result, is(false)); when(random.getNextPercentage()).thenReturn(5); when(config.getPropabilityOfMutation()).thenReturn(10); result = ea.mutationTakesPlace(); assertThat(result, is(true)); when(random.getNextPercentage()).thenReturn(10); when(config.getPropabilityOfMutation()).thenReturn(5); result = ea.mutationTakesPlace(); assertThat(result, is(false)); }
d1f81191-6ffc-45d2-8854-b6543c8d0df3
@Test public void testTerminationCriteriaNotMet() { when(config.terminationCriteriaMet(any(Population.class))).thenReturn(false); assertThat(ea.terminationCriteriaNotMet(), is(true)); when(config.terminationCriteriaMet(any(Population.class))).thenReturn(true); assertThat(ea.terminationCriteriaNotMet(), is(false)); }
4a07b49e-c0b4-4395-ab82-8d6bebbe8c66
@Test public void testSelectNextGeneration() { Population oldPopulation = mock(Population.class); Population newPopulation = mock(Population.class); ea.population = oldPopulation; Selector selector = mock(Selector.class); when(selector.selectNextGeneration(oldPopulation)).thenReturn(newPopulation); when(config.getSelector()).thenReturn(selector); ea.selectNextGeneration(); assertThat(ea.population, is(newPopulation)); }
8844d263-5019-4f1c-ad1b-47dd15cea3e8
@Test public void testMutate() { when(config.getPropabilityOfMutation()).thenReturn(100); when(random.getNextPercentage()).thenReturn(50); when(random.getNextInt(anyInt())).thenReturn(0).thenReturn(1).thenReturn(2).thenReturn(1); ea.currentChildren = children; ea.mutate(); verify(mut1).mutate(child1); verify(mut2).mutate(child2); verify(mut3).mutate(child3); verify(mut2).mutate(child4); }
c482a70c-eed8-41d4-9d0c-235aec8c279f
@Test public void testMutate2() { when(config.getPropabilityOfMutation()).thenReturn(50); when(random.getNextPercentage()).thenReturn(30).thenReturn(60).thenReturn(30); when(random.getNextInt(anyInt())).thenReturn(0).thenReturn(1).thenReturn(2); ea.currentChildren = children; ea.mutate(); verify(mut1).mutate(child1); verify(mut2).mutate(child3); verify(mut3).mutate(child4); }
89520d72-ce16-4589-a7fe-a979ed360f37
@Test public void testRecombine() { Selector selector = mock(Selector.class); when(config.getSelector()).thenReturn(selector); List<List<Individual>> parents = mockParents(); when(selector.selectParents(any(Population.class))).thenReturn(parents); GeneticOperator operator = mock(GeneticOperator.class); when(config.getRecombinationOperator()).thenReturn(operator); when(operator.operate(anyList())).thenReturn(child1, child2, child3, child4); ea.recombine(); assertThat(ea.currentChildren, is(children)); }
7f3341f6-7ba4-4cda-87df-bd697a3c8ee7
private List<List<Individual>> mockParents() { List<List<Individual>> parents = new ArrayList<List<Individual>>(children.size()); List<Individual> parent = mock(List.class); for (int i = 0; i < children.size(); i++) { parents.add(parent); } return parents; }
407813e7-e740-460d-93ec-387f14d44991
public EvolutionAlgorithm(Configuration config) { this.config = config; log = Logger.getLogger(getClass()); }
530428c5-1e8b-4d45-afe7-7a8a149bc7ba
public void evolve() { initPopulation(); calculateFitness(); sort(); do { recombine(); mutate(); addChildren(); calculateFitness(); sort(); selectNextGeneration(); } while (terminationCriteriaNotMet()); }
7b81356e-d8b0-4e36-869c-653949b212dc
protected void initPopulation() { log.trace("init Population"); population = config.getFactory().createInitialPopulation(config.getPopulationSize()); log.trace(population); }
055da6f9-47ac-4ad9-9947-822bc9c3d8ee
protected void calculateFitness() { log.trace("calculate Fitness"); config.getFitnessFunction().evaluate(population); log.debug(population); }
b146b97c-0608-46ec-b53e-fad3ba651f10
protected void recombine() { log.trace("recombine"); currentChildren = new ArrayList<Individual>(config.getNumberOfChildren()); List<List<Individual>> allParents = config.getSelector().selectParents(population); for (List<Individual> parents : allParents) { Individual child = config.getRecombinationOperator().operate(parents); currentChildren.add(child); } }
4a821e43-a5d9-4b49-8b68-8e436b476492
protected void mutate() { log.trace("mutate"); for (Individual individual : currentChildren) { if (mutationTakesPlace()) { Mutation m = chooseMutation(); m.mutate(individual); } } log.trace("Children: " + currentChildren); }
5630a39e-21c4-4f57-9e5b-80611db88e49
protected void selectNextGeneration() { log.trace("select next generation"); population = config.getSelector().selectNextGeneration(population); log.debug(population); }
d7e20c4b-2643-44f1-a1ae-c9079cc3edba
protected boolean terminationCriteriaNotMet() { return !config.terminationCriteriaMet(population); }
36e5adca-7656-413d-968f-cf5b08292524
protected boolean mutationTakesPlace() { int propability = config.getPropabilityOfMutation(); return config.getRandom().getNextPercentage() <= propability; }
a6c56be5-faeb-4b18-ad96-a1edb2a65faa
protected Mutation chooseMutation() { List<Mutation> mutations = config.getMutations(); int number = config.getRandom().getNextInt(mutations.size()); return mutations.get(number); }
0dd63432-034d-42df-9f85-3eadfdd3a172
private void sort() { Comparator<Individual> comparator = config.getFitnessComparator(); Collections.sort(population.individuals, comparator); }
2d786b42-0109-47c3-8ef6-0d56810d70a2
private void addChildren() { population.add(currentChildren); }
d27a3aa1-1ec2-41de-9ade-58c8df4ae2eb
public Individual(Genom genom) { this.genom = genom; this.fitness = null; }
29882698-fed0-470b-ba95-4cae341d4201
public static Individual createRandom(int numberOfChromosomes, int maxCrossSum, int maxSingleValue) { return new Individual(Genom.createRandom(numberOfChromosomes, maxCrossSum, maxSingleValue)); }
13e8f7e5-1a2c-4869-bc1a-fc5b64c67c40
public Genom getGenom() { return genom; }
ee78e662-2c5d-4f6a-b21b-5e7ca71bcb8a
public void setGenom(Genom genom) { this.genom = genom; }
56c7af7e-ce0e-4069-b502-a5473f1bbb4b
public Integer getFitness() { return fitness; }
c2ecc442-258a-4cae-b1ca-82516af1ce2a
public void setFitness(Integer fitness) { this.fitnessSet = true; this.fitness = fitness; }
389c20ba-cf76-4b5c-8d27-937a8e7980d3
public boolean isFitnessSet() { return fitnessSet; }
76a9b035-03cb-48f2-ac01-5afda5d63a94
@Override public String toString() { StringBuilder sb = new StringBuilder(); sb.append("{ -"); sb.append(fitness); sb.append(" ("); sb.append(customPayload); sb.append(")- "); sb.append(genom.toString()); sb.append("} "); return sb.toString(); }
08297a7a-6a84-4f8b-bed4-fd55a40384eb
@Override public boolean equals(Object obj) { if (obj == null) { return false; } if (getClass() != obj.getClass()) { return false; } final Individual other = (Individual) obj; if (this.genom != other.genom && (this.genom == null || !this.genom.equals(other.genom))) { return false; } return true; }
eccf68d0-e7be-4048-9f09-ac94903500ea
@Override public int hashCode() { int hash = 7; hash = 79 * hash + (this.genom != null ? this.genom.hashCode() : 0); return hash; }
67a7bb10-dfb1-42c2-8748-666e44009554
public Object getCustomPayload() { return customPayload; }
82981d07-d0b2-4696-acbc-70621d48800c
public void setCustomPayload(Object customPayload) { this.customPayload = customPayload; }
18a3dfe8-e945-48d8-9edf-274d76bdeb41
public abstract Individual createRandomIndividual();
a7262d5c-aa5b-4fd4-87be-96205425369f
public Population createInitialPopulation(int populationSize) { Population population = new Population(); for (int i = 0; i < populationSize; i++) { Individual ind = createRandomIndividual(); population.addIndividual(ind); } return population; }
c94c221c-68b2-4cf4-991e-d4a285ebdf4e
Individual operate(List<Individual> selectedIndividuals);
884d661d-15d5-46e9-85da-2b427fd32eb2
public abstract void evaluate(Individual ind);
22266d9d-7983-4428-aec8-f934acb52887
public void evaluate(Population population) { for (Individual individual : population) { evaluate(individual); } }
0583fe57-33f4-4814-ac31-8a9d0c59b690
public Genom(int[] chromosomes) { this.chromosomes = chromosomes; }
2ad9251a-0193-4ed6-99c4-4052a3f40777
public Genom(int numberOfChromosomes, int maxCrossSum, int maxSingleValue) { this.numberOfChromosomes = numberOfChromosomes; this.maxCrossSum = maxCrossSum; this.maxSingleValue = maxSingleValue; this.chromosomes = new int[numberOfChromosomes]; }
d9f5351f-7255-4e76-941b-858e6102e9d3
public static Genom createRandom(int numberOfChromosomes, int maxCrossSum, int maxSingleValue) { Genom instance = new Genom(numberOfChromosomes, maxCrossSum, maxSingleValue); instance.createRandomChromosomes(); return instance; }
4e894d28-9920-48c4-88a5-a5cbedbaa8eb
public int[] getChromosomes() { return chromosomes; }
cd5874db-4074-49f5-bab3-ffb1adf47040
public int getChromosom(int position) { return chromosomes[position]; }
a96dab58-e3dd-41ba-a8c3-6456cfde0bef
public void setChromosom(int position, int value) { chromosomes[position] = value; }
843f885b-b21c-4d53-b0f9-7c267d258be7
public void setChromosomes(int[] chromosomes) { this.chromosomes = chromosomes; }
3a4e775e-aca0-437a-b0e6-ddcafc1cb50b
public Genom crossover(Genom other) { int[] newChromosomes = new int[numberOfChromosomes]; int randomPosition = getRandomChromosomPosition(); firstPartFromThisGenom(newChromosomes, randomPosition); secondPartFromOtherGenom(other, randomPosition, newChromosomes); Genom result = new Genom(newChromosomes); return result; }
6d70cc6c-e830-4ecc-ad30-4fe5086d9554
public void reduceRandomChromosom() { if (crossSum() < 2) { return; } boolean end = false; do { int random = getRandomChromosomPosition(); if (chromosomes[random] > 0) { chromosomes[random] -= 1; end = true; } } while (!end); }
53426bfe-f634-4e2d-b3de-c6d4af38ebee
public void increaseRandomChromosom() { if (crossSum() >= maxCrossSum) { return; } boolean end = false; do { int random = getRandomChromosomPosition(); if (chromosomes[random] < maxSingleValue) { chromosomes[random] += 1; end = true; } } while (!end); }
fb602272-ac2f-4346-9c26-05f67a1d42b5
public void switchRandomChromosomes() { int rand1 = getRandomChromosomPosition(); int rand2 = getDifferentRandomChromosomPosition(rand1); int tmp = chromosomes[rand1]; chromosomes[rand1] = chromosomes[rand2]; chromosomes[rand2] = tmp; }
da475ef9-98e4-4761-85fc-097ec6adb3fd
public boolean isValid() { int crossSum = crossSum(); if (crossSum > 0 && crossSum <= maxCrossSum) { return true; } return false; }
7414f25d-c2dc-45f2-86b8-87af9734d107
@Override public String toString() { return Arrays.toString(chromosomes); }
a013130c-13f4-4b71-8a87-e5bed732c4ee
protected int getRandomChromosomPosition() { return getRandomInt(chromosomes.length); }
fa366054-90ec-40e3-85be-53101ebeb922
private int getDifferentRandomChromosomPosition(int rand1) { int rand2 = getRandomChromosomPosition(); while (rand1 == rand2) { rand2 = getRandomChromosomPosition(); } return rand2; }
d3c53c5e-4dfb-4f78-8dd0-e73001f09b3d
protected int createRandomChromosomeValue() { return (int) (getRandom() * maxSingleValue); }
199cd001-4aea-4e8e-b6bd-51d6e0f979cf
protected double getRandom() { return Math.random(); }
fa0781fa-9304-4bbb-9125-b06e4cd3f9f5
protected int getRandomInt(int ceiling) { return (int) (getRandom() * ceiling); }
ec33d10c-e19a-4631-a655-7b46ce65c9ba
protected void createRandomChromosomes() { int[] newChromosomes = new int[numberOfChromosomes]; int crossSum = 0; boolean startFromEnd = false; if (getRandom() < 0.5) { startFromEnd = true; } for (int i = 0; i < newChromosomes.length; i++) { if (getRandom() < 0.25) { continue; } int nextValue = createRandomChromosomeValue(); if (crossSum + nextValue > maxCrossSum) { nextValue = maxCrossSum - crossSum; } crossSum += nextValue; int position = i; if (startFromEnd) { position = newChromosomes.length - (i + 1); } newChromosomes[position] = nextValue; } this.chromosomes = newChromosomes; }
41833bbb-b7e2-4800-bbe6-f6fde7a54f94
public int crossSum() { int result = 0; for (int chromosom : chromosomes) { result += chromosom; } return result; }
8b5ceafb-a9a5-4271-a8c7-e48bb0b3333f
private void secondPartFromOtherGenom(Genom other, int randomPosition, int[] newChromosomes) { System.arraycopy(other.chromosomes, randomPosition, newChromosomes, randomPosition, newChromosomes.length - randomPosition); }
c619a4db-418c-4f42-a9ec-8eb59302fcf3
private void firstPartFromThisGenom(int[] newChromosomes, int randomPosition) { System.arraycopy(this.chromosomes, 0, newChromosomes, 0, randomPosition); }
5b760b0f-a20b-40cb-b221-33ba6d364ba7
public int length() { return this.chromosomes.length; }
edabf32d-b8ed-498c-a432-4f695f47c16d
@Override public boolean equals(Object obj) { if (obj == null) { return false; } if (getClass() != obj.getClass()) { return false; } final Genom other = (Genom) obj; if (!Arrays.equals(this.chromosomes, other.chromosomes)) { return false; } return true; }
63b53035-74c9-43fc-9946-3acde35c3fdc
@Override public int hashCode() { int hash = 3; hash = 59 * hash + Arrays.hashCode(this.chromosomes); return hash; }
598134a9-76ce-4929-9d19-e30fe56719f3
public Random() { this.random = new java.util.Random(); }
5d7abbba-c9cf-4149-b8b2-7ff0e6e8399f
public int getNextPercentage() { return getNextInt(100) + 1; }
44aedcb1-e624-437f-94d6-e47c6153705f
public int getNextInt(int max) { return random.nextInt(max); }
3c1ccf0c-8b1a-47f0-81b6-5acfc8361442
List<List<Individual>> selectParents(Population fromPopulation);
431b942e-44b0-43b6-b048-256ee8b50b3d
Population selectNextGeneration(Population fromPopulation);
1c7c2134-31a4-49a9-8bf6-cca7cd7ddb60
public abstract int getNumberOfChildren();
8c3e872e-87f0-4e67-b821-317108a75820
public abstract int getNumberOfParents();
bf51457a-4590-4e3d-b7d0-2cd858f5ce47
public abstract int getPopulationSize();
f6618aff-09a8-443a-ad4f-98598e7c698f
public abstract int getPropabilityOfMutation();