Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.ExternalEvaluation/3.4/Programmable/OptimizationSupportScript.cs @ 13183

Last change on this file since 13183 was 13183, checked in by pfleck, 8 years ago

#1674

  • Added a SupportScript for the MultiObjectiveExternalEvaluationProblem.
  • Extracted code from the SingleObjectiveOptimizationSupportScript into the OptimizationSupportScript to reuse code for the MultiObjectiveOptimizationSupportScript.
File size: 3.8 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 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    private readonly object compileLock = new object();
52    private T compiledInstance;
53    protected T CompiledInstance {
54      get {
55        if (compiledInstance == null) {
56          lock (compileLock) {
57            if (compiledInstance == null) {
58              Compile();
59            }
60          }
61        }
62        return compiledInstance;
63      }
64      private set { compiledInstance = value; }
65    }
66
67    public override Assembly Compile() {
68      var assembly = base.Compile();
69      var types = assembly.GetTypes();
70      if (!types.Any(x => typeof(CompiledOptimizationSupport).IsAssignableFrom(x)))
71        throw new OptimizationSupportException("The compiled code doesn't contain an optimization support." + Environment.NewLine + "The support class must be a subclass of CompiledOptimizationSupport.");
72      if (types.Count(x => typeof(CompiledOptimizationSupport).IsAssignableFrom(x)) > 1)
73        throw new OptimizationSupportException("The compiled code contains multiple support classes." + Environment.NewLine + "Only one subclass of CompiledOptimizationSupport is allowed.");
74
75      CompiledOptimizationSupport inst;
76      try {
77        inst = (CompiledOptimizationSupport)Activator.CreateInstance(types.Single(x => typeof(CompiledOptimizationSupport).IsAssignableFrom(x)));
78        inst.vars = new Variables(VariableStore);
79      } catch (Exception e) {
80        compiledInstance = null;
81        throw new OptimizationSupportException("Instantiating the optimization support class failed." + Environment.NewLine + "Check your default constructor.", e);
82      }
83
84      var concreteInst = inst as T;
85      if (concreteInst == null)
86        throw new OptimizationSupportException("The optimization support class does not implement ISingleObjectiveOptimizationSupport." + Environment.NewLine + "Please implement that interface in the subclass of CompiledOptimizationSupport.");
87
88      CompiledInstance = concreteInst;
89
90      return assembly;
91    }
92
93    protected override void OnCodeChanged() {
94      base.OnCodeChanged();
95      compiledInstance = null;
96    }
97  }
98}
Note: See TracBrowser for help on using the repository browser.