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

Last change on this file since 13345 was 13345, checked in by mkommend, 5 years ago

#2521: refactored programmable problem

File size: 6.3 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      codeChanged = original.codeChanged;
85    }
86    protected ProblemDefinitionScript()
87      : base() {
88    }
89    protected ProblemDefinitionScript(string code)
90      : base(code) {
91    }
92
93    private readonly object compileLock = new object();
94    private volatile CompiledProblemDefinition<TEncoding, TSolution> compiledProblemDefinition;
95    protected CompiledProblemDefinition<TEncoding, TSolution> CompiledProblemDefinition {
96      get {
97        // double checked locking pattern
98        if (compiledProblemDefinition == null) {
99          lock (compileLock) {
100            if (compiledProblemDefinition == null) {
101              if (codeChanged) throw new ProblemDefinitionScriptException("The code has been changed, but was not recompiled.");
102              Compile(false);
103            }
104          }
105        }
106        return compiledProblemDefinition;
107      }
108    }
109
110    public sealed override Assembly Compile() {
111      return Compile(true);
112    }
113
114    private Assembly Compile(bool fireChanged) {
115      var assembly = base.Compile();
116      var types = assembly.GetTypes();
117      if (!types.Any(x => typeof(CompiledProblemDefinition<TEncoding, TSolution>).IsAssignableFrom(x)))
118        throw new ProblemDefinitionScriptException("The compiled code doesn't contain a problem definition." + Environment.NewLine + "The problem definition must be a subclass of CompiledProblemDefinition.");
119      if (types.Count(x => typeof(CompiledProblemDefinition<TEncoding, TSolution>).IsAssignableFrom(x)) > 1)
120        throw new ProblemDefinitionScriptException("The compiled code contains multiple problem definitions." + Environment.NewLine + "Only one subclass of CompiledProblemDefinition is allowed.");
121
122      CompiledProblemDefinition<TEncoding, TSolution> inst;
123      try {
124        inst = (CompiledProblemDefinition<TEncoding, TSolution>)Activator.CreateInstance(types.Single(x => typeof(CompiledProblemDefinition<TEncoding, TSolution>).IsAssignableFrom(x)));
125      }
126      catch (Exception e) {
127        compiledProblemDefinition = null;
128        throw new ProblemDefinitionScriptException("Instantiating the problem definition failed." + Environment.NewLine + "Check your default constructor.", e);
129      }
130
131      try {
132        inst.vars = new Variables(VariableStore);
133        inst.Encoding = encoding;
134      }
135      catch (Exception e) {
136        compiledProblemDefinition = null;
137        throw new ProblemDefinitionScriptException("Initializing the problem definition failed." + Environment.NewLine + "Check your Initialize() method.", e);
138      }
139
140      try {
141        compiledProblemDefinition = inst;
142        if (fireChanged) OnProblemDefinitionChanged();
143      }
144      catch (Exception e) {
145        compiledProblemDefinition = null;
146        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);
147      }
148
149      codeChanged = false;
150      return assembly;
151    }
152
153    protected override void OnCodeChanged() {
154      base.OnCodeChanged();
155      compiledProblemDefinition = null;
156      codeChanged = true;
157    }
158
159    public event EventHandler ProblemDefinitionChanged;
160    protected virtual void OnProblemDefinitionChanged() {
161      var handler = ProblemDefinitionChanged;
162      if (handler != null) handler(this, EventArgs.Empty);
163    }
164  }
165}
Note: See TracBrowser for help on using the repository browser.