Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/HeuristicLab.ExactOptimization/3.3/LinearProgramming/Problems/ProgrammableLinearProblemDefinition.cs @ 17753

Last change on this file since 17753 was 17180, checked in by swagner, 5 years ago

#2875: Removed years in copyrights

File size: 5.9 KB
RevLine 
[11484]1#region License Information
2/* HeuristicLab
[17180]3 * Copyright (C) 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 */
[16288]20#endregion
[11484]21
[16172]22using System;
23using System.Linq;
24using System.Reflection;
25using Google.OrTools.LinearSolver;
[11484]26using HeuristicLab.Common;
27using HeuristicLab.Core;
[16582]28using HeuristicLab.ExactOptimization.LinearProgramming.Templates;
[11484]29using HeuristicLab.Optimization;
[16172]30using HeuristicLab.Problems.Programmable;
31using HeuristicLab.Scripting;
[16736]32using HEAL.Attic;
[11484]33
[16582]34namespace HeuristicLab.ExactOptimization.LinearProgramming {
[16233]35
[16582]36  [Item("Programmable Linear Problem Definition (LP, MIP)",
37    "Script that defines the model for a linear/mixed integer programming problem.")]
[16736]38  [StorableType("830F82CF-9FF1-4619-A75E-E43E208565F0")]
[16582]39  public sealed class ProgrammableLinearProblemDefinition : Script, ILinearProblemDefinition,
[16405]40    IStorableContent {
41    private readonly object compileLock = new object();
[11484]42
[16582]43    [Storable]
44    private readonly VariableStore variableStore;
[16233]45
[16582]46    [Storable]
47    private bool codeChanged;
[11484]48
[16582]49    private volatile ILinearProblemDefinition compiledProblemDefinition;
[16172]50
[16582]51    public ProgrammableLinearProblemDefinition()
52      : base(ScriptTemplates.CompiledLinearProblemDefinition) {
53      Name = "Programmable Linear Problem Definition";
[16405]54      variableStore = new VariableStore();
55    }
56
[11484]57    [StorableConstructor]
[16736]58    private ProgrammableLinearProblemDefinition(StorableConstructorFlag _) : base(_) { }
[16233]59
[16582]60    private ProgrammableLinearProblemDefinition(ProgrammableLinearProblemDefinition original,
[16405]61      Cloner cloner) : base(original, cloner) {
[16172]62      variableStore = cloner.Clone(original.variableStore);
63      codeChanged = original.codeChanged;
64    }
[11484]65
[16405]66    public event EventHandler ProblemDefinitionChanged;
[11484]67
[16405]68    public string Filename { get; set; }
69    public dynamic Instance => compiledProblemDefinition;
70    public VariableStore VariableStore => variableStore;
[16233]71
[16582]72    private ILinearProblemDefinition CompiledProblemDefinition {
[16172]73      get {
74        // double checked locking pattern
75        if (compiledProblemDefinition == null) {
76          lock (compileLock) {
77            if (compiledProblemDefinition == null) {
[16233]78              if (codeChanged)
79                throw new ProblemDefinitionScriptException("The code has been changed, but was not recompiled.");
[16172]80              Compile(false);
81            }
82          }
83        }
[16405]84
[16172]85        return compiledProblemDefinition;
86      }
[11484]87    }
[16233]88
[16405]89    public void Analyze(Solver solver, ResultCollection results) => CompiledProblemDefinition.Analyze(solver, results);
90
91    public void BuildModel(Solver solver) => CompiledProblemDefinition.BuildModel(solver);
92
93    public override IDeepCloneable Clone(Cloner cloner) {
[16582]94      return new ProgrammableLinearProblemDefinition(this, cloner);
[16405]95    }
96
[16233]97    public override Assembly Compile() => Compile(true);
[11484]98
[16405]99    protected override void OnCodeChanged() {
100      base.OnCodeChanged();
101      compiledProblemDefinition = null;
102      codeChanged = true;
103    }
104
[16172]105    private Assembly Compile(bool fireChanged) {
106      var assembly = base.Compile();
107      var types = assembly.GetTypes();
108      if (!types.Any(x => typeof(CompiledProblemDefinition).IsAssignableFrom(x)))
[16405]109        throw new ProblemDefinitionScriptException("The compiled code doesn't contain a problem definition." + Environment.NewLine +
[16582]110                                                   $"The problem definition must be a subclass of {nameof(CompiledProblemDefinition)} and implement {nameof(ILinearProblemDefinition)}.");
[16172]111      if (types.Count(x => typeof(CompiledProblemDefinition).IsAssignableFrom(x)) > 1)
[16405]112        throw new ProblemDefinitionScriptException("The compiled code contains multiple problem definitions." + Environment.NewLine +
[16582]113                                                   $"Only one subclass of {nameof(CompiledProblemDefinition)} is allowed.");
[16172]114
115      CompiledProblemDefinition inst;
116      try {
[16233]117        inst = (CompiledProblemDefinition)Activator.CreateInstance(types.Single(x =>
118         typeof(CompiledProblemDefinition).IsAssignableFrom(x)));
[16172]119      } catch (Exception e) {
120        compiledProblemDefinition = null;
[16233]121        throw new ProblemDefinitionScriptException(
122          "Instantiating the problem definition failed." + Environment.NewLine + "Check your default constructor.", e);
[16172]123      }
124
125      try {
126        inst.vars = new Variables(VariableStore);
127        inst.Initialize();
128      } catch (Exception e) {
129        compiledProblemDefinition = null;
[16233]130        throw new ProblemDefinitionScriptException(
131          "Initializing the problem definition failed." + Environment.NewLine + "Check your Initialize() method.", e);
[16172]132      }
133
134      try {
[16582]135        compiledProblemDefinition = (ILinearProblemDefinition)inst;
[16172]136        if (fireChanged) OnProblemDefinitionChanged();
137      } catch (Exception e) {
138        compiledProblemDefinition = null;
[16233]139        throw new ProblemDefinitionScriptException(
140          "Using the problem definition in the problem failed." + Environment.NewLine +
[16582]141          "Examine this error message carefully.", e);
[16172]142      }
143
144      codeChanged = false;
145      return assembly;
[11484]146    }
[16172]147
[16233]148    private void OnProblemDefinitionChanged() => ProblemDefinitionChanged?.Invoke(this, EventArgs.Empty);
[11484]149  }
[16582]150}
Note: See TracBrowser for help on using the repository browser.