Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/MultiObjectiveProgrammableProblem.cs @ 11711

Last change on this file since 11711 was 11587, checked in by mkommend, 10 years ago

#2174: Implemented multi-encoding operators and adapated wiring of operators in the programmable problems.

File size: 6.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.Common;
27using HeuristicLab.Core;
28using HeuristicLab.Data;
29using HeuristicLab.Optimization;
30using HeuristicLab.Parameters;
31using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
32
33namespace HeuristicLab.Problems.Programmable {
34  [Item("Programmable Problem (multi-objective)", "Represents a multi-objective problem that can be programmed.")]
35  [Creatable("Problems")]
36  [StorableClass]
37  public class MultiObjectiveProgrammableProblem : MultiObjectiveHeuristicOptimizationProblem<IMultiObjectiveProgrammableProblemEvaluator, ISolutionCreator>, IParameterizedNamedItem, IStorableContent {
38    public string Filename { get; set; }
39
40    public static new Image StaticItemImage {
41      get { return Common.Resources.VSImageLibrary.Script; }
42    }
43
44    public new ParameterCollection Parameters {
45      get { return base.Parameters; }
46    }
47    IKeyedItemCollection<string, IParameter> IParameterizedItem.Parameters {
48      get { return Parameters; }
49    }
50
51    public IValueParameter<IMultiObjectiveProblemDefinition> ProblemDefinitionParameter {
52      get { return (IValueParameter<IMultiObjectiveProblemDefinition>)Parameters["ProblemDefinition"]; }
53    }
54
55    protected IValueParameter<IEncoding> EncodingParameter {
56      get { return (IValueParameter<IEncoding>)Parameters["Encoding"]; }
57    }
58
59    public IMultiObjectiveProblemDefinition ProblemDefinition {
60      get { return ProblemDefinitionParameter.Value; }
61      set { ProblemDefinitionParameter.Value = value; }
62    }
63
64    [Storable]
65    protected List<IParameter> DynamicEncodingParameters;
66
67    [StorableConstructor]
68    protected MultiObjectiveProgrammableProblem(bool deserializing) : base(deserializing) { }
69    protected MultiObjectiveProgrammableProblem(MultiObjectiveProgrammableProblem original, Cloner cloner)
70      : base(original, cloner) {
71      DynamicEncodingParameters = original.DynamicEncodingParameters.Select(cloner.Clone).ToList();
72      RegisterEventHandlers();
73    }
74    public MultiObjectiveProgrammableProblem()
75      : base(new MultiObjectiveEvaluator(), new MultiEncodingCreator()) {
76      Parameters.Add(new ValueParameter<IMultiObjectiveProblemDefinition>("ProblemDefinition", "Defines the problem.", new MultiObjectiveProblemScript() { Name = Name }));
77      Parameters.Add(new ValueParameter<IEncoding>("Encoding", "Describes which parameters exist, what they're called, what type they are and their bounds if any."));
78
79      DynamicEncodingParameters = new List<IParameter>();
80
81      Operators.Add(new MultiObjectiveAnalyzer());
82      Operators.Add(Evaluator);
83
84      RegisterEventHandlers();
85    }
86
87    public override IDeepCloneable Clone(Cloner cloner) {
88      return new MultiObjectiveProgrammableProblem(this, cloner);
89    }
90
91    [StorableHook(HookType.AfterDeserialization)]
92    // ReSharper disable UnusedMember.Local
93    private void AfterDeserialization() {
94      RegisterEventHandlers();
95    }
96    // ReSharper restore UnusedMember.Local
97
98    private void RegisterEventHandlers() {
99      ProblemDefinitionParameter.ValueChanged += ProblemDefinitionParameterOnValueChanged;
100      RegisterHostInstanceChanges();
101    }
102
103    private void ProblemDefinitionParameterOnValueChanged(object sender, EventArgs eventArgs) {
104      RegisterHostInstanceChanges();
105      Parameterize();
106    }
107
108    private void RegisterHostInstanceChanges() {
109      ProblemDefinitionParameter.Value.ProblemDefinitionChanged += ProblemDefinitionChanged;
110      ProblemDefinitionParameter.Value.NameChanged += ProblemDefinitionHostOnNameChanged;
111    }
112
113    private void ProblemDefinitionHostOnNameChanged(object sender, EventArgs eventArgs) {
114      if (sender != ProblemDefinitionParameter.Value) return;
115      Name = ProblemDefinitionParameter.Value.Name;
116    }
117
118    protected override void OnNameChanged() {
119      base.OnNameChanged();
120      ProblemDefinitionParameter.Value.Name = Name;
121    }
122
123    protected override void OnEvaluatorChanged() {
124      base.OnEvaluatorChanged();
125      Parameterize();
126    }
127
128    protected virtual void ProblemDefinitionChanged(object sender, EventArgs eventArgs) {
129      Parameterize();
130    }
131
132    protected virtual void Parameterize() {
133      var definition = ProblemDefinitionParameter.Value;
134      if (definition == null) return;
135
136      IEncoding encoding = definition.Encoding;
137      EncodingParameter.Value = encoding;
138      Maximization = new BoolArray(definition.Maximization);
139
140      Evaluator.EncodingParameter.ActualName = EncodingParameter.Name;
141      Evaluator.ProblemDefinitionParameter.ActualName = ProblemDefinitionParameter.Name;
142      foreach (var evalOp in Operators.OfType<IMultiObjectiveProgrammableProblemEvaluator>()) {
143        evalOp.EncodingParameter.ActualName = EncodingParameter.Name;
144        evalOp.ProblemDefinitionParameter.ActualName = ProblemDefinitionParameter.Name;
145      }
146      foreach (var analyzeOp in Operators.OfType<IMultiObjectiveProgrammableProblemAnalyzer>()) {
147        analyzeOp.EncodingParameter.ActualName = EncodingParameter.Name;
148        analyzeOp.ProblemDefinitionParameter.ActualName = ProblemDefinitionParameter.Name;
149        analyzeOp.QualitiesParameter.ActualName = Evaluator.QualitiesParameter.ActualName;
150      }
151
152      SolutionCreator = encoding.SolutionCreator;
153
154      foreach (var param in DynamicEncodingParameters)
155        if (Parameters.Contains(param)) Parameters.Remove(param);
156      DynamicEncodingParameters.Clear();
157
158      DynamicEncodingParameters.AddRange(encoding.Parameters);
159
160
161      foreach (var param in DynamicEncodingParameters) {
162        param.Hidden = true;
163        Parameters.Add(param);
164      }
165
166    }
167  }
168}
Note: See TracBrowser for help on using the repository browser.