001    /*
002     * Copyright 2011 Christian Kumpe http://kumpe.de/christian/java
003     *
004     * Licensed under the Apache License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     *     http://www.apache.org/licenses/LICENSE-2.0
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package de.kumpe.hadooptimizer;
017    
018    /**
019     * A configuration for evolution strategies. The individuals' type of evolution
020     * strategies is always {@link EsIndividual}.
021     * <p>
022     * Additionally to the components in {@link EaOptimizerConfigurationBase} it
023     * contains:
024     * <ul>
025     * <li>The number of {@link #setOffspring(int) offsprings} that will be created
026     * per parent.
027     * </ul>
028     * 
029     * For an E(μ+/,λ)-evolution strategy these configuration has the following
030     * corresponding parameters:
031     * <ul>
032     * <li>μ: the number of {@link #setParents(int) parents}.
033     * <li>+ or ,: the {@link #setPreserveParents(boolean) preserveParents} flag to
034     * indicate that parents should be preserved (plus-selection) or discarded
035     * (comma-selection).
036     * <li>λ: the number of {@link #setOffspring(int) offspring} created per parent.
037     * </ul>
038     * 
039     * @author <a href="http://kumpe.de/christian/java">Christian Kumpe</a>
040     */
041    public class EsOptimizerConfiguration extends
042                    EaOptimizerConfigurationBase<EsIndividual> {
043            private static final long serialVersionUID = 1L;
044    
045            private int offspring = 1;
046    
047            @Override
048            public EsOptimizerConfiguration clone() {
049                    return (EsOptimizerConfiguration) super.clone();
050            }
051    
052            /**
053             * @return the number of offspring
054             * @see #setOffspring(int)
055             */
056            public final int getOffspring() {
057                    assert 0 < offspring;
058    
059                    return offspring;
060            }
061    
062            /**
063             * Sets the number of offspring for the evolution strategy.
064             * <p>
065             * For an E(μ+/,λ)-evolution strategy, this sets the λ.
066             * 
067             * @param offspring
068             *            the number of offspring
069             * 
070             * @throws IllegalArgumentException
071             *             if {@code offspring} is zero or negative
072             */
073            public final void setOffspring(final int offspring) {
074                    if (0 >= offspring) {
075                            throw new IllegalArgumentException(
076                                            "offspring must be greater than zero");
077                    }
078    
079                    this.offspring = offspring;
080            }
081    
082            @Override
083            public int hashCode() {
084                    final int prime = 31;
085                    int result = super.hashCode();
086                    result = prime * result + offspring;
087                    return result;
088            }
089    
090            @Override
091            public boolean equals(final Object obj) {
092                    if (this == obj) {
093                            return true;
094                    }
095                    if (!super.equals(obj)) {
096                            return false;
097                    }
098                    if (!(obj instanceof EsOptimizerConfiguration)) {
099                            return false;
100                    }
101                    final EsOptimizerConfiguration other = (EsOptimizerConfiguration) obj;
102                    if (offspring != other.offspring) {
103                            return false;
104                    }
105                    return true;
106            }
107    
108            @Override
109            public String toString() {
110                    return "EsOptimizerConfiguration [halter=" + getHalter()
111                                    + ", offspring=" + offspring + ", mutator=" + getMutator()
112                                    + ", evaluator=" + getEvaluator() + ", parents=" + getParents()
113                                    + ", preserveParents=" + isPreserveParents() + "]";
114            }
115    }