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 |
|
---|
22 | using System;
|
---|
23 | using System.Linq;
|
---|
24 | using System.Reflection;
|
---|
25 | using HeuristicLab.Common;
|
---|
26 | using HEAL.Attic;
|
---|
27 | using HeuristicLab.Scripting;
|
---|
28 |
|
---|
29 | namespace HeuristicLab.Problems.ExternalEvaluation {
|
---|
30 | [StorableType("F1BC4885-753B-4E47-9169-EFC2E744782C")]
|
---|
31 | public abstract class OptimizationSupportScript<T> : Script
|
---|
32 | where T : class {
|
---|
33 |
|
---|
34 | [Storable]
|
---|
35 | private VariableStore variableStore;
|
---|
36 | public VariableStore VariableStore {
|
---|
37 | get { return variableStore; }
|
---|
38 | }
|
---|
39 |
|
---|
40 | [StorableConstructor]
|
---|
41 | protected OptimizationSupportScript(StorableConstructorFlag _) : base(_) { }
|
---|
42 | protected OptimizationSupportScript(OptimizationSupportScript<T> original, Cloner cloner)
|
---|
43 | : base(original, cloner) {
|
---|
44 | variableStore = cloner.Clone(original.variableStore);
|
---|
45 | }
|
---|
46 |
|
---|
47 | protected OptimizationSupportScript()
|
---|
48 | : base() {
|
---|
49 | variableStore = new VariableStore();
|
---|
50 | }
|
---|
51 |
|
---|
52 | protected OptimizationSupportScript(string code)
|
---|
53 | : base(code) {
|
---|
54 | variableStore = new VariableStore();
|
---|
55 | }
|
---|
56 |
|
---|
57 | private readonly object compileLock = new object();
|
---|
58 | private T compiledInstance;
|
---|
59 | protected T CompiledInstance {
|
---|
60 | get {
|
---|
61 | if (compiledInstance == null) {
|
---|
62 | lock (compileLock) {
|
---|
63 | if (compiledInstance == null) {
|
---|
64 | Compile();
|
---|
65 | }
|
---|
66 | }
|
---|
67 | }
|
---|
68 | return compiledInstance;
|
---|
69 | }
|
---|
70 | private set { compiledInstance = value; }
|
---|
71 | }
|
---|
72 | public dynamic Instance {
|
---|
73 | get { return compiledInstance; }
|
---|
74 | }
|
---|
75 |
|
---|
76 | public override Assembly Compile() {
|
---|
77 | var assembly = base.Compile();
|
---|
78 | var types = assembly.GetTypes();
|
---|
79 | if (!types.Any(x => typeof(CompiledOptimizationSupport).IsAssignableFrom(x)))
|
---|
80 | throw new OptimizationSupportException("The compiled code doesn't contain an optimization support." + Environment.NewLine + "The support class must be a subclass of CompiledOptimizationSupport.");
|
---|
81 | if (types.Count(x => typeof(CompiledOptimizationSupport).IsAssignableFrom(x)) > 1)
|
---|
82 | throw new OptimizationSupportException("The compiled code contains multiple support classes." + Environment.NewLine + "Only one subclass of CompiledOptimizationSupport is allowed.");
|
---|
83 |
|
---|
84 | CompiledOptimizationSupport inst;
|
---|
85 | try {
|
---|
86 | inst = (CompiledOptimizationSupport)Activator.CreateInstance(types.Single(x => typeof(CompiledOptimizationSupport).IsAssignableFrom(x)));
|
---|
87 | inst.vars = new Variables(VariableStore);
|
---|
88 | } catch (Exception e) {
|
---|
89 | compiledInstance = null;
|
---|
90 | throw new OptimizationSupportException("Instantiating the optimization support class failed." + Environment.NewLine + "Check your default constructor.", e);
|
---|
91 | }
|
---|
92 |
|
---|
93 | var concreteInst = inst as T;
|
---|
94 | if (concreteInst == null)
|
---|
95 | throw new OptimizationSupportException("The optimization support class does not implement ISingleObjectiveOptimizationSupport." + Environment.NewLine + "Please implement that interface in the subclass of CompiledOptimizationSupport.");
|
---|
96 |
|
---|
97 | CompiledInstance = concreteInst;
|
---|
98 |
|
---|
99 | return assembly;
|
---|
100 | }
|
---|
101 |
|
---|
102 | protected override void OnCodeChanged() {
|
---|
103 | base.OnCodeChanged();
|
---|
104 | compiledInstance = null;
|
---|
105 | }
|
---|
106 | }
|
---|
107 | } |
---|