Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/SingleObjectiveProgrammableProblem.cs @ 11619

Last change on this file since 11619 was 11595, checked in by mkommend, 10 years ago

#2174: Added possibility to look up parameters of the encoding and removed them from the programmable problem.

File size: 8.8 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 SingleObjectiveProgrammableProblem : SingleObjectiveHeuristicOptimizationProblem<ISingleObjectiveProgrammableProblemEvaluator, 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 new ParameterCollection Parameters {
46      get { return base.Parameters; }
47    }
48    IKeyedItemCollection<string, IParameter> IParameterizedItem.Parameters {
49      get { return Parameters; }
50    }
51
52    public IValueParameter<ISingleObjectiveProblemDefinition> ProblemDefinitionParameter {
53      get { return (IValueParameter<ISingleObjectiveProblemDefinition>)Parameters["ProblemDefinition"]; }
54    }
55
56    protected IValueParameter<IEncoding> EncodingParameter {
57      get { return (IValueParameter<IEncoding>)Parameters["Encoding"]; }
58    }
59
60    public ISingleObjectiveProblemDefinition ProblemDefinition {
61      get { return ProblemDefinitionParameter.Value; }
62      set { ProblemDefinitionParameter.Value = value; }
63    }
64
65
66    public override IEnumerable<IParameterizedItem> ExecutionContextItems {
67      get { return base.ExecutionContextItems.Concat(new[] { ProblemDefinition.Encoding }); }
68    }
69    protected override IEnumerable<IItem> GetOperators() {
70      return base.GetOperators().Concat(ProblemDefinition.Encoding.Operators);
71    }
72
73    [StorableConstructor]
74    protected SingleObjectiveProgrammableProblem(bool deserializing) : base(deserializing) { }
75
76    protected SingleObjectiveProgrammableProblem(SingleObjectiveProgrammableProblem original, Cloner cloner)
77      : base(original, cloner) {
78      RegisterEventHandlers();
79    }
80    public SingleObjectiveProgrammableProblem()
81      : base(new SingleObjectiveEvaluator(), new MultiEncodingCreator()) {
82      Parameters.Add(new ValueParameter<ISingleObjectiveProblemDefinition>("ProblemDefinition", "Defines the problem.", new SingleObjectiveProblemScript() { Name = Name }));
83      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."));
84
85
86      Operators.Add(new BestScopeSolutionAnalyzer());
87      Operators.Add(new SingleObjectiveAnalyzer());
88      Operators.Add(Evaluator);
89
90      RegisterEventHandlers();
91    }
92
93    public override IDeepCloneable Clone(Cloner cloner) {
94      return new SingleObjectiveProgrammableProblem(this, cloner);
95    }
96
97    [StorableHook(HookType.AfterDeserialization)]
98    // ReSharper disable UnusedMember.Local
99    private void AfterDeserialization() {
100      RegisterEventHandlers();
101    }
102    // ReSharper restore UnusedMember.Local
103
104    private void RegisterEventHandlers() {
105      ProblemDefinitionParameter.ValueChanged += ProblemDefinitionParameterOnValueChanged;
106      RegisterProblemDefinitionEventHandlers();
107    }
108
109    private void ProblemDefinitionParameterOnValueChanged(object sender, EventArgs eventArgs) {
110      RegisterProblemDefinitionEventHandlers();
111      Parameterize();
112    }
113
114    private void RegisterProblemDefinitionEventHandlers() {
115      ProblemDefinitionParameter.Value.ProblemDefinitionChanged += ProblemDefinitionChanged;
116      ProblemDefinitionParameter.Value.NameChanged += ProblemDefinitionNameChanged;
117    }
118
119    private void ProblemDefinitionNameChanged(object sender, EventArgs eventArgs) {
120      if (sender != ProblemDefinitionParameter.Value) return;
121      Name = ProblemDefinitionParameter.Value.Name;
122    }
123
124    protected override void OnNameChanged() {
125      base.OnNameChanged();
126      ProblemDefinitionParameter.Value.Name = Name;
127    }
128
129    protected override void OnEvaluatorChanged() {
130      base.OnEvaluatorChanged();
131      Parameterize();
132    }
133
134    protected virtual void ProblemDefinitionChanged(object sender, EventArgs eventArgs) {
135      Parameterize();
136    }
137
138    protected virtual void Parameterize() {
139      var definition = ProblemDefinitionParameter.Value;
140      if (definition == null) return;
141
142      IEncoding encoding = definition.Encoding;
143      SolutionCreator = encoding.SolutionCreator;
144
145      EncodingParameter.Value = encoding;
146      Maximization.Value = definition.IsMaximizationProblem;
147
148      Evaluator.EncodingParameter.ActualName = EncodingParameter.Name;
149      Evaluator.ProblemDefinitionParameter.ActualName = ProblemDefinitionParameter.Name;
150      foreach (var evalOp in Operators.OfType<ISingleObjectiveProgrammableProblemEvaluator>()) {
151        evalOp.EncodingParameter.ActualName = EncodingParameter.Name;
152        evalOp.ProblemDefinitionParameter.ActualName = ProblemDefinitionParameter.Name;
153      }
154      foreach (var analyzeOp in Operators.OfType<ISingleObjectiveProgrammableProblemAnalyzer>()) {
155        analyzeOp.EncodingParameter.ActualName = EncodingParameter.Name;
156        analyzeOp.ProblemDefinitionParameter.ActualName = ProblemDefinitionParameter.Name;
157        analyzeOp.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName;
158      }
159
160      ConfigureSingleEncodingOperators();
161      UpdateImprovementOperators();
162      UpdateMoveOperators();
163    }
164
165
166    protected virtual void ConfigureSingleEncodingOperators() {
167      foreach (var su in GetOperators().OfType<IRealVectorSwarmUpdater>()) {
168        su.MaximizationParameter.ActualName = MaximizationParameter.Name;
169      }
170    }
171
172    protected virtual void UpdateImprovementOperators() {
173      if (!Operators.Any(x => x is SingleObjectiveImprover))
174        Operators.Add(new SingleObjectiveImprover());
175      foreach (var improver in Operators.OfType<SingleObjectiveImprover>()) {
176        improver.EncodingParameter.ActualName = EncodingParameter.Name;
177        improver.MaximizationParameter.ActualName = MaximizationParameter.Name;
178        improver.ProblemDefinitionParameter.ActualName = ProblemDefinitionParameter.Name;
179        improver.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName;
180      }
181    }
182    protected virtual void UpdateMoveOperators() {
183      if (!Operators.Any(x => x is SingleObjectiveMoveGenerator))
184        Operators.Add(new SingleObjectiveMoveGenerator());
185      if (!Operators.Any(x => x is SingleObjectiveMoveEvaluator))
186        Operators.Add(new SingleObjectiveMoveEvaluator());
187      if (!Operators.Any(x => x is SingleObjectiveMoveMaker))
188        Operators.Add(new SingleObjectiveMoveMaker());
189
190      foreach (var generator in Operators.OfType<SingleObjectiveMoveGenerator>()) {
191        generator.EncodingParameter.ActualName = EncodingParameter.Name;
192        generator.ProblemDefinitionParameter.ActualName = ProblemDefinitionParameter.Name;
193      }
194      foreach (var evaluator in Operators.OfType<SingleObjectiveMoveEvaluator>()) {
195        evaluator.EncodingParameter.ActualName = EncodingParameter.Name;
196        evaluator.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName;
197        evaluator.ProblemDefinitionParameter.ActualName = ProblemDefinitionParameter.Name;
198      }
199      foreach (var maker in Operators.OfType<SingleObjectiveMoveMaker>()) {
200        maker.EncodingParameter.ActualName = EncodingParameter.Name;
201        maker.MoveQualityParameter.ActualName = Operators.OfType<SingleObjectiveMoveEvaluator>().First().MoveQualityParameter.ActualName;
202        maker.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName;
203      }
204    }
205  }
206}
Note: See TracBrowser for help on using the repository browser.