Skip to content

Commit 454d318

Browse files
committed
token + sol from one run to the other
token are taken only once per cplex solution from one run usedin next one
1 parent 44af2d8 commit 454d318

6 files changed

Lines changed: 164 additions & 15 deletions

File tree

src/MixBlend.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,9 @@ public static void main( String[] args ) {
119119
System.out.println("Elements:");
120120
for(int j = 0; j < _nbElements; j++)
121121
System.out.println("(" + j + ") " + eVals[j]);
122+
123+
// Solve again to check the previous solution is taken into account
124+
cplex.solve();
122125
}
123126
}
124127
catch (IloException exc) {

src/Transport.java

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/* --------------------------------------------------------------------------
2+
* File: Transport.java
3+
* Version 12.10.0
4+
* --------------------------------------------------------------------------
5+
* Licensed Materials - Property of IBM
6+
* 5725-A06 5725-A29 5724-Y48 5724-Y49 5724-Y54 5724-Y55 5655-Y21
7+
* Copyright IBM Corporation 2001, 2019. All Rights Reserved.
8+
*
9+
* US Government Users Restricted Rights - Use, duplication or
10+
* disclosure restricted by GSA ADP Schedule Contract with
11+
* IBM Corp.
12+
* --------------------------------------------------------------------------
13+
*/
14+
15+
import com.ibm.wmlconnector.WMLConnector;
16+
import ilog.cplex.*;
17+
import ilog.concert.*;
18+
19+
public class Transport {
20+
public static void main(String[] args) {
21+
if ( args.length < 1 ) {
22+
System.err.println("Usage: java Transport <type>");
23+
System.err.println(" type = 0 -> convex piecewise linear model");
24+
System.err.println(" type = 1 -> concave piecewise linear model");
25+
//System.exit(-1);
26+
args = new String[1];
27+
args[0] = "0";
28+
29+
}
30+
31+
//try (IloCplex cplex = new IloCplex()) {
32+
try (IloCplex cplex = new CplexWithWML(new MyProdBetaV4Credentials(), WMLConnector.ModelType.CPLEX_12_10, WMLConnector.TShirtSize.M)) {
33+
final int nbDemand = 4;
34+
final int nbSupply = 3;
35+
double[] supply = {1000.0, 850.0, 1250.0};
36+
double[] demand = {900.0, 1200.0, 600.0, 400.0};
37+
38+
IloNumVar[][] x = new IloNumVar[nbSupply][];
39+
IloNumVar[][] y = new IloNumVar[nbSupply][];
40+
41+
for (int i = 0; i < nbSupply; i++) {
42+
x[i] = cplex.numVarArray(nbDemand, 0., Double.MAX_VALUE);
43+
y[i] = cplex.numVarArray(nbDemand, 0., Double.MAX_VALUE);
44+
}
45+
46+
for (int i = 0; i < nbSupply; i++) // supply must meet demand
47+
cplex.addEq(cplex.sum(x[i]), supply[i]);
48+
49+
for (int j = 0; j < nbDemand; j++) { // demand must meet supply
50+
IloLinearNumExpr v = cplex.linearNumExpr();
51+
for(int i = 0; i < nbSupply; i++)
52+
v.addTerm(1., x[i][j]);
53+
cplex.addEq(v, demand[j]);
54+
}
55+
56+
double[] points;
57+
double[] slopes;
58+
if ( args[0].charAt(0) == '0' ) { // convex case
59+
points = new double[] {200.0, 400.0};
60+
slopes = new double[] { 30.0, 80.0, 130.0};
61+
}
62+
else { // concave case
63+
points = new double[] {200.0, 400.0};
64+
slopes = new double[] {120.0, 80.0, 50.0};
65+
}
66+
for (int i = 0; i < nbSupply; ++i) {
67+
for (int j = 0; j < nbDemand; ++j) {
68+
cplex.addEq(y[i][j],
69+
cplex.piecewiseLinear(x[i][j],
70+
points, slopes, 0.0, 0.0));
71+
}
72+
}
73+
74+
IloLinearNumExpr expr = cplex.linearNumExpr();
75+
for (int i = 0; i < nbSupply; ++i) {
76+
for (int j = 0; j < nbDemand; ++j) {
77+
expr.addTerm(y[i][j], 1.);
78+
}
79+
}
80+
81+
cplex.addMinimize(expr);
82+
83+
if ( cplex.solve() ) {
84+
System.out.println("Solution status: " + cplex.getStatus());
85+
System.out.println(" - Solution: ");
86+
for (int i = 0; i < nbSupply; ++i) {
87+
System.out.print(" " + i + ": ");
88+
for (int j = 0; j < nbDemand; ++j)
89+
System.out.print("" + cplex.getValue(x[i][j]) + "\t");
90+
System.out.println();
91+
}
92+
System.out.println(" Cost = " + cplex.getObjValue());
93+
}
94+
}
95+
catch (IloException exc) {
96+
System.out.println(exc);
97+
System.exit(-1);
98+
}
99+
}
100+
}

src/com/ibm/wmlconnector/WMLConnector.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ public String toString(){
8080
public String getModelHref(String modelId, boolean displayModel);
8181
public String deployModel(String deployName, String model_id, TShirtSize size, int nodes);
8282

83+
public JSONObject createDataFromString(String id, String text);
8384
public JSONArray createDataFromJSONPayload(String fileName);
8485
public JSONObject createDataFromCSV(String id, String fileName);
8586
public JSONObject createDataFromFile(String id, String fileName);

src/com/ibm/wmlconnector/impl/WMLConnectorImpl.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,25 @@ public JSONObject createDataFromCSV(String id, String fileName) {
308308
return data;
309309
}
310310

311+
@Override
312+
public JSONObject createDataFromString(String id, String text) {
313+
byte[] bytes = text.getBytes();
314+
byte[] encoded = Base64.getEncoder().encode(bytes);
315+
316+
JSONObject data = new JSONObject();
317+
data.put("id", id);
318+
319+
JSONArray fields = new JSONArray();
320+
fields.put("___TEXT___");
321+
data.put("fields", fields);
322+
323+
JSONArray values = new JSONArray();
324+
values.put(new JSONArray().put(new String(encoded)));
325+
data.put("values", values);
326+
327+
return data;
328+
}
329+
311330
@Override
312331
public JSONObject createDataFromFile(String id, String fileName) {
313332

src/ilog/cplex/CplexWithWML.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,12 @@ public class CplexWithWML extends ExternalCplex {
3636
private static final long serialVersionUID = 1;
3737

3838
protected Credentials wml_credentials;
39+
protected WMLConnectorImpl wml;
3940
protected WMLConnector.ModelType wml_type;
4041
protected WMLConnector.TShirtSize wml_size;
42+
protected int wml_nodes = 1;
4143
protected String wml_name;
44+
protected String deployment_id = null;
4245
public CplexWithWML(Credentials credentials) throws IloException {
4346
this(NamingStrategy.MAKE_NAMES, credentials, WMLConnector.ModelType.CPLEX_12_10, WMLConnector.TShirtSize.S, "CPLEXWithWML");
4447
}
@@ -51,6 +54,7 @@ public CplexWithWML(Credentials credentials, WMLConnector.ModelType type, WMLCo
5154
public CplexWithWML(NamingStrategy namingStrategy, Credentials credentials, WMLConnector.ModelType type, WMLConnector.TShirtSize size, String name) throws IloException {
5255
super(namingStrategy);
5356
wml_credentials = credentials;
57+
wml = new WMLConnectorImpl(wml_credentials);
5458
this.wml_type = type;
5559
this.wml_size = size;
5660
wml_name = name;
@@ -69,25 +73,26 @@ protected Solution externalSolve(Set<String> knownVariables, Set<String> knownCo
6973
final File parameters = File.createTempFile("cpx", ".prm");
7074
writeParam(parameters.getAbsolutePath());
7175
LOGGER.fine("Exported .prm file to " + parameters.getAbsolutePath());
76+
boolean hasStartSolution = false;
7277
try {
7378

74-
int nodes = 1;
75-
WMLConnectorImpl wml = new WMLConnectorImpl(wml_credentials);
76-
77-
String deployment_id = wml.getDeploymentIdByName(wml_name);
79+
if (deployment_id == null)
80+
deployment_id = wml.getDeploymentIdByName(wml_name);
7881
if (deployment_id == null) {
7982
LOGGER.info("Creating model and deployment");
8083
LOGGER.fine("Create Empty " + wml_type + " Model");
8184
String model_id = wml.createNewModel(wml_name, wml_type, null);
8285
LOGGER.fine("model_id = " + model_id);
8386

84-
deployment_id = wml.deployModel(wml_name, model_id, wml_size, nodes);
87+
deployment_id = wml.deployModel(wml_name, model_id, wml_size, wml_nodes);
8588
}
8689
LOGGER.fine("deployment_id = " + deployment_id);
8790

8891
JSONArray input_data = new JSONArray();
8992
input_data.put(wml.createDataFromFile(wml_name+".sav.gz", model.getAbsolutePath()));
9093
input_data.put(wml.createDataFromFile(wml_name+".prm", parameters.getAbsolutePath()));
94+
if (isMIP() && result != null && result.hasSolution()) // TODO only for MIP ?
95+
input_data.put(wml.createDataFromString(wml_name+".sol", result.getSolution()));
9196
WMLJob job = wml.createAndRunJob(deployment_id, input_data, null, null, null);
9297

9398
switch (job.getSolveStatus()) {

src/ilog/cplex/ExternalCplex.java

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,8 @@
1313
// This class is in package ilog.cplex so that we can call some undocumented functions
1414
package ilog.cplex;
1515

16-
import java.io.File;
17-
import java.io.FileInputStream;
18-
import java.io.IOException;
19-
import java.io.InputStream;
16+
import java.io.*;
17+
import java.nio.charset.StandardCharsets;
2018
import java.util.HashMap;
2119
import java.util.Iterator;
2220
import java.util.Map;
@@ -40,6 +38,7 @@
4038
import org.w3c.dom.Node;
4139
import org.w3c.dom.NodeList;
4240
import org.xml.sax.SAXException;
41+
import sun.misc.IOUtils;
4342

4443
/** Base class for external solves.
4544
* @author daniel.junglas@de.ibm.com
@@ -143,6 +142,9 @@ private void addExpr(HashMap<String,IloNumVar> name2var, HashMap<IloNumVar,Strin
143142
*
144143
*/
145144
protected static class Solution {
145+
146+
private String solution = null;
147+
146148
boolean feasible = false;
147149
/** Map variable names to values. */
148150
public HashMap<String,Double> name2val = new HashMap<String,Double>();
@@ -160,7 +162,7 @@ protected static class Solution {
160162
public boolean pfeas = false;
161163
/** Dual feasible? */
162164
public boolean dfeas = false;
163-
165+
164166
public Solution() {}
165167
public Solution(File solutionXml, Set<String> knownVariables, Set<String> knownConstraints) throws IOException {
166168
this();
@@ -170,8 +172,17 @@ public Solution(InputStream solutionXml, Set<String> knownVariables, Set<String>
170172
this();
171173
parse(solutionXml, knownVariables, knownConstraints);
172174
}
173-
175+
176+
public boolean hasSolution() {
177+
return solution != null;
178+
}
179+
180+
public String getSolution() {
181+
return solution;
182+
}
183+
174184
public void reset() {
185+
solution = null;
175186
feasible = false;
176187
name2val = new HashMap<String, Double>();
177188
var2val = new HashMap<IloNumVar, Double>();
@@ -189,6 +200,16 @@ public void parse(File solutionXml, Set<String> knownVariables, Set<String> know
189200
try (FileInputStream fis = new FileInputStream(solutionXml)) {
190201
parse(fis, knownVariables, knownConstraints);
191202
}
203+
try (FileInputStream fis = new FileInputStream(solutionXml)) {
204+
ByteArrayOutputStream result = new ByteArrayOutputStream();
205+
byte[] buffer = new byte[1024];
206+
int length;
207+
while ((length = fis.read(buffer)) != -1) {
208+
result.write(buffer, 0, length);
209+
}
210+
// StandardCharsets.UTF_8.name() > JDK 7
211+
solution = result.toString("UTF-8");
212+
}
192213
}
193214

194215
private enum ParserState {
@@ -220,8 +241,9 @@ private static String[] getAttributes(XMLStreamReader reader, String... attrs) {
220241
* @param knownVariables The names of the variables for which values should be extracted from <code>solutionXml</code>.
221242
* @throws IOException If an input/output error occurs or mandatory solution information is missing.
222243
*/
223-
public void parse(InputStream solutionXml, Set<String> knownVariables, Set<String> knownConstraints) throws IOException {
244+
private void parse(InputStream solutionXml, Set<String> knownVariables, Set<String> knownConstraints) throws IOException {
224245
reset();
246+
225247
boolean ok = false;
226248
try {
227249
final String MALFORMED_XML = "Malformed XML";
@@ -438,11 +460,10 @@ public void parse2(InputStream solutionXml, Set<String> knownVariables) throws I
438460
}
439461
}
440462

441-
private Solution result = null;
463+
protected Solution result = null;
442464

443465
public boolean solve() throws IloException {
444-
result = null;
445-
466+
446467
HashMap<IloNumVar,String> oldVarNames = null;
447468
if (namingStrategy == NamingStrategy.MAKE_NAMES)
448469
oldVarNames = new HashMap<IloNumVar, String>();

0 commit comments

Comments
 (0)