Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2994-AutoDiffForIntervals/HeuristicLab.ExactOptimization/3.3/LinearProgramming/Problems/FileBasedLinearProblemDefinition.cs

Last change on this file was 17209, checked in by gkronber, 5 years ago

#2994: merged r17132:17198 from trunk to branch

File size: 3.7 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 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
22using System.Drawing;
23using System.IO;
24using Google.OrTools.LinearSolver;
25using HEAL.Attic;
26using HeuristicLab.Common;
27using HeuristicLab.Common.Resources;
28using HeuristicLab.Core;
29using HeuristicLab.Data;
30using HeuristicLab.Optimization;
31using HeuristicLab.Parameters;
32
33namespace 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}
Note: See TracBrowser for help on using the repository browser.