All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jamesframework.core.search.algo.SteepestDescent Maven / Gradle / Ivy

Go to download

The James core module is part of the James framework for optimization using local search metaheuristics in Java. The core contains general components to model problems, objectives and constraints, as well as generic algorithms to solve the problems. Moreover, the core provides implementations of specific utilities for subset selection.

There is a newer version: 1.2
Show newest version
//  Copyright 2014 Herman De Beukelaer
//
//  Licensed under the Apache License, Version 2.0 (the "License");
//  you may not use this file except in compliance with the License.
//  You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
//  Unless required by applicable law or agreed to in writing, software
//  distributed under the License is distributed on an "AS IS" BASIS,
//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//  See the License for the specific language governing permissions and
//  limitations under the License.

package org.jamesframework.core.search.algo;

import org.jamesframework.core.exceptions.JamesRuntimeException;
import org.jamesframework.core.problems.Problem;
import org.jamesframework.core.problems.solutions.Solution;
import org.jamesframework.core.search.SingleNeighbourhoodSearch;
import org.jamesframework.core.search.neigh.Move;
import org.jamesframework.core.search.neigh.Neighbourhood;

/**
 * Steepest descent algorithm, also referred to as hill climbing. In every search step, all neighbours of the current solution
 * are evaluated and the best one is adopted as the new current solution, given that it is an improvement over the current solution.
 * If the best neighbour is no improvement over the current solution, the search stops.
 * 
 * @param  solution type of the problems that may be solved using this search, required to extend {@link Solution}
 * @author Herman De Beukelaer
 */
public class SteepestDescent extends SingleNeighbourhoodSearch {

    /**
     * Creates a new steepest descent search, specifying the problem to solve and the neighbourhood used to
     * modify the current solution. Neither arguments can be null. The search name defaults
     * to "SteepestDescent".
     * 
     * @throws NullPointerException if problem or neighbourhood are null
     * @param problem problem to solve
     * @param neighbourhood neighbourhood used to create neighbouring solutions
     */
    public SteepestDescent(Problem problem, Neighbourhood neighbourhood){
        this(null, problem, neighbourhood);
    }
    
    /**
     * Creates a new steepest descent search, specifying the problem to solve, the neighbourhood used to
     * modify the current solution, and a custom search name. The problem and neighbourhood can not be
     * null. The search name can be null in which case the default name
     * "SteepestDescent" is assigned.
     * 
     * @throws NullPointerException if problem or neighbourhood are null
     * @param problem problem to solve
     * @param neighbourhood neighbourhood used to create neighbouring solutions
     * @param name custom search name
     */
    public SteepestDescent(String name, Problem problem, Neighbourhood neighbourhood){
        super(name != null ? name : "SteepestDescent", problem, neighbourhood);
    }

    /**
     * Investigates all neighbours of the current solution and adopts the best one as the new current solution,
     * if it is an improvement. If no improvement is found, the search is requested to stop and no further steps
     * will be performed.
     * 
     * @throws JamesRuntimeException if depending on malfunctioning components (problem, neighbourhood, ...)
     */
    @Override
    protected void searchStep() {
        // get best valid move with positive delta
        Move move = getMoveWithLargestDelta(
                                            getNeighbourhood().getAllMoves(getCurrentSolution()),   // generate all moves
                                            true);                                                  // only improvements
        // found improvement ?
        if(move != null){
            // accept move
            acceptMove(move);
        } else {
            // no improvement found
            stop();
        }
    }
    
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy