source: branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/OLD_SingleObjectiveProgrammableProblem.cs @ 11739

Last change on this file since 11739 was 11739, checked in by mkommend, 8 years ago

#2174: Worked on operators and programmable problem base classes and scripts.

File size: 8.6 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2014 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 System.Collections.Generic;
24using System.Drawing;
25using System.Linq;
26using HeuristicLab.Analysis;
27using HeuristicLab.Common;
28using HeuristicLab.Core;
29using HeuristicLab.Encodings.RealVectorEncoding;
30using HeuristicLab.Optimization;
31using HeuristicLab.Parameters;
32using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
33
34namespace HeuristicLab.Problems.Programmable {
35  [Item("Programmable Problem (single-objective)", "Represents a single-objective problem that can be programmed.")]
36  [Creatable("Problems")]
37  [StorableClass]
38  public class OLD_SingleObjectiveProgrammableProblem : SingleObjectiveHeuristicOptimizationProblem<ISingleObjectiveEvaluator, ISolutionCreator>, IParameterizedNamedItem, IStorableContent {
39    public string Filename { get; set; }
40
41    public static new Image StaticItemImage {
42      get { return Common.Resources.VSImageLibrary.Script; }
43    }
44
45    public IValueParameter<ISingleObjectiveProblemDefinition> ProblemDefinitionParameter {
46      get { return (IValueParameter<ISingleObjectiveProblemDefinition>)Parameters["ProblemDefinition"]; }
47    }
48
49    protected IValueParameter<IEncoding> EncodingParameter {
50      get { return (IValueParameter<IEncoding>)Parameters["Encoding"]; }
51    }
52
53    public ISingleObjectiveProblemDefinition ProblemDefinition {
54      get { return ProblemDefinitionParameter.Value; }
55      set { ProblemDefinitionParameter.Value = value; }
56    }
57
58
59    public override IEnumerable<IParameterizedItem> ExecutionContextItems {
60      get { return base.ExecutionContextItems.Concat(new[] { ProblemDefinition.Encoding }); }
61    }
62    protected override IEnumerable<IItem> GetOperators() {
63      return base.GetOperators().Concat(ProblemDefinition.Encoding.Operators);
64    }
65
66    [StorableConstructor]
67    protected OLD_SingleObjectiveProgrammableProblem(bool deserializing) : base(deserializing) { }
68
69    protected OLD_SingleObjectiveProgrammableProblem(OLD_SingleObjectiveProgrammableProblem original, Cloner cloner)
70      : base(original, cloner) {
71      RegisterEventHandlers();
72    }
73    public OLD_SingleObjectiveProgrammableProblem()
74      : base(new SingleObjectiveEvaluator(), new MultiEncodingCreator()) {
75      Parameters.Add(new ValueParameter<ISingleObjectiveProblemDefinition>("ProblemDefinition", "Defines the problem.", new SingleObjectiveProblemDefinitionScript() { Name = Name }));
76      Parameters.Add(new ValueParameter<IEncoding>("Encoding", "Describes the configuration of the encoding, what the variables are called, what type they are and their bounds if any."));
77
78
79      Operators.Add(new BestScopeSolutionAnalyzer());
80      Operators.Add(new SingleObjectiveAnalyzer());
81      Operators.Add(Evaluator);
82
83      RegisterEventHandlers();
84    }
85
86    public override IDeepCloneable Clone(Cloner cloner) {
87      return new OLD_SingleObjectiveProgrammableProblem(this, cloner);
88    }
89
90    [StorableHook(HookType.AfterDeserialization)]
91    // ReSharper disable UnusedMember.Local
92    private void AfterDeserialization() {
93      RegisterEventHandlers();
94    }
95    // ReSharper restore UnusedMember.Local
96
97    private void RegisterEventHandlers() {
98      ProblemDefinitionParameter.ValueChanged += ProblemDefinitionParameterOnValueChanged;
99      RegisterProblemDefinitionEventHandlers();
100    }
101
102    private void ProblemDefinitionParameterOnValueChanged(object sender, EventArgs eventArgs) {
103      RegisterProblemDefinitionEventHandlers();
104      Parameterize();
105    }
106
107    private void RegisterProblemDefinitionEventHandlers() {
108      //ProblemDefinitionParameter.Value.ProblemDefinitionChanged += ProblemDefinitionChanged;
109      ProblemDefinitionParameter.Value.NameChanged += ProblemDefinitionNameChanged;
110    }
111
112    private void ProblemDefinitionNameChanged(object sender, EventArgs eventArgs) {
113      if (sender != ProblemDefinitionParameter.Value) return;
114      Name = ProblemDefinitionParameter.Value.Name;
115    }
116
117    protected override void OnNameChanged() {
118      base.OnNameChanged();
119      ProblemDefinitionParameter.Value.Name = Name;
120    }
121
122    protected override void OnEvaluatorChanged() {
123      base.OnEvaluatorChanged();
124      Parameterize();
125    }
126
127    protected virtual void ProblemDefinitionChanged(object sender, EventArgs eventArgs) {
128      Parameterize();
129    }
130
131    protected virtual void Parameterize() {
132      var definition = ProblemDefinitionParameter.Value;
133      if (definition == null) return;
134
135      IEncoding encoding = definition.Encoding;
136      SolutionCreator = encoding.SolutionCreator;
137
138      EncodingParameter.Value = encoding;
139      Maximization.Value = definition.Maximization;
140
141      //Evaluator.EncodingParameter.ActualName = EncodingParameter.Name;
142      //Evaluator.ProblemDefinitionParameter.ActualName = ProblemDefinitionParameter.Name;
143      //foreach (var evalOp in Operators.OfType<ISingleObjectiveProgrammableProblemEvaluator>()) {
144      //  evalOp.EncodingParameter.ActualName = EncodingParameter.Name;
145      //  evalOp.ProblemDefinitionParameter.ActualName = ProblemDefinitionParameter.Name;
146      //}
147      //foreach (var analyzeOp in Operators.OfType<ISingleObjectiveProgrammableProblemAnalyzer>()) {
148      //  analyzeOp.EncodingParameter.ActualName = EncodingParameter.Name;
149      //  analyzeOp.ProblemDefinitionParameter.ActualName = ProblemDefinitionParameter.Name;
150      //  analyzeOp.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName;
151      //}
152
153      ConfigureSingleEncodingOperators();
154      UpdateImprovementOperators();
155      UpdateMoveOperators();
156    }
157
158
159    protected virtual void ConfigureSingleEncodingOperators() {
160      foreach (var su in GetOperators().OfType<IRealVectorSwarmUpdater>()) {
161        su.MaximizationParameter.ActualName = MaximizationParameter.Name;
162      }
163    }
164
165    protected virtual void UpdateImprovementOperators() {
166      if (!Operators.Any(x => x is SingleObjectiveImprover))
167        Operators.Add(new SingleObjectiveImprover());
168      //foreach (var improver in Operators.OfType<SingleObjectiveImprover>()) {
169      //  improver.EncodingParameter.ActualName = EncodingParameter.Name;
170      //  improver.MaximizationParameter.ActualName = MaximizationParameter.Name;
171      //  improver.ProblemDefinitionParameter.ActualName = ProblemDefinitionParameter.Name;
172      //  improver.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName;
173      //}
174    }
175    protected virtual void UpdateMoveOperators() {
176      if (!Operators.Any(x => x is SingleObjectiveMoveGenerator))
177        Operators.Add(new SingleObjectiveMoveGenerator());
178      if (!Operators.Any(x => x is SingleObjectiveMoveEvaluator))
179        Operators.Add(new SingleObjectiveMoveEvaluator());
180      if (!Operators.Any(x => x is SingleObjectiveMoveMaker))
181        Operators.Add(new SingleObjectiveMoveMaker());
182
183      foreach (var generator in Operators.OfType<SingleObjectiveMoveGenerator>()) {
184        generator.EncodingParameter.ActualName = EncodingParameter.Name;
185        //generator.ProblemDefinitionParameter.ActualName = ProblemDefinitionParameter.Name;
186      }
187      foreach (var evaluator in Operators.OfType<SingleObjectiveMoveEvaluator>()) {
188        evaluator.EncodingParameter.ActualName = EncodingParameter.Name;
189        evaluator.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName;
190        //evaluator.ProblemDefinitionParameter.ActualName = ProblemDefinitionParameter.Name;
191      }
192      foreach (var maker in Operators.OfType<SingleObjectiveMoveMaker>()) {
193        maker.EncodingParameter.ActualName = EncodingParameter.Name;
194        maker.MoveQualityParameter.ActualName = Operators.OfType<SingleObjectiveMoveEvaluator>().First().MoveQualityParameter.ActualName;
195        maker.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName;
196      }
197    }
198  }
199}
Note: See TracBrowser for help on using the repository browser.