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