1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2018 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 |
|
---|
22 | using System.Drawing;
|
---|
23 | using System.IO;
|
---|
24 | using Google.OrTools.LinearSolver;
|
---|
25 | using HEAL.Attic;
|
---|
26 | using HeuristicLab.Common;
|
---|
27 | using HeuristicLab.Common.Resources;
|
---|
28 | using HeuristicLab.Core;
|
---|
29 | using HeuristicLab.Data;
|
---|
30 | using HeuristicLab.Optimization;
|
---|
31 | using HeuristicLab.Parameters;
|
---|
32 |
|
---|
33 | namespace HeuristicLab.ExactOptimization.LinearProgramming {
|
---|
34 |
|
---|
35 | [Item("Linear Problem Definition File (MPS, OR-Tools Protocol Buffers Files)", "File that defines the model for linear/mixed integer programming problem.")]
|
---|
36 | [StorableType("8779D26D-37DB-4595-A4E9-C7F29B870802")]
|
---|
37 | public sealed class FileBasedLinearProblemDefinition : ParameterizedNamedItem, ILinearProblemDefinition {
|
---|
38 |
|
---|
39 | [Storable]
|
---|
40 | private readonly IFixedValueParameter<FileValue> fileNameParam;
|
---|
41 |
|
---|
42 | [Storable]
|
---|
43 | private byte[] fileContent;
|
---|
44 |
|
---|
45 | public FileBasedLinearProblemDefinition() {
|
---|
46 | Parameters.Add(fileNameParam = new FixedValueParameter<FileValue>(nameof(FileName), new FileValue()));
|
---|
47 | fileNameParam.Value.FileDialogFilter =
|
---|
48 | "All Supported Files (*.mps;*.bin;*.prototxt)|*.mps;*.bin;*.prototxt|" +
|
---|
49 | "Mathematical Programming System Files (*.mps)|*.mps|" +
|
---|
50 | "Google OR-Tools Protocol Buffers Files (*.bin;*.prototxt)|*.bin;*.prototxt|" +
|
---|
51 | "All Files (*.*)|*.*";
|
---|
52 | fileNameParam.Value.StringValue.ValueChanged += (o, e) => {
|
---|
53 | if (File.Exists(FileName)) {
|
---|
54 | fileContent = File.ReadAllBytes(FileName);
|
---|
55 | }
|
---|
56 | };
|
---|
57 | }
|
---|
58 |
|
---|
59 | private FileBasedLinearProblemDefinition(FileBasedLinearProblemDefinition original, Cloner cloner)
|
---|
60 | : base(original, cloner) {
|
---|
61 | fileNameParam = cloner.Clone(original.fileNameParam);
|
---|
62 | }
|
---|
63 |
|
---|
64 | [StorableConstructor]
|
---|
65 | private FileBasedLinearProblemDefinition(StorableConstructorFlag _) : base(_) { }
|
---|
66 |
|
---|
67 | public new static Image StaticItemImage => VSImageLibrary.File;
|
---|
68 |
|
---|
69 | public string FileName {
|
---|
70 | get => fileNameParam.Value.Value;
|
---|
71 | set => fileNameParam.Value.Value = value;
|
---|
72 | }
|
---|
73 |
|
---|
74 | public IFixedValueParameter<FileValue> FileNameParam => fileNameParam;
|
---|
75 |
|
---|
76 | public void Analyze(Solver solver, ResultCollection results) {
|
---|
77 | }
|
---|
78 |
|
---|
79 | public void BuildModel(Solver solver) {
|
---|
80 | var fileInfo = new FileInfo(FileName);
|
---|
81 | var tempFileName = Path.GetTempFileName();
|
---|
82 |
|
---|
83 | try {
|
---|
84 | File.WriteAllBytes(tempFileName, fileContent);
|
---|
85 |
|
---|
86 | var status = (SolverResponseStatus)(fileInfo.Extension == ".mps"
|
---|
87 | ? solver.ImportModelFromMpsFormat(tempFileName)
|
---|
88 | : solver.ImportModelFromProtoFormat(tempFileName));
|
---|
89 |
|
---|
90 | if (status == SolverResponseStatus.Abnormal)
|
---|
91 | throw new FileFormatException($"'{FileName}' is not a valid MPS or Google OR-Tools Protocol Buffers file.");
|
---|
92 | } finally {
|
---|
93 | File.Delete(tempFileName);
|
---|
94 | }
|
---|
95 | }
|
---|
96 |
|
---|
97 | public override IDeepCloneable Clone(Cloner cloner) => new FileBasedLinearProblemDefinition(this, cloner);
|
---|
98 | }
|
---|
99 | }
|
---|