
net.sf.tweety.math.opt.solver.SimulatedAnnealingOnConstrProb Maven / Gradle / Ivy
/*
* This file is part of "TweetyProject", a collection of Java libraries for
* logical aspects of artificial intelligence and knowledge representation.
*
* TweetyProject is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License version 3 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see .
*
* Copyright 2020 The TweetyProject Team
*/
package net.sf.tweety.math.opt.solver;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import net.sf.tweety.math.opt.problem.*;
import net.sf.tweety.math.term.FloatConstant;
import net.sf.tweety.math.term.Variable;
import net.sf.tweety.math.term.Term;
/**
* implements the Simulated Annealing algorithm
* for optimization problems
* @author Sebastian Franke
*
*/
public class SimulatedAnnealingOnConstrProb extends Solver{
/**the exact problem that is to be solved*/
private ConstraintSatisfactionProblem prob;
/** starting temperature to be lowered*/
private double startTemp;
/**the factor to linearely reduce the temperature by every iteration*/
private double decreasePerIt;
private int maxStepsWithNoImprove;
/** For randomization */
private Random rand = new Random();
/** The magnitude of changing the value of a variable in the mutation step. */
private static final double VAR_MUTATE_STRENGTH = 0.5;
public SimulatedAnnealingOnConstrProb(double startTemp, double decreasePerIt, int maxStepsWithNoImprove) {
this.decreasePerIt = decreasePerIt;
this.startTemp = startTemp;
this.maxStepsWithNoImprove = maxStepsWithNoImprove;
}
/**
* creates one new solution that changes every variable of the inital solution a bit in a positive or negative direction
* @param ind: the initial solution used as a strating point
* @return the changed solution
*/
private Map createNewSol(Map ind){
Map mutant = new HashMap();
//iterate through all variables
for(Variable v: ind.keySet()){
// decide if there is a positive or negative mutation
if(rand.nextBoolean()){
double val = ind.get(v).doubleValue();
val = val + rand.nextDouble() * VAR_MUTATE_STRENGTH * (v.getUpperBound() - val);
mutant.put(v, new FloatConstant(val));
}else{
double val = ind.get(v).doubleValue();
val = val - rand.nextDouble() * VAR_MUTATE_STRENGTH * (val- v.getLowerBound());
mutant.put(v, new FloatConstant(val));
}
}
return mutant;
}
/**
* @param minIterations: the minimum amount of solutions to be created
* @param maxIterations: the maximum amount of solutions to be created
* @param threshold: if a solution with the quality of threshold is reached we do maximum 10 more tries
* @param currSol: the solution that every newly created solution uses as a initial solution in createNewSol
* @return the best solution that was found and is a mutant of currSol
*/
public Map chooseANeighbor(Map currSol, int minIterations, int maxIterations, double threshold)
{
int cnt = 0;
int thresholdCnt = 0;
boolean thresholdSwitch = false;
ArrayList
© 2015 - 2025 Weber Informatics LLC | Privacy Policy