source: src/main/java/agents/anac/y2019/harddealer/math3/genetics/Chromosome.java

Last change on this file was 200, checked in by Katsuhide Fujita, 5 years ago

Add ANAC 2019 agents

  • Property svn:executable set to *
File size: 3.9 KB
Line 
1/*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17package agents.anac.y2019.harddealer.math3.genetics;
18
19/**
20 * Individual in a population. Chromosomes are compared based on their fitness.
21 * <p>
22 * The chromosomes are IMMUTABLE, and so their fitness is also immutable and
23 * therefore it can be cached.
24 *
25 * @since 2.0
26 */
27public abstract class Chromosome implements Comparable<Chromosome>,Fitness {
28 /** Value assigned when no fitness has been computed yet. */
29 private static final double NO_FITNESS = Double.NEGATIVE_INFINITY;
30
31 /** Cached value of the fitness of this chromosome. */
32 private double fitness = NO_FITNESS;
33
34 /**
35 * Access the fitness of this chromosome. The bigger the fitness, the better the chromosome.
36 * <p>
37 * Computation of fitness is usually very time-consuming task, therefore the fitness is cached.
38 *
39 * @return the fitness
40 */
41 public double getFitness() {
42 if (this.fitness == NO_FITNESS) {
43 // no cache - compute the fitness
44 this.fitness = fitness();
45 }
46 return this.fitness;
47 }
48
49 /**
50 * Compares two chromosomes based on their fitness. The bigger the fitness, the better the chromosome.
51 *
52 * @param another another chromosome to compare
53 * @return
54 * <ul>
55 * <li>-1 if <code>another</code> is better than <code>this</code></li>
56 * <li>1 if <code>another</code> is worse than <code>this</code></li>
57 * <li>0 if the two chromosomes have the same fitness</li>
58 * </ul>
59 */
60 public int compareTo(final Chromosome another) {
61 return Double.compare(getFitness(), another.getFitness());
62 }
63
64 /**
65 * Returns <code>true</code> iff <code>another</code> has the same representation and therefore the same fitness. By
66 * default, it returns false -- override it in your implementation if you need it.
67 *
68 * @param another chromosome to compare
69 * @return true if <code>another</code> is equivalent to this chromosome
70 */
71 protected boolean isSame(final Chromosome another) {
72 return false;
73 }
74
75 /**
76 * Searches the <code>population</code> for another chromosome with the same representation. If such chromosome is
77 * found, it is returned, if no such chromosome exists, returns <code>null</code>.
78 *
79 * @param population Population to search
80 * @return Chromosome with the same representation, or <code>null</code> if no such chromosome exists.
81 */
82 protected Chromosome findSameChromosome(final Population population) {
83 for (Chromosome anotherChr : population) {
84 if (this.isSame(anotherChr)) {
85 return anotherChr;
86 }
87 }
88 return null;
89 }
90
91 /**
92 * Searches the population for a chromosome representing the same solution, and if it finds one,
93 * updates the fitness to its value.
94 *
95 * @param population Population to search
96 */
97 public void searchForFitnessUpdate(final Population population) {
98 Chromosome sameChromosome = findSameChromosome(population);
99 if (sameChromosome != null) {
100 fitness = sameChromosome.getFitness();
101 }
102 }
103
104}
Note: See TracBrowser for help on using the repository browser.