Free cookie consent management tool by TermsFeed Policy Generator

source: branches/VNS/HeuristicLab.Algorithms.VariableNeighborhoodSearch/3.3/VariableNeighborhoodSearch.cs @ 5603

Last change on this file since 5603 was 5603, checked in by svonolfe, 13 years ago

Added first version of VNS main loop (WIP) #1425

File size: 5.9 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using HeuristicLab.Core;
6using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
7using HeuristicLab.Common;
8using HeuristicLab.Optimization;
9using HeuristicLab.Parameters;
10using HeuristicLab.Data;
11using HeuristicLab.Analysis;
12using HeuristicLab.Random;
13using HeuristicLab.Optimization.Operators;
14using HeuristicLab.Operators;
15
16namespace HeuristicLab.Algorithms.VariableNeighborhoodSearch {
17  [Item("Variable Neighborhood Search", "A variable neighborhood search algorithm.")]
18  [Creatable("Algorithms")]
19  [StorableClass]
20  public sealed class VariableNeighborhoodSearch : EngineAlgorithm, IStorableContent {
21    public string Filename { get; set; }
22
23    #region Problem Properties
24    public override Type ProblemType {
25      get { return typeof(ISingleObjectiveProblem); }
26    }
27    public new ISingleObjectiveProblem Problem {
28      get { return (ISingleObjectiveProblem)base.Problem; }
29      set { base.Problem = value; }
30    }
31    #endregion
32
33    #region Parameter Properties
34    private ValueParameter<IntValue> SeedParameter {
35      get { return (ValueParameter<IntValue>)Parameters["Seed"]; }
36    }
37    private ValueParameter<BoolValue> SetSeedRandomlyParameter {
38      get { return (ValueParameter<BoolValue>)Parameters["SetSeedRandomly"]; }
39    }
40    public /*Constrained*/ValueParameter<ISingleObjectiveEvaluator> EvaluatorParameter {
41      get { return (/*Constrained*/ValueParameter<ISingleObjectiveEvaluator>)Parameters["Evaluator"]; }
42    }
43    private ValueParameter<ILocalImprovement> LocalImprovementParameter {
44      get { return (ValueParameter<ILocalImprovement>)Parameters["LocalImprovement"]; }
45    }
46    private ValueParameter<ShakingOperator> ShakingParameter {
47      get { return (ValueParameter<ShakingOperator>)Parameters["Shaking"]; }
48    }
49    private ValueParameter<IntValue> MaximumIterationsParameter {
50      get { return (ValueParameter<IntValue>)Parameters["MaximumIterations"]; }
51    }
52    private ValueParameter<MultiAnalyzer> AnalyzerParameter {
53      get { return (ValueParameter<MultiAnalyzer>)Parameters["Analyzer"]; }
54    }
55    #endregion
56
57    #region Properties
58    private RandomCreator RandomCreator {
59      get { return (RandomCreator)OperatorGraph.InitialOperator; }
60    }
61    #endregion
62
63    [StorableConstructor]
64    private VariableNeighborhoodSearch(bool deserializing) : base(deserializing) { }
65    [StorableHook(HookType.AfterDeserialization)]
66    private void AfterDeserialization() {
67
68    }
69    private VariableNeighborhoodSearch(VariableNeighborhoodSearch original, Cloner cloner)
70      : base(original, cloner) {
71
72    }
73    public override IDeepCloneable Clone(Cloner cloner) {
74      return new VariableNeighborhoodSearch(this, cloner);
75    }
76    public VariableNeighborhoodSearch()
77      : base() {
78      Parameters.Add(new ValueParameter<IntValue>("Seed", "The random seed used to initialize the new pseudo random number generator.", new IntValue(0)));
79      Parameters.Add(new ValueParameter<BoolValue>("SetSeedRandomly", "True if the random seed should be set to a random value, otherwise false.", new BoolValue(true)));
80      Parameters.Add(new /*Constrained*/ValueParameter<ISingleObjectiveEvaluator>("Evaluator", "The operator used to evaluate the solutions."));
81      Parameters.Add(new ValueParameter<ILocalImprovement>("LocalImprovement", "The local improvement operation", new LocalSearchImprovement()));
82      Parameters.Add(new ValueParameter<ShakingOperator>("Shaking", "The shaking operation", new ShakingOperator()));
83      Parameters.Add(new ValueParameter<IntValue>("MaximumIterations", "The maximum number of generations which should be processed.", new IntValue(1000)));
84      Parameters.Add(new ValueParameter<MultiAnalyzer>("Analyzer", "The operator used to analyze the solution and moves.", new MultiAnalyzer()));
85
86      RandomCreator randomCreator = new RandomCreator();
87      SolutionsCreator solutionsCreator = new SolutionsCreator();
88      VariableCreator variableCreator = new VariableCreator();
89      ResultsCollector resultsCollector = new ResultsCollector();
90      VariableNeighborhoodSearchMainLoop mainLoop = new VariableNeighborhoodSearchMainLoop();
91      OperatorGraph.InitialOperator = randomCreator;
92
93      randomCreator.RandomParameter.ActualName = "Random";
94      randomCreator.SeedParameter.ActualName = SeedParameter.Name;
95      randomCreator.SeedParameter.Value = null;
96      randomCreator.SetSeedRandomlyParameter.ActualName = SetSeedRandomlyParameter.Name;
97      randomCreator.SetSeedRandomlyParameter.Value = null;
98      randomCreator.Successor = solutionsCreator;
99
100      solutionsCreator.NumberOfSolutions = new IntValue(1);
101      solutionsCreator.Successor = variableCreator;
102
103      variableCreator.Name = "Initialize Evaluated Solutions";
104      variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("EvaluatedSolutions", new IntValue()));
105      variableCreator.Successor = resultsCollector;
106
107      resultsCollector.CollectedValues.Add(new LookupParameter<IntValue>("Evaluated Solutions", null, "EvaluatedSolutions"));
108      resultsCollector.ResultsParameter.ActualName = "Results";
109      resultsCollector.Successor = mainLoop;
110
111      mainLoop.LocalImprovementParameter.ActualName = LocalImprovementParameter.Name;
112      mainLoop.ShakingParameter.ActualName = ShakingParameter.Name;
113      mainLoop.EvaluatorParameter.ActualName = EvaluatorParameter.Name;
114      mainLoop.MaximumIterationsParameter.ActualName = MaximumIterationsParameter.Name;
115      mainLoop.RandomParameter.ActualName = RandomCreator.RandomParameter.ActualName;
116      mainLoop.ResultsParameter.ActualName = "Results";
117      mainLoop.AnalyzerParameter.ActualName = AnalyzerParameter.Name;
118      mainLoop.EvaluatedSolutionsParameter.ActualName = "EvaluatedSolutions";
119
120      /*Initialize();*/
121    }
122  }
123}
Note: See TracBrowser for help on using the repository browser.