[11484] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[17181] | 3 | * Copyright (C) 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;
|
---|
[17097] | 28 | using HEAL.Attic;
|
---|
[11484] | 29 | using HeuristicLab.Scripting;
|
---|
| 30 |
|
---|
| 31 | namespace HeuristicLab.Problems.Programmable {
|
---|
| 32 | [Item("ProblemDefinitionScript", "Script that defines the parameter vector and evaluates the solution for a programmable problem.")]
|
---|
[17097] | 33 | [StorableType("5573B778-C60C-44BF-98FB-A8E189818C00")]
|
---|
[11739] | 34 | public abstract class ProblemDefinitionScript : Script, IProblemDefinition {
|
---|
[11484] | 35 | protected bool SuppressEvents { get; set; }
|
---|
| 36 |
|
---|
| 37 | [Storable]
|
---|
| 38 | private VariableStore variableStore;
|
---|
| 39 | public VariableStore VariableStore {
|
---|
| 40 | get { return variableStore; }
|
---|
| 41 | }
|
---|
| 42 |
|
---|
[11944] | 43 | [Storable]
|
---|
| 44 | private bool codeChanged;
|
---|
| 45 |
|
---|
[11484] | 46 | [StorableConstructor]
|
---|
[17097] | 47 | protected ProblemDefinitionScript(StorableConstructorFlag _) : base(_) { }
|
---|
[11739] | 48 | protected ProblemDefinitionScript(ProblemDefinitionScript original, Cloner cloner)
|
---|
[11484] | 49 | : base(original, cloner) {
|
---|
| 50 | variableStore = cloner.Clone(original.variableStore);
|
---|
[11944] | 51 | codeChanged = original.codeChanged;
|
---|
[11484] | 52 | }
|
---|
[11753] | 53 | protected ProblemDefinitionScript()
|
---|
| 54 | : base() {
|
---|
[11484] | 55 | variableStore = new VariableStore();
|
---|
| 56 | }
|
---|
[13277] | 57 | protected ProblemDefinitionScript(string code)
|
---|
| 58 | : base(code) {
|
---|
| 59 | variableStore = new VariableStore();
|
---|
| 60 | }
|
---|
[11484] | 61 |
|
---|
[11739] | 62 | IEncoding IProblemDefinition.Encoding {
|
---|
[11753] | 63 | get { return CompiledProblemDefinition.Encoding; }
|
---|
[11739] | 64 | }
|
---|
| 65 |
|
---|
[11900] | 66 | private readonly object compileLock = new object();
|
---|
[11739] | 67 | private volatile IProblemDefinition compiledProblemDefinition;
|
---|
| 68 | protected IProblemDefinition CompiledProblemDefinition {
|
---|
[11484] | 69 | get {
|
---|
[11900] | 70 | // double checked locking pattern
|
---|
| 71 | if (compiledProblemDefinition == null) {
|
---|
| 72 | lock (compileLock) {
|
---|
| 73 | if (compiledProblemDefinition == null) {
|
---|
[11944] | 74 | if (codeChanged) throw new ProblemDefinitionScriptException("The code has been changed, but was not recompiled.");
|
---|
[11900] | 75 | Compile(false);
|
---|
| 76 | }
|
---|
| 77 | }
|
---|
| 78 | }
|
---|
[11739] | 79 | return compiledProblemDefinition;
|
---|
[11484] | 80 | }
|
---|
| 81 | }
|
---|
[15260] | 82 | public dynamic Instance {
|
---|
| 83 | get { return compiledProblemDefinition; }
|
---|
| 84 | }
|
---|
[11484] | 85 |
|
---|
[11900] | 86 | public sealed override Assembly Compile() {
|
---|
| 87 | return Compile(true);
|
---|
| 88 | }
|
---|
| 89 |
|
---|
[11944] | 90 | private Assembly Compile(bool fireChanged) {
|
---|
[11484] | 91 | var assembly = base.Compile();
|
---|
| 92 | var types = assembly.GetTypes();
|
---|
[11885] | 93 | if (!types.Any(x => typeof(CompiledProblemDefinition).IsAssignableFrom(x)))
|
---|
| 94 | throw new ProblemDefinitionScriptException("The compiled code doesn't contain a problem definition." + Environment.NewLine + "The problem definition must be a subclass of CompiledProblemDefinition.");
|
---|
| 95 | if (types.Count(x => typeof(CompiledProblemDefinition).IsAssignableFrom(x)) > 1)
|
---|
| 96 | throw new ProblemDefinitionScriptException("The compiled code contains multiple problem definitions." + Environment.NewLine + "Only one subclass of CompiledProblemDefinition is allowed.");
|
---|
| 97 |
|
---|
| 98 | CompiledProblemDefinition inst;
|
---|
[11484] | 99 | try {
|
---|
[11885] | 100 | inst = (CompiledProblemDefinition)Activator.CreateInstance(types.Single(x => typeof(CompiledProblemDefinition).IsAssignableFrom(x)));
|
---|
| 101 | } catch (Exception e) {
|
---|
| 102 | compiledProblemDefinition = null;
|
---|
| 103 | throw new ProblemDefinitionScriptException("Instantiating the problem definition failed." + Environment.NewLine + "Check your default constructor.", e);
|
---|
| 104 | }
|
---|
| 105 |
|
---|
| 106 | try {
|
---|
[11484] | 107 | inst.vars = new Variables(VariableStore);
|
---|
| 108 | inst.Initialize();
|
---|
[11885] | 109 | } catch (Exception e) {
|
---|
| 110 | compiledProblemDefinition = null;
|
---|
| 111 | throw new ProblemDefinitionScriptException("Initializing the problem definition failed." + Environment.NewLine + "Check your Initialize() method.", e);
|
---|
| 112 | }
|
---|
| 113 |
|
---|
| 114 | try {
|
---|
[11900] | 115 | compiledProblemDefinition = inst;
|
---|
| 116 | if (fireChanged) OnProblemDefinitionChanged();
|
---|
[11880] | 117 | } catch (Exception e) {
|
---|
[11739] | 118 | compiledProblemDefinition = null;
|
---|
[11885] | 119 | 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] | 120 | }
|
---|
[11885] | 121 |
|
---|
[11944] | 122 | codeChanged = false;
|
---|
[11484] | 123 | return assembly;
|
---|
| 124 | }
|
---|
| 125 |
|
---|
| 126 | protected override void OnCodeChanged() {
|
---|
| 127 | base.OnCodeChanged();
|
---|
[11739] | 128 | compiledProblemDefinition = null;
|
---|
[11944] | 129 | codeChanged = true;
|
---|
[11484] | 130 | }
|
---|
| 131 |
|
---|
[11739] | 132 | public event EventHandler ProblemDefinitionChanged;
|
---|
| 133 | protected virtual void OnProblemDefinitionChanged() {
|
---|
| 134 | var handler = ProblemDefinitionChanged;
|
---|
[11484] | 135 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 136 | }
|
---|
| 137 | }
|
---|
| 138 | }
|
---|