[15562] | 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 |
|
---|
| 22 | using System.Threading;
|
---|
| 23 | using HeuristicLab.Common;
|
---|
| 24 | using HeuristicLab.Core;
|
---|
| 25 | using HeuristicLab.Data;
|
---|
[15572] | 26 | using HeuristicLab.Optimization;
|
---|
[15562] | 27 | using HeuristicLab.Parameters;
|
---|
| 28 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 29 | using HeuristicLab.Random;
|
---|
| 30 |
|
---|
| 31 | namespace HeuristicLab.Problems.GeneralizedQuadraticAssignment.Algorithms {
|
---|
| 32 | [Item("Stochastic Algorithm", "Stochastic context-based algorithms to facilitate applying operators.")]
|
---|
| 33 | [StorableClass]
|
---|
[15572] | 34 | public abstract class StochasticAlgorithm<TContext, TEncoding> : ContextAlgorithm<TContext, TEncoding>
|
---|
| 35 | where TContext : class, IStochasticContext, new()
|
---|
| 36 | where TEncoding : class, IEncoding {
|
---|
[15562] | 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) { }
|
---|
[15572] | 60 | protected StochasticAlgorithm(StochasticAlgorithm<TContext, TEncoding> original, Cloner cloner)
|
---|
[15562] | 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 | }
|
---|