Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2522_RefactorPluginInfrastructure/HeuristicLab.Problems.ExternalEvaluation/3.4/Programmable/OptimizationSupportScript.cs @ 18242

Last change on this file since 18242 was 15973, checked in by gkronber, 7 years ago

#2522: merged trunk changes from r13402:15972 to branch resolving conflicts where necessary

File size: 4.0 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2018 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.Persistence.Default.CompositeSerializers.Storable;
27using HeuristicLab.Scripting;
28
29namespace HeuristicLab.Problems.ExternalEvaluation.Programmable {
30  public abstract class OptimizationSupportScript<T> : Script
31    where T : class {
32
33    [Storable]
34    private VariableStore variableStore;
35    public VariableStore VariableStore {
36      get { return variableStore; }
37    }
38
39    [StorableConstructor]
40    protected OptimizationSupportScript(bool deserializing) : base(deserializing) { }
41    protected OptimizationSupportScript(OptimizationSupportScript<T> original, Cloner cloner)
42      : base(original, cloner) {
43      variableStore = cloner.Clone(original.variableStore);
44    }
45
46    protected OptimizationSupportScript()
47      : base() {
48      variableStore = new VariableStore();
49    }
50
51    protected OptimizationSupportScript(string code)
52      : base(code) {
53      variableStore = new VariableStore();
54    }
55
56    private readonly object compileLock = new object();
57    private T compiledInstance;
58    protected T CompiledInstance {
59      get {
60        if (compiledInstance == null) {
61          lock (compileLock) {
62            if (compiledInstance == null) {
63              Compile();
64            }
65          }
66        }
67        return compiledInstance;
68      }
69      private set { compiledInstance = value; }
70    }
71    public dynamic Instance {
72      get { return compiledInstance; }
73    }
74
75    public override Assembly Compile() {
76      var assembly = base.Compile();
77      var types = assembly.GetTypes();
78      if (!types.Any(x => typeof(CompiledOptimizationSupport).IsAssignableFrom(x)))
79        throw new OptimizationSupportException("The compiled code doesn't contain an optimization support." + Environment.NewLine + "The support class must be a subclass of CompiledOptimizationSupport.");
80      if (types.Count(x => typeof(CompiledOptimizationSupport).IsAssignableFrom(x)) > 1)
81        throw new OptimizationSupportException("The compiled code contains multiple support classes." + Environment.NewLine + "Only one subclass of CompiledOptimizationSupport is allowed.");
82
83      CompiledOptimizationSupport inst;
84      try {
85        inst = (CompiledOptimizationSupport)Activator.CreateInstance(types.Single(x => typeof(CompiledOptimizationSupport).IsAssignableFrom(x)));
86        inst.vars = new Variables(VariableStore);
87      } catch (Exception e) {
88        compiledInstance = null;
89        throw new OptimizationSupportException("Instantiating the optimization support class failed." + Environment.NewLine + "Check your default constructor.", e);
90      }
91
92      var concreteInst = inst as T;
93      if (concreteInst == null)
94        throw new OptimizationSupportException("The optimization support class does not implement ISingleObjectiveOptimizationSupport." + Environment.NewLine + "Please implement that interface in the subclass of CompiledOptimizationSupport.");
95
96      CompiledInstance = concreteInst;
97
98      return assembly;
99    }
100
101    protected override void OnCodeChanged() {
102      base.OnCodeChanged();
103      compiledInstance = null;
104    }
105  }
106}
Note: See TracBrowser for help on using the repository browser.