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 Rosenbrock extends OptimizerExample<double[]> {
036 private final static class RosenbrockEvaluator 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 - 1; i++) {
044 final double xi = individual[i];
045 final double xi1 = individual[i + 1];
046 sum += 100 * Math.pow(xi * xi - xi1, 2) + Math.pow(1 - xi, 2);
047 }
048 return sum;
049 }
050 }
051
052 @Override
053 protected EaOptimizerConfiguration<double[]> createEaOptimizerConfiguration() {
054 final EaOptimizerConfiguration<double[]> conf = new EaOptimizerConfiguration<double[]>();
055 initConfiguration(conf, new double[] { -2.048, -2.048 });
056 conf.setMutator(new GaussianMutator(standardDeviation));
057 conf.setEvaluator(new RosenbrockEvaluator());
058
059 if (commandLine.hasOption(OPTION_GUI)) {
060 final JFrame frame = new JFrame("RandomPaint in "
061 + getClass().getSimpleName());
062 final ResultReporterPanel resultReporter = new ResultReporterPanel();
063 frame.add(resultReporter, BorderLayout.CENTER);
064 final JSlider slider = new JSlider(0, 100, 50);
065 slider.addChangeListener(new ChangeListener() {
066 @Override
067 public void stateChanged(final ChangeEvent e) {
068 resultReporter.setScale(1d / slider.getValue());
069 }
070 });
071 frame.add(slider, BorderLayout.SOUTH);
072
073 frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
074 frame.pack();
075 frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
076 frame.setVisible(true);
077 addReporter(resultReporter);
078 }
079
080 return conf;
081 }
082
083 @Override
084 protected String getVersionInfo() {
085 return "$Id: Rosenbrock.java 3890 2011-04-20 14:58:14Z baumbart $";
086 }
087 }