source: branches/GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment.Algorithms/3.3/Infrastructure/Algorithms/StochasticAlgorithm.cs @ 15572

Last change on this file since 15572 was 15572, checked in by abeham, 5 years ago

#1614:

  • fixed a bug in GRASP where solutions in the elite set would be mutated
  • introduced termination criteria when reaching best-known quality
  • tweaked generating random numbers in StochasticNMoveSingleMoveGenerator
  • changed DiscreteLocationCrossover to use an allele from one of the parents instead of introducing a mutation in case no feasible insert location is found
  • changed OSGA maxselpress to 500
  • slight change to contexts, introduced single-objectiveness much earlier in the class hierachy
    • limited ContextAlgorithm to SingleObjectiveBasicProblems (doesn't matter here)
File size: 3.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2017 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.Threading;
23using HeuristicLab.Common;
24using HeuristicLab.Core;
25using HeuristicLab.Data;
26using HeuristicLab.Optimization;
27using HeuristicLab.Parameters;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29using HeuristicLab.Random;
30
31namespace HeuristicLab.Problems.GeneralizedQuadraticAssignment.Algorithms {
32  [Item("Stochastic Algorithm", "Stochastic context-based algorithms to facilitate applying operators.")]
33  [StorableClass]
34  public abstract class StochasticAlgorithm<TContext, TEncoding> : ContextAlgorithm<TContext, TEncoding>
35    where TContext : class, IStochasticContext, new()
36    where TEncoding : class, IEncoding {
37   
38    [Storable]
39    private FixedValueParameter<BoolValue> setSeedRandomlyParameter;
40    private IFixedValueParameter<BoolValue> SetSeedRandomlyParameter {
41      get { return setSeedRandomlyParameter; }
42    }
43    [Storable]
44    private FixedValueParameter<IntValue> seedParameter;
45    private IFixedValueParameter<IntValue> SeedParameter {
46      get { return seedParameter; }
47    }
48   
49    public bool SetSeedRandomly {
50      get { return setSeedRandomlyParameter.Value.Value; }
51      set { setSeedRandomlyParameter.Value.Value = value; }
52    }
53    public int Seed {
54      get { return seedParameter.Value.Value; }
55      set { seedParameter.Value.Value = value; }
56    }
57
58    [StorableConstructor]
59    protected StochasticAlgorithm(bool deserializing) : base(deserializing) { }
60    protected StochasticAlgorithm(StochasticAlgorithm<TContext, TEncoding> original, Cloner cloner)
61      : base(original, cloner) {
62      setSeedRandomlyParameter = cloner.Clone(original.setSeedRandomlyParameter);
63      seedParameter = cloner.Clone(original.seedParameter);
64    }
65    protected StochasticAlgorithm()
66      : base() {
67      Parameters.Add(setSeedRandomlyParameter = new FixedValueParameter<BoolValue>("SetSeedRandomly", "Whether to overwrite the seed with a random value each time the algorithm is run.", new BoolValue(true)));
68      Parameters.Add(seedParameter = new FixedValueParameter<IntValue>("Seed", "The random seed that is used in the stochastic algorithm", new IntValue(0)));
69    }
70
71    protected override void Initialize(CancellationToken cancellationToken) {
72      base.Initialize(cancellationToken);
73
74      if (SetSeedRandomly) {
75        var rnd = new System.Random();
76        Seed = rnd.Next();
77      }
78
79      Context.Random = new MersenneTwister((uint)Seed);
80    }
81  }
82}
Note: See TracBrowser for help on using the repository browser.