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.examples.functions;
017
018 import java.awt.BorderLayout;
019
020 import javax.swing.JFrame;
021 import javax.swing.JSlider;
022 import javax.swing.event.ChangeEvent;
023 import javax.swing.event.ChangeListener;
024
025 import de.kumpe.hadooptimizer.EaOptimizerConfiguration;
026 import de.kumpe.hadooptimizer.Evaluator;
027 import de.kumpe.hadooptimizer.examples.OptimizerExample;
028 import de.kumpe.hadooptimizer.impl.GaussianMutator;
029
030 /**
031 * Tries to minimize a polygon.
032 *
033 * @author <a href="http://kumpe.de/christian/java">Christian Kumpe</a>
034 */
035 public final class Rastrigin extends OptimizerExample<double[]> {
036 private final static class RastriginEvaluator implements
037 Evaluator<double[]> {
038 private static final long serialVersionUID = 1L;
039
040 @Override
041 public double evaluate(final double[] individual) {
042 double sum = 0;
043 for (int i = 0; i < individual.length; i++) {
044 final double xi = individual[i];
045 sum += xi * xi - 10 * Math.cos(2 * Math.PI * xi);
046 }
047 return 10 * individual.length + sum;
048 }
049 }
050
051 @Override
052 protected EaOptimizerConfiguration<double[]> createEaOptimizerConfiguration() {
053 final EaOptimizerConfiguration<double[]> conf = new EaOptimizerConfiguration<double[]>();
054 initConfiguration(conf, new double[] { -5.12, 5.12 });
055 conf.setMutator(new GaussianMutator(standardDeviation));
056 conf.setEvaluator(new RastriginEvaluator());
057
058 if (commandLine.hasOption(OPTION_GUI)) {
059 final JFrame frame = new JFrame("RandomPaint in "
060 + getClass().getSimpleName());
061 final ResultReporterPanel resultReporter = new ResultReporterPanel();
062 frame.add(resultReporter, BorderLayout.CENTER);
063 final JSlider slider = new JSlider(0, 100, 50);
064 slider.addChangeListener(new ChangeListener() {
065 @Override
066 public void stateChanged(final ChangeEvent e) {
067 resultReporter.setScale(1d / slider.getValue());
068 }
069 });
070 frame.add(slider, BorderLayout.SOUTH);
071
072 frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
073 frame.pack();
074 frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
075 frame.setVisible(true);
076 addReporter(resultReporter);
077 }
078
079 return conf;
080 }
081
082 @Override
083 protected String getVersionInfo() {
084 return "$Id: Rastrigin.java 3890 2011-04-20 14:58:14Z baumbart $";
085 }
086 }