Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ProgrammableProblem/HeuristicLab.Problems.ExternalEvaluation/3.4/Programmable/SingleObjectiveOptimizationSupportScript.cs @ 11893

Last change on this file since 11893 was 11893, checked in by abeham, 9 years ago

#2174:

  • Added possibility to define neighborhood and analyze function for external evaluation problems
File size: 4.9 KB
Line 
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
22using System;
23using System.Collections.Generic;
24using System.Linq;
25using System.Reflection;
26using HeuristicLab.Common;
27using HeuristicLab.Core;
28using HeuristicLab.Optimization;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30using HeuristicLab.Problems.ExternalEvaluation.Programmable;
31using HeuristicLab.Problems.Programmable;
32using HeuristicLab.Scripting;
33
34namespace HeuristicLab.Problems.ExternalEvaluation {
35  [Item("ProblemDefinitionScript", "Script that defines the parameter vector and evaluates the solution for a programmable problem.")]
36  [StorableClass]
37  public sealed class SingleObjectiveOptimizationSupportScript : Script, ISingleObjectiveOptimizationSupport {
38
39    [Storable]
40    private VariableStore variableStore;
41    public VariableStore VariableStore {
42      get { return variableStore; }
43    }
44
45    protected override string CodeTemplate {
46      get { return Templates.CompiledSingleObjectiveOptimizationSupport; }
47    }
48
49    [StorableConstructor]
50    private SingleObjectiveOptimizationSupportScript(bool deserializing) : base(deserializing) { }
51    private SingleObjectiveOptimizationSupportScript(SingleObjectiveOptimizationSupportScript original, Cloner cloner)
52      : base(original, cloner) {
53      variableStore = cloner.Clone(original.variableStore);
54    }
55    public SingleObjectiveOptimizationSupportScript()
56      : base() {
57      variableStore = new VariableStore();
58    }
59
60    public override IDeepCloneable Clone(Cloner cloner) {
61      return new SingleObjectiveOptimizationSupportScript(this, cloner);
62    }
63
64    private volatile ISingleObjectiveOptimizationSupport compiledInstance;
65    private ISingleObjectiveOptimizationSupport CompiledInstance {
66      get {
67        if (compiledInstance == null) throw new InvalidOperationException("The problem definition script is not compiled and cannot be used.");
68        return compiledInstance;
69      }
70      set { compiledInstance = value; }
71    }
72
73    public override Assembly Compile() {
74      var assembly = base.Compile();
75      var types = assembly.GetTypes();
76      if (!types.Any(x => typeof(CompiledOptimizationSupport).IsAssignableFrom(x)))
77        throw new SingleObjectiveOptimizationSupportException("The compiled code doesn't contain an optimization support." + Environment.NewLine + "The support class must be a subclass of CompiledOptimizationSupport.");
78      if (types.Count(x => typeof(CompiledOptimizationSupport).IsAssignableFrom(x)) > 1)
79        throw new SingleObjectiveOptimizationSupportException("The compiled code contains multiple support classes." + Environment.NewLine + "Only one subclass of CompiledOptimizationSupport is allowed.");
80
81      CompiledOptimizationSupport inst;
82      try {
83        inst = (CompiledOptimizationSupport)Activator.CreateInstance(types.Single(x => typeof(CompiledOptimizationSupport).IsAssignableFrom(x)));
84        inst.vars = new Variables(VariableStore);
85      } catch (Exception e) {
86        compiledInstance = null;
87        throw new SingleObjectiveOptimizationSupportException("Instantiating the optimization support class failed." + Environment.NewLine + "Check your default constructor.", e);
88      }
89
90      var soInst = inst as ISingleObjectiveOptimizationSupport;
91      if (soInst == null)
92        throw new SingleObjectiveOptimizationSupportException("The optimization support class does not implement ISingleObjectiveOptimizationSupport." + Environment.NewLine + "Please implement that interface in the subclass of CompiledOptimizationSupport.");
93
94      CompiledInstance = soInst;
95
96      return assembly;
97    }
98
99    protected override void OnCodeChanged() {
100      base.OnCodeChanged();
101      compiledInstance = null;
102    }
103
104    void ISingleObjectiveOptimizationSupport.Analyze(Individual[] individuals, double[] qualities, ResultCollection results, IRandom random) {
105      CompiledInstance.Analyze(individuals, qualities, results, random);
106    }
107
108    IEnumerable<Individual> ISingleObjectiveOptimizationSupport.GetNeighbors(Individual individual, IRandom random) {
109      return CompiledInstance.GetNeighbors(individual, random);
110    }
111  }
112}
Note: See TracBrowser for help on using the repository browser.