[16172] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[17180] | 3 | * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[16172] | 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
|
---|
[16172] | 21 |
|
---|
[16405] | 22 | using System;
|
---|
[16582] | 23 | using System.Collections.Generic;
|
---|
| 24 | using System.IO;
|
---|
| 25 | using System.Linq;
|
---|
| 26 | using System.Reflection;
|
---|
| 27 | using Google.OrTools.LinearSolver;
|
---|
[16910] | 28 | using HEAL.Attic;
|
---|
[16172] | 29 | using HeuristicLab.Common;
|
---|
| 30 | using HeuristicLab.Core;
|
---|
[16582] | 31 | using HeuristicLab.Data;
|
---|
[16172] | 32 | using HeuristicLab.Optimization;
|
---|
[16582] | 33 | using HeuristicLab.Parameters;
|
---|
[16172] | 34 |
|
---|
[16582] | 35 | namespace HeuristicLab.ExactOptimization.LinearProgramming {
|
---|
[16233] | 36 |
|
---|
[16582] | 37 | [Item("Mixed-Integer Linear Programming Problem (LP, MIP)", "Represents a linear/mixed integer problem.")]
|
---|
[16736] | 38 | [StorableType("0F6BD4A4-8C70-4922-9BA1-1F372820DD76")]
|
---|
[16582] | 39 | public sealed class LinearProblem : Problem, IStorableContent {
|
---|
| 40 |
|
---|
[16405] | 41 | [Storable]
|
---|
[16582] | 42 | private readonly IValueParameter<ILinearProblemDefinition> problemDefinitionParam;
|
---|
[16172] | 43 |
|
---|
[16582] | 44 | public LinearProblem() {
|
---|
[16405] | 45 | Parameters.Remove(Parameters["Operators"]);
|
---|
[16910] | 46 | Parameters.Add(problemDefinitionParam = new ValueParameter<ILinearProblemDefinition>("Model", "The linear programming problem",
|
---|
| 47 | new ProgrammableLinearProblemDefinition()) { GetsCollected = false });
|
---|
[16233] | 48 | }
|
---|
[16172] | 49 |
|
---|
[16582] | 50 | private LinearProblem(LinearProblem original, Cloner cloner)
|
---|
[16172] | 51 | : base(original, cloner) {
|
---|
[16582] | 52 | problemDefinitionParam = cloner.Clone(original.problemDefinitionParam);
|
---|
[16172] | 53 | }
|
---|
| 54 |
|
---|
| 55 | [StorableConstructor]
|
---|
[16736] | 56 | private LinearProblem(StorableConstructorFlag _) : base(_) { }
|
---|
[16233] | 57 |
|
---|
[16405] | 58 | public event EventHandler ProblemDefinitionChanged;
|
---|
[16233] | 59 |
|
---|
[16582] | 60 | public string Filename { get; set; }
|
---|
| 61 |
|
---|
| 62 | public ILinearProblemDefinition ProblemDefinition {
|
---|
| 63 | get => problemDefinitionParam.Value;
|
---|
[16405] | 64 | set {
|
---|
[16582] | 65 | if (problemDefinitionParam.Value == value)
|
---|
[16405] | 66 | return;
|
---|
[16582] | 67 | problemDefinitionParam.Value = value;
|
---|
[16405] | 68 | ProblemDefinitionChanged?.Invoke(this, EventArgs.Empty);
|
---|
| 69 | }
|
---|
[16172] | 70 | }
|
---|
| 71 |
|
---|
[16582] | 72 | public IValueParameter<ILinearProblemDefinition> ProblemDefinitionParameter => problemDefinitionParam;
|
---|
[16233] | 73 |
|
---|
[16582] | 74 | public override IDeepCloneable Clone(Cloner cloner) => new LinearProblem(this, cloner);
|
---|
| 75 |
|
---|
| 76 | public override void CollectParameterValues(IDictionary<string, IItem> values) {
|
---|
| 77 | base.CollectParameterValues(values);
|
---|
| 78 |
|
---|
| 79 | if (ProblemDefinition == null) return;
|
---|
| 80 |
|
---|
| 81 | values.Add("Model Type", new StringValue(
|
---|
| 82 | (ProblemDefinition.GetType().GetCustomAttributes().Single(a => a is ItemAttribute) as ItemAttribute).Name));
|
---|
| 83 |
|
---|
| 84 | if (ProblemDefinition is ProgrammableLinearProblemDefinition model) {
|
---|
| 85 | values.Add("Model Name", new StringValue(model.Name));
|
---|
| 86 | }
|
---|
| 87 | }
|
---|
| 88 |
|
---|
| 89 | public void ExportModel(string fileName) {
|
---|
| 90 | if (string.IsNullOrWhiteSpace(fileName))
|
---|
| 91 | throw new ArgumentNullException(nameof(fileName));
|
---|
| 92 | if (ProblemDefinition == null)
|
---|
| 93 | throw new ArgumentNullException(nameof(ProblemDefinition));
|
---|
| 94 |
|
---|
| 95 | var fileInfo = new FileInfo(fileName);
|
---|
| 96 |
|
---|
| 97 | if (!fileInfo.Directory?.Exists ?? false) {
|
---|
| 98 | Directory.CreateDirectory(fileInfo.Directory.FullName);
|
---|
| 99 | }
|
---|
| 100 |
|
---|
| 101 | var solver = new Solver(ProblemDefinition.ItemName, Solver.OptimizationProblemType.CbcMixedIntegerProgramming);
|
---|
| 102 |
|
---|
| 103 | ProblemDefinition.BuildModel(solver);
|
---|
| 104 |
|
---|
| 105 | var exportSuccessful = false;
|
---|
| 106 | switch (fileInfo.Extension) {
|
---|
| 107 | case ".lp":
|
---|
| 108 | var lpFormat = solver.ExportModelAsLpFormat(false);
|
---|
| 109 | if (!string.IsNullOrEmpty(lpFormat)) {
|
---|
| 110 | File.WriteAllText(fileName, lpFormat);
|
---|
| 111 | exportSuccessful = true;
|
---|
| 112 | }
|
---|
| 113 | break;
|
---|
| 114 |
|
---|
| 115 | case ".mps":
|
---|
| 116 | var mpsFormat = solver.ExportModelAsMpsFormat(false, false);
|
---|
| 117 | if (!string.IsNullOrEmpty(mpsFormat)) {
|
---|
| 118 | File.WriteAllText(fileName, mpsFormat);
|
---|
| 119 | exportSuccessful = true;
|
---|
| 120 | }
|
---|
| 121 | break;
|
---|
| 122 |
|
---|
| 123 | case ".prototxt":
|
---|
| 124 | exportSuccessful = solver.ExportModelAsProtoFormat(fileName,
|
---|
| 125 | (Google.OrTools.LinearSolver.ProtoWriteFormat)ProtoWriteFormat.ProtoText);
|
---|
| 126 | break;
|
---|
| 127 |
|
---|
| 128 | case ".bin": // remove file extension as it is added by OR-Tools
|
---|
| 129 | fileName = Path.ChangeExtension(fileName, null);
|
---|
| 130 | exportSuccessful = solver.ExportModelAsProtoFormat(fileName,
|
---|
| 131 | (Google.OrTools.LinearSolver.ProtoWriteFormat)ProtoWriteFormat.ProtoBinary);
|
---|
| 132 | break;
|
---|
| 133 |
|
---|
| 134 | default:
|
---|
| 135 | throw new NotSupportedException($"File format {fileInfo.Extension} to export model is not supported.");
|
---|
| 136 | }
|
---|
| 137 |
|
---|
| 138 | if (!exportSuccessful)
|
---|
[16910] | 139 | throw new InvalidOperationException($"Model could not be exported. " +
|
---|
| 140 | $"For details, see the log files in '{LinearSolver.LogDirectory}'.");
|
---|
[16582] | 141 | }
|
---|
| 142 |
|
---|
[16172] | 143 | [StorableHook(HookType.AfterDeserialization)]
|
---|
| 144 | private void AfterDeserialization() {
|
---|
| 145 | }
|
---|
| 146 | }
|
---|
[16288] | 147 | }
|
---|