001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.math4.optim.nonlinear.scalar;
018
019import org.apache.commons.rng.UniformRandomProvider;
020import org.apache.commons.math4.optim.OptimizationData;
021
022/**
023 * Simulated annealing set-up
024 *
025 * @since 4.0
026 */
027public class SimulatedAnnealing implements OptimizationData {
028    /** RNG. */
029    private final UniformRandomProvider rng;
030    /** Number of iterations of the annealing process. */
031    private final int iterations;
032
033    /**
034     * @param random Random number generator.
035     * @param numberOfIterations Number of iterations of the annealing
036     * process.
037     * When the current iteration exceeds the given value, the state with
038     * higher energy will always be rejected.
039     */
040    public SimulatedAnnealing(UniformRandomProvider random,
041                              int numberOfIterations) {
042        rng = random;
043        iterations = numberOfIterations;
044    }
045
046    /**
047     *
048     * @param current Value of the objective function of the default state.
049     * @param alternative Value of the objective function of the alternative
050     * state.
051     * @param isMinimization Indicates whether small values (of the objective
052     * function) are better.
053     * @param iteration Current iteration.
054     * @return {@code true} if the state with the {@code alternative} value
055     * should be accepted.
056     */
057    public boolean accept(double current,
058                          double alternative,
059                          boolean isMinimization,
060                          int iteration) {
061        final double diff = isMinimization ?
062            current - alternative :
063            alternative - current;
064
065        if (diff >= 0) {
066            // Always accept an equivalent or better alternative state.
067            return true;
068        }
069        if (iteration > iterations) {
070            // No annealing (temperature is below 0).
071            return false;
072        }
073
074        final int temperature = iterations - iteration;
075        final double a = Math.exp(diff / temperature);
076        final double b = rng.nextDouble();
077        System.out.println("t=" + temperature + " a=" + a + " b=" + b); // XXX
078        return a > b;
079    }
080
081    /**
082     * @return the number of iterations below which an alternative state with
083     * higher energy might be {@link #accept(double,double,boolean,int) accepted}.
084     */
085    public int getIterations() {
086        return iterations;
087    }
088}