[11484] | 1 | #region License Information
|
---|
[16233] | 2 |
|
---|
[11484] | 3 | /* HeuristicLab
|
---|
[15583] | 4 | * Copyright (C) 2002-2018 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[11484] | 5 | *
|
---|
| 6 | * This file is part of HeuristicLab.
|
---|
| 7 | *
|
---|
| 8 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
| 9 | * it under the terms of the GNU General Public License as published by
|
---|
| 10 | * the Free Software Foundation, either version 3 of the License, or
|
---|
| 11 | * (at your option) any later version.
|
---|
| 12 | *
|
---|
| 13 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 16 | * GNU General Public License for more details.
|
---|
| 17 | *
|
---|
| 18 | * You should have received a copy of the GNU General Public License
|
---|
| 19 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
| 20 | */
|
---|
| 21 |
|
---|
[16233] | 22 | #endregion License Information
|
---|
| 23 |
|
---|
[16172] | 24 | using System;
|
---|
| 25 | using System.Linq;
|
---|
| 26 | using System.Reflection;
|
---|
| 27 | using Google.OrTools.LinearSolver;
|
---|
[11484] | 28 | using HeuristicLab.Common;
|
---|
| 29 | using HeuristicLab.Core;
|
---|
| 30 | using HeuristicLab.Optimization;
|
---|
| 31 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
[16172] | 32 | using HeuristicLab.Problems.Programmable;
|
---|
| 33 | using HeuristicLab.Scripting;
|
---|
[11484] | 34 |
|
---|
[16172] | 35 | namespace HeuristicLab.MathematicalOptimization.LinearProgramming.Problems {
|
---|
[16233] | 36 |
|
---|
[11484] | 37 | [Item("Single-objective Problem Definition Script", "Script that defines the parameter vector and evaluates the solution for a programmable problem.")]
|
---|
| 38 | [StorableClass]
|
---|
[16172] | 39 | public sealed class LinearProgrammingProblemDefinitionScript : Script, ILinearProgrammingProblemDefinition, IStorableContent {
|
---|
| 40 | protected bool SuppressEvents { get; set; }
|
---|
[11484] | 41 |
|
---|
[16172] | 42 | [Storable]
|
---|
| 43 | private VariableStore variableStore;
|
---|
[16233] | 44 |
|
---|
[16172] | 45 | public VariableStore VariableStore => variableStore;
|
---|
[11484] | 46 |
|
---|
[16172] | 47 | [Storable]
|
---|
| 48 | private bool codeChanged;
|
---|
| 49 |
|
---|
[11484] | 50 | [StorableConstructor]
|
---|
[16172] | 51 | protected LinearProgrammingProblemDefinitionScript(bool deserializing) : base(deserializing) { }
|
---|
[16233] | 52 |
|
---|
[16172] | 53 | protected LinearProgrammingProblemDefinitionScript(LinearProgrammingProblemDefinitionScript original, Cloner cloner)
|
---|
| 54 | : base(original, cloner) {
|
---|
| 55 | variableStore = cloner.Clone(original.variableStore);
|
---|
| 56 | codeChanged = original.codeChanged;
|
---|
| 57 | }
|
---|
[11484] | 58 |
|
---|
[16172] | 59 | public LinearProgrammingProblemDefinitionScript()
|
---|
| 60 | : base(ScriptTemplates.CompiledLinearProgrammingProblemDefinition) {
|
---|
| 61 | variableStore = new VariableStore();
|
---|
[11484] | 62 | }
|
---|
| 63 |
|
---|
[16172] | 64 | private readonly object compileLock = new object();
|
---|
| 65 | private volatile ILinearProgrammingProblemDefinition compiledProblemDefinition;
|
---|
[16233] | 66 |
|
---|
[16172] | 67 | protected ILinearProgrammingProblemDefinition CompiledProblemDefinition {
|
---|
| 68 | get {
|
---|
| 69 | // double checked locking pattern
|
---|
| 70 | if (compiledProblemDefinition == null) {
|
---|
| 71 | lock (compileLock) {
|
---|
| 72 | if (compiledProblemDefinition == null) {
|
---|
[16233] | 73 | if (codeChanged)
|
---|
| 74 | throw new ProblemDefinitionScriptException("The code has been changed, but was not recompiled.");
|
---|
[16172] | 75 | Compile(false);
|
---|
| 76 | }
|
---|
| 77 | }
|
---|
| 78 | }
|
---|
| 79 | return compiledProblemDefinition;
|
---|
| 80 | }
|
---|
[11484] | 81 | }
|
---|
[16233] | 82 |
|
---|
[16172] | 83 | public dynamic Instance => compiledProblemDefinition;
|
---|
[11484] | 84 |
|
---|
[16233] | 85 | public override Assembly Compile() => Compile(true);
|
---|
[11484] | 86 |
|
---|
[16172] | 87 | private Assembly Compile(bool fireChanged) {
|
---|
| 88 | var assembly = base.Compile();
|
---|
| 89 | var types = assembly.GetTypes();
|
---|
| 90 | if (!types.Any(x => typeof(CompiledProblemDefinition).IsAssignableFrom(x)))
|
---|
[16233] | 91 | throw new ProblemDefinitionScriptException("The compiled code doesn't contain a problem definition." +
|
---|
| 92 | Environment.NewLine +
|
---|
| 93 | "The problem definition must be a subclass of CompiledProblemDefinition.");
|
---|
[16172] | 94 | if (types.Count(x => typeof(CompiledProblemDefinition).IsAssignableFrom(x)) > 1)
|
---|
[16233] | 95 | throw new ProblemDefinitionScriptException("The compiled code contains multiple problem definitions." +
|
---|
| 96 | Environment.NewLine +
|
---|
| 97 | "Only one subclass of CompiledProblemDefinition is allowed.");
|
---|
[16172] | 98 |
|
---|
| 99 | CompiledProblemDefinition inst;
|
---|
| 100 | try {
|
---|
[16233] | 101 | inst = (CompiledProblemDefinition)Activator.CreateInstance(types.Single(x =>
|
---|
| 102 | typeof(CompiledProblemDefinition).IsAssignableFrom(x)));
|
---|
[16172] | 103 | } catch (Exception e) {
|
---|
| 104 | compiledProblemDefinition = null;
|
---|
[16233] | 105 | throw new ProblemDefinitionScriptException(
|
---|
| 106 | "Instantiating the problem definition failed." + Environment.NewLine + "Check your default constructor.", e);
|
---|
[16172] | 107 | }
|
---|
| 108 |
|
---|
| 109 | try {
|
---|
| 110 | inst.vars = new Variables(VariableStore);
|
---|
| 111 | inst.Initialize();
|
---|
| 112 | } catch (Exception e) {
|
---|
| 113 | compiledProblemDefinition = null;
|
---|
[16233] | 114 | throw new ProblemDefinitionScriptException(
|
---|
| 115 | "Initializing the problem definition failed." + Environment.NewLine + "Check your Initialize() method.", e);
|
---|
[16172] | 116 | }
|
---|
| 117 |
|
---|
| 118 | try {
|
---|
| 119 | compiledProblemDefinition = (ILinearProgrammingProblemDefinition)inst;
|
---|
| 120 | if (fireChanged) OnProblemDefinitionChanged();
|
---|
| 121 | } catch (Exception e) {
|
---|
| 122 | compiledProblemDefinition = null;
|
---|
[16233] | 123 | throw new ProblemDefinitionScriptException(
|
---|
| 124 | "Using the problem definition in the problem failed." + Environment.NewLine +
|
---|
| 125 | "Examine this error message carefully (often there is an issue with the defined encoding).", e);
|
---|
[16172] | 126 | }
|
---|
| 127 |
|
---|
| 128 | codeChanged = false;
|
---|
| 129 | return assembly;
|
---|
[11484] | 130 | }
|
---|
[16172] | 131 |
|
---|
| 132 | protected override void OnCodeChanged() {
|
---|
| 133 | base.OnCodeChanged();
|
---|
| 134 | compiledProblemDefinition = null;
|
---|
| 135 | codeChanged = true;
|
---|
[11753] | 136 | }
|
---|
[16172] | 137 |
|
---|
| 138 | public event EventHandler ProblemDefinitionChanged;
|
---|
| 139 |
|
---|
[16233] | 140 | private void OnProblemDefinitionChanged() => ProblemDefinitionChanged?.Invoke(this, EventArgs.Empty);
|
---|
| 141 |
|
---|
[16172] | 142 | public string Filename { get; set; }
|
---|
| 143 |
|
---|
| 144 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 145 | return new LinearProgrammingProblemDefinitionScript(this, cloner);
|
---|
| 146 | }
|
---|
| 147 |
|
---|
| 148 | public void BuildModel(Solver solver) => CompiledProblemDefinition.BuildModel(solver);
|
---|
| 149 |
|
---|
| 150 | public void Analyze(Solver solver, ResultCollection results) => CompiledProblemDefinition.Analyze(solver, results);
|
---|
[11484] | 151 | }
|
---|
[16233] | 152 | } |
---|