[11484] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[12012] | 3 | * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[11484] | 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 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.Linq;
|
---|
| 24 | using System.Reflection;
|
---|
| 25 | using HeuristicLab.Common;
|
---|
| 26 | using HeuristicLab.Core;
|
---|
[11949] | 27 | using HeuristicLab.Optimization;
|
---|
[11484] | 28 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 29 | using HeuristicLab.Scripting;
|
---|
| 30 |
|
---|
| 31 | namespace HeuristicLab.Problems.Programmable {
|
---|
| 32 | [StorableClass]
|
---|
[13345] | 33 | public abstract class ProblemDefinitionScript : Script {
|
---|
[11484] | 34 | [Storable]
|
---|
| 35 | private VariableStore variableStore;
|
---|
| 36 | public VariableStore VariableStore {
|
---|
| 37 | get { return variableStore; }
|
---|
| 38 | }
|
---|
| 39 |
|
---|
| 40 | [StorableConstructor]
|
---|
[11739] | 41 | protected ProblemDefinitionScript(bool deserializing) : base(deserializing) { }
|
---|
| 42 | protected ProblemDefinitionScript(ProblemDefinitionScript original, Cloner cloner)
|
---|
[11484] | 43 | : base(original, cloner) {
|
---|
| 44 | variableStore = cloner.Clone(original.variableStore);
|
---|
| 45 | }
|
---|
[11753] | 46 | protected ProblemDefinitionScript()
|
---|
| 47 | : base() {
|
---|
[11484] | 48 | variableStore = new VariableStore();
|
---|
| 49 | }
|
---|
[13218] | 50 | protected ProblemDefinitionScript(string code)
|
---|
| 51 | : base(code) {
|
---|
| 52 | variableStore = new VariableStore();
|
---|
| 53 | }
|
---|
[13345] | 54 | }
|
---|
[11484] | 55 |
|
---|
[13345] | 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; }
|
---|
[11739] | 70 | }
|
---|
| 71 |
|
---|
[13345] | 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) {
|
---|
[13348] | 84 | encoding = cloner.Clone(original.encoding);
|
---|
[13345] | 85 | codeChanged = original.codeChanged;
|
---|
| 86 | }
|
---|
| 87 | protected ProblemDefinitionScript()
|
---|
| 88 | : base() {
|
---|
| 89 | }
|
---|
| 90 | protected ProblemDefinitionScript(string code)
|
---|
| 91 | : base(code) {
|
---|
| 92 | }
|
---|
| 93 |
|
---|
[11900] | 94 | private readonly object compileLock = new object();
|
---|
[13345] | 95 | private volatile CompiledProblemDefinition<TEncoding, TSolution> compiledProblemDefinition;
|
---|
| 96 | protected CompiledProblemDefinition<TEncoding, TSolution> CompiledProblemDefinition {
|
---|
[11484] | 97 | get {
|
---|
[11900] | 98 | // double checked locking pattern
|
---|
| 99 | if (compiledProblemDefinition == null) {
|
---|
| 100 | lock (compileLock) {
|
---|
| 101 | if (compiledProblemDefinition == null) {
|
---|
[11944] | 102 | if (codeChanged) throw new ProblemDefinitionScriptException("The code has been changed, but was not recompiled.");
|
---|
[11900] | 103 | Compile(false);
|
---|
| 104 | }
|
---|
| 105 | }
|
---|
| 106 | }
|
---|
[11739] | 107 | return compiledProblemDefinition;
|
---|
[11484] | 108 | }
|
---|
| 109 | }
|
---|
| 110 |
|
---|
[11900] | 111 | public sealed override Assembly Compile() {
|
---|
| 112 | return Compile(true);
|
---|
| 113 | }
|
---|
| 114 |
|
---|
[11944] | 115 | private Assembly Compile(bool fireChanged) {
|
---|
[11484] | 116 | var assembly = base.Compile();
|
---|
| 117 | var types = assembly.GetTypes();
|
---|
[13345] | 118 | if (!types.Any(x => typeof(CompiledProblemDefinition<TEncoding, TSolution>).IsAssignableFrom(x)))
|
---|
[11885] | 119 | throw new ProblemDefinitionScriptException("The compiled code doesn't contain a problem definition." + Environment.NewLine + "The problem definition must be a subclass of CompiledProblemDefinition.");
|
---|
[13345] | 120 | if (types.Count(x => typeof(CompiledProblemDefinition<TEncoding, TSolution>).IsAssignableFrom(x)) > 1)
|
---|
[11885] | 121 | throw new ProblemDefinitionScriptException("The compiled code contains multiple problem definitions." + Environment.NewLine + "Only one subclass of CompiledProblemDefinition is allowed.");
|
---|
| 122 |
|
---|
[13345] | 123 | CompiledProblemDefinition<TEncoding, TSolution> inst;
|
---|
[11484] | 124 | try {
|
---|
[13345] | 125 | inst = (CompiledProblemDefinition<TEncoding, TSolution>)Activator.CreateInstance(types.Single(x => typeof(CompiledProblemDefinition<TEncoding, TSolution>).IsAssignableFrom(x)));
|
---|
[13385] | 126 | } catch (Exception e) {
|
---|
[11885] | 127 | compiledProblemDefinition = null;
|
---|
| 128 | throw new ProblemDefinitionScriptException("Instantiating the problem definition failed." + Environment.NewLine + "Check your default constructor.", e);
|
---|
| 129 | }
|
---|
| 130 |
|
---|
| 131 | try {
|
---|
[11484] | 132 | inst.vars = new Variables(VariableStore);
|
---|
[13345] | 133 | inst.Encoding = encoding;
|
---|
[13385] | 134 | inst.Initialize();
|
---|
| 135 | } catch (Exception e) {
|
---|
[11885] | 136 | compiledProblemDefinition = null;
|
---|
| 137 | throw new ProblemDefinitionScriptException("Initializing the problem definition failed." + Environment.NewLine + "Check your Initialize() method.", e);
|
---|
| 138 | }
|
---|
| 139 |
|
---|
| 140 | try {
|
---|
[11900] | 141 | compiledProblemDefinition = inst;
|
---|
| 142 | if (fireChanged) OnProblemDefinitionChanged();
|
---|
[13385] | 143 | } catch (Exception e) {
|
---|
[11739] | 144 | compiledProblemDefinition = null;
|
---|
[11885] | 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);
|
---|
[11739] | 146 | }
|
---|
[11885] | 147 |
|
---|
[11944] | 148 | codeChanged = false;
|
---|
[11484] | 149 | return assembly;
|
---|
| 150 | }
|
---|
| 151 |
|
---|
| 152 | protected override void OnCodeChanged() {
|
---|
| 153 | base.OnCodeChanged();
|
---|
[11739] | 154 | compiledProblemDefinition = null;
|
---|
[11944] | 155 | codeChanged = true;
|
---|
[11484] | 156 | }
|
---|
| 157 |
|
---|
[11739] | 158 | public event EventHandler ProblemDefinitionChanged;
|
---|
| 159 | protected virtual void OnProblemDefinitionChanged() {
|
---|
| 160 | var handler = ProblemDefinitionChanged;
|
---|
[11484] | 161 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 162 | }
|
---|
| 163 | }
|
---|
| 164 | }
|
---|