source: branches/ProblemRefactoring/HeuristicLab.Problems.Programmable/3.3/ProblemDefinitionScript.cs @ 13348

Last change on this file since 13348 was 13348, checked in by mkommend, 4 years ago

#2521: Refactored single-objective programmable problem.

File size: 6.4 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 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.Linq;
24using System.Reflection;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Optimization;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29using HeuristicLab.Scripting;
30
31namespace HeuristicLab.Problems.Programmable {
32  [StorableClass]
33  public abstract class ProblemDefinitionScript : Script {
34    [Storable]
35    private VariableStore variableStore;
36    public VariableStore VariableStore {
37      get { return variableStore; }
38    }
39
40    [StorableConstructor]
41    protected ProblemDefinitionScript(bool deserializing) : base(deserializing) { }
42    protected ProblemDefinitionScript(ProblemDefinitionScript original, Cloner cloner)
43      : base(original, cloner) {
44      variableStore = cloner.Clone(original.variableStore);
45    }
46    protected ProblemDefinitionScript()
47      : base() {
48      variableStore = new VariableStore();
49    }
50    protected ProblemDefinitionScript(string code)
51      : base(code) {
52      variableStore = new VariableStore();
53    }
54  }
55
56  [Item("ProblemDefinitionScript", "Script that defines the parameter vector and evaluates the solution for a programmable problem.")]
57  [StorableClass]
58  public abstract class ProblemDefinitionScript<TEncoding, TSolution> : ProblemDefinitionScript, IProblemDefinition<TEncoding, TSolution>
59    where TEncoding : class, IEncoding<TSolution>
60    where TSolution : class, ISolution {
61
62    [Storable]
63    private bool codeChanged;
64
65    [Storable]
66    private TEncoding encoding;
67    internal TEncoding Encoding {
68      get { return encoding; }
69      set { encoding = value; }
70    }
71
72    TEncoding IProblemDefinition<TEncoding, TSolution>.Encoding {
73      get { return Encoding; }
74    }
75
76    internal void Initialize() {
77      CompiledProblemDefinition.Initialize();
78    }
79
80    [StorableConstructor]
81    protected ProblemDefinitionScript(bool deserializing) : base(deserializing) { }
82    protected ProblemDefinitionScript(ProblemDefinitionScript<TEncoding, TSolution> original, Cloner cloner)
83      : base(original, cloner) {
84      encoding = cloner.Clone(original.encoding);
85      codeChanged = original.codeChanged;
86    }
87    protected ProblemDefinitionScript()
88      : base() {
89    }
90    protected ProblemDefinitionScript(string code)
91      : base(code) {
92    }
93
94    private readonly object compileLock = new object();
95    private volatile CompiledProblemDefinition<TEncoding, TSolution> compiledProblemDefinition;
96    protected CompiledProblemDefinition<TEncoding, TSolution> CompiledProblemDefinition {
97      get {
98        // double checked locking pattern
99        if (compiledProblemDefinition == null) {
100          lock (compileLock) {
101            if (compiledProblemDefinition == null) {
102              if (codeChanged) throw new ProblemDefinitionScriptException("The code has been changed, but was not recompiled.");
103              Compile(false);
104            }
105          }
106        }
107        return compiledProblemDefinition;
108      }
109    }
110
111    public sealed override Assembly Compile() {
112      return Compile(true);
113    }
114
115    private Assembly Compile(bool fireChanged) {
116      var assembly = base.Compile();
117      var types = assembly.GetTypes();
118      if (!types.Any(x => typeof(CompiledProblemDefinition<TEncoding, TSolution>).IsAssignableFrom(x)))
119        throw new ProblemDefinitionScriptException("The compiled code doesn't contain a problem definition." + Environment.NewLine + "The problem definition must be a subclass of CompiledProblemDefinition.");
120      if (types.Count(x => typeof(CompiledProblemDefinition<TEncoding, TSolution>).IsAssignableFrom(x)) > 1)
121        throw new ProblemDefinitionScriptException("The compiled code contains multiple problem definitions." + Environment.NewLine + "Only one subclass of CompiledProblemDefinition is allowed.");
122
123      CompiledProblemDefinition<TEncoding, TSolution> inst;
124      try {
125        inst = (CompiledProblemDefinition<TEncoding, TSolution>)Activator.CreateInstance(types.Single(x => typeof(CompiledProblemDefinition<TEncoding, TSolution>).IsAssignableFrom(x)));
126      }
127      catch (Exception e) {
128        compiledProblemDefinition = null;
129        throw new ProblemDefinitionScriptException("Instantiating the problem definition failed." + Environment.NewLine + "Check your default constructor.", e);
130      }
131
132      try {
133        inst.vars = new Variables(VariableStore);
134        inst.Encoding = encoding;
135      }
136      catch (Exception e) {
137        compiledProblemDefinition = null;
138        throw new ProblemDefinitionScriptException("Initializing the problem definition failed." + Environment.NewLine + "Check your Initialize() method.", e);
139      }
140
141      try {
142        compiledProblemDefinition = inst;
143        if (fireChanged) OnProblemDefinitionChanged();
144      }
145      catch (Exception e) {
146        compiledProblemDefinition = null;
147        throw new ProblemDefinitionScriptException("Using the problem definition in the problem failed." + Environment.NewLine + "Examine this error message carefully (often there is an issue with the defined encoding).", e);
148      }
149
150      codeChanged = false;
151      return assembly;
152    }
153
154    protected override void OnCodeChanged() {
155      base.OnCodeChanged();
156      compiledProblemDefinition = null;
157      codeChanged = true;
158    }
159
160    public event EventHandler ProblemDefinitionChanged;
161    protected virtual void OnProblemDefinitionChanged() {
162      var handler = ProblemDefinitionChanged;
163      if (handler != null) handler(this, EventArgs.Empty);
164    }
165  }
166}
Note: See TracBrowser for help on using the repository browser.