1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2014 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 |
|
---|
22 | using System;
|
---|
23 | using System.Linq;
|
---|
24 | using System.Reflection;
|
---|
25 | using HeuristicLab.Common;
|
---|
26 | using HeuristicLab.Core;
|
---|
27 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
28 | using HeuristicLab.Scripting;
|
---|
29 |
|
---|
30 | namespace HeuristicLab.Problems.Programmable {
|
---|
31 | [Item("ProblemDefinitionScript", "Script that defines the parameter vector and evaluates the solution for a programmable problem.")]
|
---|
32 | [StorableClass]
|
---|
33 | public abstract class ProblemDefinitionScript : Script, IProblemDefinition {
|
---|
34 | protected bool SuppressEvents { get; set; }
|
---|
35 |
|
---|
36 | [Storable]
|
---|
37 | private VariableStore variableStore;
|
---|
38 | public VariableStore VariableStore {
|
---|
39 | get { return variableStore; }
|
---|
40 | }
|
---|
41 |
|
---|
42 | [StorableConstructor]
|
---|
43 | protected ProblemDefinitionScript(bool deserializing) : base(deserializing) { }
|
---|
44 | protected ProblemDefinitionScript(ProblemDefinitionScript original, Cloner cloner)
|
---|
45 | : base(original, cloner) {
|
---|
46 | variableStore = cloner.Clone(original.variableStore);
|
---|
47 | }
|
---|
48 | protected ProblemDefinitionScript()
|
---|
49 | : base() {
|
---|
50 | variableStore = new VariableStore();
|
---|
51 | }
|
---|
52 |
|
---|
53 | IEncoding IProblemDefinition.Encoding {
|
---|
54 | get { return CompiledProblemDefinition.Encoding; }
|
---|
55 | }
|
---|
56 |
|
---|
57 | private readonly object locker = new object();
|
---|
58 | private volatile IProblemDefinition compiledProblemDefinition;
|
---|
59 | protected IProblemDefinition CompiledProblemDefinition {
|
---|
60 | get {
|
---|
61 | if (compiledProblemDefinition == null) throw new InvalidOperationException("The problem definition script is not compiled and cannot be used.");
|
---|
62 | return compiledProblemDefinition;
|
---|
63 | }
|
---|
64 | private set {
|
---|
65 | compiledProblemDefinition = value;
|
---|
66 | OnProblemDefinitionChanged();
|
---|
67 | }
|
---|
68 | }
|
---|
69 |
|
---|
70 | public override Assembly Compile() {
|
---|
71 | var assembly = base.Compile();
|
---|
72 | var types = assembly.GetTypes();
|
---|
73 | try {
|
---|
74 | var inst = (CompiledProblemDefinition)Activator.CreateInstance(types.First(x => typeof(CompiledProblemDefinition).IsAssignableFrom(x)));
|
---|
75 | inst.vars = new Variables(VariableStore);
|
---|
76 | inst.Initialize();
|
---|
77 | CompiledProblemDefinition = inst;
|
---|
78 | }
|
---|
79 | catch {
|
---|
80 | compiledProblemDefinition = null;
|
---|
81 | }
|
---|
82 | return assembly;
|
---|
83 | }
|
---|
84 |
|
---|
85 | protected override void OnCodeChanged() {
|
---|
86 | base.OnCodeChanged();
|
---|
87 | compiledProblemDefinition = null;
|
---|
88 | }
|
---|
89 |
|
---|
90 | public event EventHandler ProblemDefinitionChanged;
|
---|
91 | protected virtual void OnProblemDefinitionChanged() {
|
---|
92 | var handler = ProblemDefinitionChanged;
|
---|
93 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
94 | }
|
---|
95 | }
|
---|
96 | }
|
---|