Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2521_ProblemRefactoring/HeuristicLab.Problems.Programmable/3.3/ProblemDefinitionScript.cs @ 17226

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

#2521: Merged trunk changes into problem refactoring branch.

File size: 6.6 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 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 HEAL.Attic;
26using HeuristicLab.Common;
27using HeuristicLab.Core;
28using HeuristicLab.Optimization;
29using HeuristicLab.Scripting;
30
31namespace HeuristicLab.Problems.Programmable {
32  [StorableType("5573B778-C60C-44BF-98FB-A8E189818C00")]
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(StorableConstructorFlag _) : base(_) { }
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  [StorableType("0B3AF22C-4744-4860-BBCF-A92046000847")]
58  public abstract class ProblemDefinitionScript<TEncoding, TEncodedSolution> : ProblemDefinitionScript, IProblemDefinition<TEncoding, TEncodedSolution>
59    where TEncoding : class, IEncoding<TEncodedSolution>
60    where TEncodedSolution : class, IEncodedSolution {
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, TEncodedSolution>.Encoding {
73      get { return Encoding; }
74    }
75
76    internal void Initialize() {
77      CompiledProblemDefinition.Initialize();
78    }
79
80    [StorableConstructor]
81    protected ProblemDefinitionScript(StorableConstructorFlag _) : base(_) { }
82    protected ProblemDefinitionScript(ProblemDefinitionScript<TEncoding, TEncodedSolution> 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, TEncodedSolution> compiledProblemDefinition;
96    protected CompiledProblemDefinition<TEncoding, TEncodedSolution> 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    public dynamic Instance {
111      get { return compiledProblemDefinition; }
112    }
113
114    public sealed override Assembly Compile() {
115      return Compile(true);
116    }
117
118    private Assembly Compile(bool fireChanged) {
119      var assembly = base.Compile();
120      var types = assembly.GetTypes();
121      if (!types.Any(x => typeof(CompiledProblemDefinition<TEncoding, TEncodedSolution>).IsAssignableFrom(x)))
122        throw new ProblemDefinitionScriptException("The compiled code doesn't contain a problem definition." + Environment.NewLine + "The problem definition must be a subclass of CompiledProblemDefinition.");
123      if (types.Count(x => typeof(CompiledProblemDefinition<TEncoding, TEncodedSolution>).IsAssignableFrom(x)) > 1)
124        throw new ProblemDefinitionScriptException("The compiled code contains multiple problem definitions." + Environment.NewLine + "Only one subclass of CompiledProblemDefinition is allowed.");
125
126      CompiledProblemDefinition<TEncoding, TEncodedSolution> inst;
127      try {
128        inst = (CompiledProblemDefinition<TEncoding, TEncodedSolution>)Activator.CreateInstance(types.Single(x => typeof(CompiledProblemDefinition<TEncoding, TEncodedSolution>).IsAssignableFrom(x)));
129      } catch (Exception e) {
130        compiledProblemDefinition = null;
131        throw new ProblemDefinitionScriptException("Instantiating the problem definition failed." + Environment.NewLine + "Check your default constructor.", e);
132      }
133
134      try {
135        inst.vars = new Variables(VariableStore);
136        inst.Encoding = encoding;
137        inst.Initialize();
138      } catch (Exception e) {
139        compiledProblemDefinition = null;
140        throw new ProblemDefinitionScriptException("Initializing the problem definition failed." + Environment.NewLine + "Check your Initialize() method.", e);
141      }
142
143      try {
144        compiledProblemDefinition = inst;
145        if (fireChanged) OnProblemDefinitionChanged();
146      } catch (Exception e) {
147        compiledProblemDefinition = null;
148        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);
149      }
150
151      codeChanged = false;
152      return assembly;
153    }
154
155    protected override void OnCodeChanged() {
156      base.OnCodeChanged();
157      compiledProblemDefinition = null;
158      codeChanged = true;
159    }
160
161    public event EventHandler ProblemDefinitionChanged;
162    protected virtual void OnProblemDefinitionChanged() {
163      var handler = ProblemDefinitionChanged;
164      if (handler != null) handler(this, EventArgs.Empty);
165    }
166  }
167}
Note: See TracBrowser for help on using the repository browser.