Skip to content

Commit 44af2d8

Browse files
committed
added support for CPLEX parameters
1 parent 4a3c472 commit 44af2d8

6 files changed

Lines changed: 87 additions & 55 deletions

File tree

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,23 @@ IloCplex cplex = new CplexWithWML( new MyProdBetaV4Credentials());
6767

6868
The new CPLEX class will behave exactly like the original one (see [supported API and limitations](#Supported-API-and-limitations)), you can call solve, get the variables values, etc
6969

70+
You can use extended CplexWithWML constructors to set:
71+
* the type of model to use (to select a specific CPLEX versions)
72+
* the size of runtime to use (Small, Medium or Large)
73+
* the name of the deployment to use (and reuse)
74+
7075
### Supported API and limitations
7176

7277
#### CPLEX
7378

79+
The CPLEX parameters are exported and sent to WML.
80+
Some parameters can be overridden due to the WML settings, in particular:
81+
```
82+
CPXPARAM_Threads 1 # will be limited according to the runtime size
83+
CPXPARAM_Output_CloneLog -1
84+
CPXPARAM_Read_APIEncoding "utf-8"
85+
```
86+
7487
##### Supported API
7588
```
7689
public boolean solve() throws IloException

src/Diet.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
* See the usage message for more details.
3939
*/
4040

41+
import com.ibm.wmlconnector.WMLConnector;
4142
import ilog.concert.*;
4243
import ilog.cplex.CplexWithWML;
4344
import ilog.cplex.IloCplex;
@@ -291,7 +292,7 @@ else if (args[i].equals("-m"))
291292
Data data = new Data(filename);
292293
final int nFoods = data.nFoods;
293294
//IloCplex cplex = new IloCplex();
294-
IloCplex cplex = new CplexWithWML(new MyProdBetaV4Credentials());
295+
IloCplex cplex = new CplexWithWML(new MyProdBetaV4Credentials(), WMLConnector.ModelType.CPLEX_12_10, WMLConnector.TShirtSize.M);
295296
try {
296297
// Build model
297298
IloNumVar[] buy = new IloNumVar[nFoods];
@@ -317,6 +318,8 @@ else if (args[i].equals("-m"))
317318
// Write a copy of the problem to a file.
318319

319320
cplex.exportModel("Diet.java.lp");
321+
cplex.setParam(IloCplex.Param.Preprocessing.Presolve, false);
322+
cplex.setParam(IloCplex.Param.Threads, 2);
320323

321324
// Solve model
322325

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

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import com.ibm.wmlconnector.COSConnector;
44
import com.ibm.wmlconnector.Credentials;
5-
import org.json.JSONArray;
65
import org.json.JSONObject;
76

87
import java.util.HashMap;
@@ -13,18 +12,18 @@ public class COSConnectorImpl extends ConnectorImpl implements COSConnector {
1312
private static final Logger LOGGER = Logger.getLogger(COSConnectorImpl.class.getName());
1413

1514

16-
String url;
17-
String bucket;
15+
protected String cos_url;
16+
protected String cos_bucket;
1817

19-
String access_key_id;
20-
String secret_access_key;
18+
protected String cos_access_key_id;
19+
protected String cos_secret_access_key;
2120

2221
public COSConnectorImpl(Credentials credentials) {
23-
super(credentials.IAM_URL, credentials.COS_APIKEY);
24-
this.url = credentials.COS_ENDPOINT;
25-
this.bucket = credentials.COS_BUCKET;
26-
this.access_key_id = credentials.COS_ACCESS_KEY_ID;
27-
this.secret_access_key = credentials.COS_SECRET_ACCESS_KEY;
22+
super(credentials, true);
23+
this.cos_url = credentials.COS_ENDPOINT;
24+
this.cos_bucket = credentials.COS_BUCKET;
25+
this.cos_access_key_id = credentials.COS_ACCESS_KEY_ID;
26+
this.cos_secret_access_key = credentials.COS_SECRET_ACCESS_KEY;
2827
lookupBearerToken();
2928
}
3029

@@ -35,7 +34,7 @@ public void putFile(String fileName, String filePath) {
3534
headers.put("Authorization", "bearer " + bearerToken);
3635
headers.put("Content-Type", "text/plain");
3736

38-
doPut(url + "/" + bucket + "/" + fileName, headers, getFileContent(filePath));
37+
doPut(cos_url + "/" + cos_bucket + "/" + fileName, headers, getFileContent(filePath));
3938
}
4039

4140
@Override
@@ -45,7 +44,7 @@ public void putBinaryFile(String fileName, String filePath) {
4544

4645
byte[] bytes = getBinaryFileContent(filePath);
4746

48-
doPut(url + "/" + bucket + "/" + fileName, headers, bytes);
47+
doPut(cos_url + "/" + cos_bucket + "/" + fileName, headers, bytes);
4948

5049
}
5150

@@ -57,7 +56,7 @@ public String getFile(String fileName) {
5756
headers.put("Content-Type", "text/plain");
5857

5958

60-
String res = doGet(url + "/" + bucket + "/" + fileName, headers);
59+
String res = doGet(cos_url + "/" + cos_bucket + "/" + fileName, headers);
6160

6261
return res;
6362
}
@@ -68,12 +67,12 @@ public JSONObject getDataReferences(String id) {
6867
"\"id\": \"" + id + "\",\n" +
6968
"\"type\": \"s3\",\n" +
7069
"\"connection\": {\n" +
71-
"\"endpoint_url\": \"" + url + "\",\n" +
72-
"\"access_key_id\": \"" + access_key_id + "\",\n" +
73-
"\"secret_access_key\": \"" + secret_access_key + "\"\n" +
70+
"\"endpoint_url\": \"" + cos_url + "\",\n" +
71+
"\"access_key_id\": \"" + cos_access_key_id + "\",\n" +
72+
"\"secret_access_key\": \"" + cos_secret_access_key + "\"\n" +
7473
"}, \n" +
7574
"\"location\": {\n" +
76-
"\"bucket\": \"" + bucket + "\",\n" +
75+
"\"bucket\": \"" + cos_bucket + "\",\n" +
7776
"\"path\": \"" + id + "\"\n" +
7877
"}\n" +
7978
"}\n";

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

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import java.net.URL;
1111
import java.util.*;
1212

13+
import com.ibm.wmlconnector.Credentials;
1314
import org.json.JSONException;
1415
import org.json.JSONObject;
1516

@@ -20,14 +21,19 @@ public abstract class ConnectorImpl {
2021

2122
private static final int IAM_TIMEOUT = 3600;
2223

23-
String iam_url;
24-
String apikey;
25-
String bearerToken;
26-
long bearerTokenTime;
24+
protected Credentials credentials;
25+
protected boolean isCOS;
26+
protected String bearerToken;
27+
protected long bearerTokenTime;
2728

28-
ConnectorImpl(String url, String apikey) {
29-
this.iam_url = url;
30-
this.apikey = apikey;
29+
ConnectorImpl(Credentials credentials, boolean isCOS) {
30+
this.credentials = credentials;
31+
}
32+
private String getApiKey() {
33+
if (isCOS)
34+
return credentials.COS_APIKEY;
35+
else
36+
return credentials.USE_V4_FINAL ? credentials.USER_APIKEY : credentials.WML_APIKEY;
3137
}
3238

3339
public String getBearerToken() {
@@ -42,7 +48,7 @@ public void lookupBearerToken() {
4248
headers.put("Authorization", "Basic Yng6Yng=");
4349
headers.put("Content-Type", "application/x-www-form-urlencoded");
4450

45-
String res = doPost(iam_url, headers, "apikey="+apikey+"&grant_type=urn%3Aibm%3Aparams%3Aoauth%3Agrant-type%3Aapikey&response_type=cloud_iam");
51+
String res = doPost(credentials.IAM_URL, headers, "apikey="+getApiKey()+"&grant_type=urn%3Aibm%3Aparams%3Aoauth%3Agrant-type%3Aapikey&response_type=cloud_iam");
4652

4753
try {
4854
JSONObject json = new JSONObject(res);

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

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,12 @@ public class WMLConnectorImpl extends ConnectorImpl implements WMLConnector {
2121

2222

2323

24-
Credentials credentials;
25-
String wml_url;
26-
String instance_id;
27-
28-
String api_url = null;
24+
protected String wml_url;
25+
protected String wml_instance_id;
26+
protected String api_url = null;
2927

3028
public WMLConnectorImpl(Credentials credentials) {
31-
super(credentials.IAM_URL, credentials.USE_V4_FINAL ? credentials.USER_APIKEY : credentials.WML_APIKEY);
32-
this.credentials = credentials;
29+
super(credentials, false);
3330
this.wml_url = credentials.WML_URL;
3431
if (credentials.USE_V4_FINAL) {
3532
api_url = credentials.API_URL;
@@ -38,7 +35,7 @@ public WMLConnectorImpl(Credentials credentials) {
3835
LOGGER.info("WMLConnector using V4 BETA APIs");
3936
}
4037

41-
this.instance_id = credentials.WML_INSTANCE_ID;
38+
this.wml_instance_id = credentials.WML_INSTANCE_ID;
4239
lookupBearerToken();
4340
}
4441

@@ -64,7 +61,7 @@ public void updateStatus() {
6461
HashMap<String, String> headers = new HashMap<String, String>();
6562
headers.put("Accept", "application/json");
6663
headers.put("Authorization", "bearer " + bearerToken);
67-
headers.put("ML-Instance-ID", instance_id);
64+
headers.put("ML-Instance-ID", wml_instance_id);
6865
headers.put("cache-control", "no-cache");
6966

7067
String url = "";
@@ -163,7 +160,7 @@ public void delete() {
163160
HashMap<String, String> headers = new HashMap<String, String>();
164161
headers.put("Accept", "application/json");
165162
headers.put("Authorization", "bearer " + bearerToken);
166-
headers.put("ML-Instance-ID", instance_id);
163+
headers.put("ML-Instance-ID", wml_instance_id);
167164
headers.put("cache-control", "no-cache");
168165

169166
doDelete(wml_url + "/v4/jobs/" + job_id + "?hard_delete=true", headers);
@@ -399,7 +396,7 @@ public WMLJob createJob(String deployment_id,
399396
HashMap<String, String> headers = new HashMap<String, String>();
400397
headers.put("Accept", "application/json");
401398
headers.put("Authorization", "bearer " + bearerToken);
402-
headers.put("ML-Instance-ID", instance_id);
399+
headers.put("ML-Instance-ID", wml_instance_id);
403400
headers.put("cache-control", "no-cache");
404401
headers.put("Content-Type", "application/json");
405402

@@ -500,7 +497,7 @@ public String createNewModel(String modelName, ModelType type, String modelAsset
500497
{
501498
HashMap<String, String> headers = new HashMap<String, String>();
502499
headers.put("Authorization", "bearer " + bearerToken);
503-
headers.put("ML-Instance-ID", instance_id);
500+
headers.put("ML-Instance-ID", wml_instance_id);
504501
headers.put("cache-control", "no-cache");
505502
headers.put("Content-Type", "application/json");
506503

@@ -531,7 +528,7 @@ public String createNewModel(String modelName, ModelType type, String modelAsset
531528
{
532529
HashMap<String, String> headers = new HashMap<String, String>();
533530
headers.put("Authorization", "bearer " + bearerToken);
534-
headers.put("ML-Instance-ID", instance_id);
531+
headers.put("ML-Instance-ID", wml_instance_id);
535532
headers.put("cache-control", "no-cache");
536533

537534

@@ -557,7 +554,7 @@ public String getModelHref(String modelId, boolean displayModel) {
557554

558555
HashMap<String, String> headers = new HashMap<String, String>();
559556
headers.put("Authorization", "bearer " + bearerToken);
560-
headers.put("ML-Instance-ID", instance_id);
557+
headers.put("ML-Instance-ID", wml_instance_id);
561558
headers.put("cache-control", "no-cache");
562559
headers.put("Content-Type", "application/json");
563560

@@ -574,7 +571,7 @@ public String deployModel(String deployName, String model_id, TShirtSize size, i
574571

575572
HashMap<String, String> headers = new HashMap<String, String>();
576573
headers.put("Authorization", "bearer " + bearerToken);
577-
headers.put("ML-Instance-ID", instance_id);
574+
headers.put("ML-Instance-ID", wml_instance_id);
578575
headers.put("cache-control", "no-cache");
579576
headers.put("Content-Type", "application/json");
580577

@@ -616,7 +613,7 @@ public void deleteDeployment(String deployment_id) {
616613

617614
HashMap<String, String> headers = new HashMap<String, String>();
618615
headers.put("Authorization", "bearer " + bearerToken);
619-
headers.put("ML-Instance-ID", instance_id);
616+
headers.put("ML-Instance-ID", wml_instance_id);
620617
headers.put("cache-control", "no-cache");
621618
headers.put("Content-Type", "application/json");
622619

@@ -662,7 +659,7 @@ public String createDeploymentSpace(String name) {
662659
HashMap<String, String> headers = new HashMap<String, String>();
663660
headers.put("Accept", "application/json");
664661
headers.put("Authorization", "bearer " + bearerToken);
665-
headers.put("ML-Instance-ID", instance_id);
662+
headers.put("ML-Instance-ID", wml_instance_id);
666663
headers.put("cache-control", "no-cache");
667664
headers.put("Content-Type", "application/json");
668665

@@ -713,7 +710,7 @@ public JSONObject getDeployments() {
713710
HashMap<String, String> headers = new HashMap<String, String>();
714711
headers.put("Accept", "application/json");
715712
headers.put("Authorization", "bearer " + bearerToken);
716-
headers.put("ML-Instance-ID", instance_id);
713+
headers.put("ML-Instance-ID", wml_instance_id);
717714
headers.put("cache-control", "no-cache");
718715

719716
String url = "";

src/ilog/cplex/CplexWithWML.java

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,24 @@ public class CplexWithWML extends ExternalCplex {
3535
private static final Logger LOGGER = Logger.getLogger(CplexWithWML.class.getName());
3636
private static final long serialVersionUID = 1;
3737

38-
Credentials wml_credentials;
39-
String wml_name;
40-
public CplexWithWML(Credentials credentials) throws IloException { this(NamingStrategy.MAKE_NAMES, credentials, "CPLEXWithWML"); }
41-
public CplexWithWML(Credentials credentials, String name) throws IloException { this(NamingStrategy.MAKE_NAMES, credentials, name); }
42-
public CplexWithWML(NamingStrategy namingStrategy, Credentials credentials, String name) throws IloException {
38+
protected Credentials wml_credentials;
39+
protected WMLConnector.ModelType wml_type;
40+
protected WMLConnector.TShirtSize wml_size;
41+
protected String wml_name;
42+
public CplexWithWML(Credentials credentials) throws IloException {
43+
this(NamingStrategy.MAKE_NAMES, credentials, WMLConnector.ModelType.CPLEX_12_10, WMLConnector.TShirtSize.S, "CPLEXWithWML");
44+
}
45+
public CplexWithWML(Credentials credentials, WMLConnector.ModelType type, WMLConnector.TShirtSize size) throws IloException {
46+
this(NamingStrategy.MAKE_NAMES, credentials, type, size, "CPLEXWithWML");
47+
}
48+
public CplexWithWML(Credentials credentials, WMLConnector.ModelType type, WMLConnector.TShirtSize size, String name) throws IloException {
49+
this(NamingStrategy.MAKE_NAMES, credentials, type, size, name);
50+
}
51+
public CplexWithWML(NamingStrategy namingStrategy, Credentials credentials, WMLConnector.ModelType type, WMLConnector.TShirtSize size, String name) throws IloException {
4352
super(namingStrategy);
4453
wml_credentials = credentials;
54+
this.wml_type = type;
55+
this.wml_size = size;
4556
wml_name = name;
4657
}
4758

@@ -54,26 +65,29 @@ protected Solution externalSolve(Set<String> knownVariables, Set<String> knownCo
5465
final File solution = new File(model.getAbsolutePath() + ".sol");
5566
solution.deleteOnExit();
5667
exportModel(model.getAbsolutePath());
57-
LOGGER.fine("Exported sav file.");
68+
LOGGER.fine("Exported .sav.gz file.");
69+
final File parameters = File.createTempFile("cpx", ".prm");
70+
writeParam(parameters.getAbsolutePath());
71+
LOGGER.fine("Exported .prm file to " + parameters.getAbsolutePath());
5872
try {
5973

60-
WMLConnector.ModelType type = WMLConnector.ModelType.CPLEX_12_9;
61-
WMLConnector.TShirtSize size = WMLConnector.TShirtSize.S;
6274
int nodes = 1;
6375
WMLConnectorImpl wml = new WMLConnectorImpl(wml_credentials);
6476

6577
String deployment_id = wml.getDeploymentIdByName(wml_name);
6678
if (deployment_id == null) {
67-
LOGGER.fine("Create Empty " + type + " Model");
68-
String model_id = wml.createNewModel(wml_name, type, null);
79+
LOGGER.info("Creating model and deployment");
80+
LOGGER.fine("Create Empty " + wml_type + " Model");
81+
String model_id = wml.createNewModel(wml_name, wml_type, null);
6982
LOGGER.fine("model_id = " + model_id);
7083

71-
deployment_id = wml.deployModel(wml_name, model_id, size, nodes);
84+
deployment_id = wml.deployModel(wml_name, model_id, wml_size, nodes);
7285
}
7386
LOGGER.fine("deployment_id = " + deployment_id);
7487

7588
JSONArray input_data = new JSONArray();
7689
input_data.put(wml.createDataFromFile(wml_name+".sav.gz", model.getAbsolutePath()));
90+
input_data.put(wml.createDataFromFile(wml_name+".prm", parameters.getAbsolutePath()));
7791
WMLJob job = wml.createAndRunJob(deployment_id, input_data, null, null, null);
7892

7993
switch (job.getSolveStatus()) {

0 commit comments

Comments
 (0)