Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 13780 was 13385, checked in by abeham, 9 years ago

#2521: fixed plugin dependencies and updated samples

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      } 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        inst.Initialize();
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      } catch (Exception e) {
144        compiledProblemDefinition = null;
145        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);
146      }
147
148      codeChanged = false;
149      return assembly;
150    }
151
152    protected override void OnCodeChanged() {
153      base.OnCodeChanged();
154      compiledProblemDefinition = null;
155      codeChanged = true;
156    }
157
158    public event EventHandler ProblemDefinitionChanged;
159    protected virtual void OnProblemDefinitionChanged() {
160      var handler = ProblemDefinitionChanged;
161      if (handler != null) handler(this, EventArgs.Empty);
162    }
163  }
164}
Note: See TracBrowser for help on using the repository browser.