LotSizingDataModel.Solver.Cplex 2.0.1
IBM ILOG CPLEX adapter and installation discovery integration.
Loading...
Searching...
No Matches
CplexInstallationLocator.cs
Go to the documentation of this file.
1using System;
2using System.Collections.Generic;
3using System.IO;
4
6
7/// <summary>
8/// Describes and discovers supported IBM ILOG CPLEX
9/// installations.
10/// </summary>
11public static class CplexInstallationLocator
12{
13 private static readonly IReadOnlyList<CplexInstallationDefinition>
14 Definitions =
15 [
16 new(
17 "22.2",
18 [
19 "CPLEX_STUDIO_DIR222",
20 "CPLEX_STUDIO_DIR2220"
21 ],
22 [
23 "CPLEX_Studio222",
24 "CPLEX_Studio2220"
25 ]),
26 new(
27 "22.1.2",
28 ["CPLEX_STUDIO_DIR2212"],
29 ["CPLEX_Studio2212"]),
30 new(
31 "22.1.1",
32 ["CPLEX_STUDIO_DIR2211"],
33 ["CPLEX_Studio2211"]),
34 new(
35 "22.1",
36 ["CPLEX_STUDIO_DIR221"],
37 ["CPLEX_Studio221"]),
38 new(
39 "20.1",
40 ["CPLEX_STUDIO_DIR201"],
41 ["CPLEX_Studio201"])
42 ];
43
44 /// <summary>
45 /// Gets the supported installation definitions, ordered
46 /// from newest to oldest.
47 /// </summary>
48 public static IReadOnlyList<CplexInstallationDefinition>
50 Definitions;
51
52 /// <summary>
53 /// Finds the newest compatible CPLEX installation visible
54 /// to the current process.
55 /// </summary>
56 /// <returns>
57 /// Discovery result containing the selected installation and
58 /// diagnostic messages.
59 /// </returns>
61 {
62 var diagnostics =
63 new List<string>();
64
65 foreach (CplexInstallationDefinition definition in Definitions)
66 {
67 foreach (string variableName in definition.EnvironmentVariables)
68 {
69 string? root =
70 Environment.GetEnvironmentVariable(
71 variableName);
72
73 if (TryResolve(
74 definition.Version,
75 root,
76 variableName,
77 out CplexInstallationInfo? info))
78 {
80 info,
81 diagnostics);
82 }
83
84 if (!string.IsNullOrWhiteSpace(root))
85 {
86 diagnostics.Add(
87 $"Environment variable '{variableName}' points " +
88 $"to '{root}', but the required CPLEX .NET " +
89 "assemblies were not found there.");
90 }
91 }
92
93 if (!OperatingSystem.IsWindows())
94 {
95 continue;
96 }
97
98 string programFiles =
99 Environment.GetFolderPath(
100 Environment.SpecialFolder.ProgramFiles);
101
102 foreach (string folderName in definition.WindowsFolderNames)
103 {
104 string root =
105 Path.Combine(
106 programFiles,
107 "IBM",
108 "ILOG",
109 folderName);
110
111 if (TryResolve(
112 definition.Version,
113 root,
114 "default Windows installation",
115 out CplexInstallationInfo? info))
116 {
118 info,
119 diagnostics);
120 }
121 }
122 }
123
124 diagnostics.Add(
125 "No compatible CPLEX installation was found. " +
126 "Supported automatic discovery currently covers " +
127 "22.2.x, 22.1.2, 22.1.1, 22.1 and 20.1.");
128
130 null,
131 diagnostics);
132 }
133
134 private static bool TryResolve(
135 string version,
136 string? rootDirectory,
137 string source,
138 out CplexInstallationInfo? installation)
139 {
140 installation =
141 null;
142
143 if (string.IsNullOrWhiteSpace(rootDirectory))
144 {
145 return false;
146 }
147
148 string root =
149 Environment.ExpandEnvironmentVariables(
150 rootDirectory.Trim());
151
152 string managedDirectory =
153 Path.Combine(
154 root,
155 "cplex",
156 "bin",
157 "x64_win64");
158
159 string concertAssembly =
160 Path.Combine(
161 managedDirectory,
162 "ILOG.Concert.dll");
163
164 string cplexAssembly =
165 Path.Combine(
166 managedDirectory,
167 "ILOG.CPLEX.dll");
168
169 if (!File.Exists(concertAssembly) ||
170 !File.Exists(cplexAssembly))
171 {
172 return false;
173 }
174
175 installation =
176 new CplexInstallationInfo(
177 version,
178 root,
179 managedDirectory,
180 source);
181
182 return true;
183 }
184}
IReadOnlyList< string > WindowsFolderNames
Gets common Windows installation folder names.
IReadOnlyList< string > EnvironmentVariables
Gets accepted environment-variable names.
Represents the result of compatible CPLEX installation discovery.
Represents one compatible CPLEX installation discovered on the current machine.
Describes and discovers supported IBM ILOG CPLEX installations.
static IReadOnlyList< CplexInstallationDefinition > SupportedInstallations
Gets the supported installation definitions, ordered from newest to oldest.
static CplexInstallationDiscoveryResult Discover()
Finds the newest compatible CPLEX installation visible to the current process.