Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.SA/TemperatureBasedFitnessComparer.cs @ 1289

Last change on this file since 1289 was 1289, checked in by abeham, 15 years ago

Added simulated annealing (SA) user interface (#18)

File size: 3.7 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2009 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using HeuristicLab.Core;
24using HeuristicLab.Data;
25
26namespace HeuristicLab.SA {
27  public class TemperatureBasedFitnessComparer : OperatorBase {
28
29    public override string Description {
30      get { return @"Compares the quality of two parents considering a certain dampening factor (temperature).
31A mutant is successful if it is either better than its parent or with a probability e^(-FitnessDifference / Temperature)."; }
32    }
33
34    public TemperatureBasedFitnessComparer()
35      : base() {
36      AddVariableInfo(new VariableInfo("Random", "The PRNG to use (Uniform)", typeof(IRandom), VariableKind.In));
37      AddVariableInfo(new VariableInfo("Maximization", "Whether the problem is a maximization or minimization problem", typeof(BoolData), VariableKind.In));
38      AddVariableInfo(new VariableInfo("Quality", "The variable that holds the fitness value", typeof(DoubleData), VariableKind.In));
39      AddVariableInfo(new VariableInfo("Temperature", "The current temperature", typeof(DoubleData), VariableKind.In));
40      AddVariableInfo(new VariableInfo("SuccessfulChild", "Boolean variable that tells if a child is successful (true) or not (false)", typeof(BoolData), VariableKind.New | VariableKind.Out));
41    }
42
43    public override IOperation Apply(IScope scope) {
44      double mutantQuality = GetVariableValue<DoubleData>("Quality", scope, false).Data;
45      double parentQuality = scope.SubScopes[0].GetVariableValue<DoubleData>("Quality", false).Data;
46      bool maximization = GetVariableValue<BoolData>("Maximization", scope, true).Data;
47
48      // if mutant is better, accept it
49      if (maximization && mutantQuality > parentQuality || !maximization && mutantQuality < parentQuality) {
50        BoolData sc = GetVariableValue<BoolData>("SuccessfulChild", scope, false, false);
51        if (sc == null) {
52          if (GetVariableInfo("SuccessfulChild").Local) {
53            AddVariable(new Variable("SuccessfulChild", new BoolData(true)));
54          } else {
55            scope.AddVariable(new Variable("SuccessfulChild", new BoolData(true)));
56          }
57        } else sc.Data = true;
58        return null;
59      }
60
61      IRandom random = scope.GetVariableValue<IRandom>("Random", true);
62      double temperature = scope.GetVariableValue<DoubleData>("Temperature", true).Data;
63
64      double probability = Math.Exp(-Math.Abs(mutantQuality - parentQuality) / temperature);
65      bool success = random.NextDouble() < probability;
66      BoolData sc2 = GetVariableValue<BoolData>("SuccessfulChild", scope, false, false);
67      if (sc2 == null) {
68        if (GetVariableInfo("SuccessfulChild").Local) {
69          AddVariable(new Variable("SuccessfulChild", new BoolData(true)));
70        } else {
71          scope.AddVariable(new Variable("SuccessfulChild", new BoolData(true)));
72        }
73      } else sc2.Data = success;
74      return null;
75    }
76  }
77}
Note: See TracBrowser for help on using the repository browser.