Skip to content

Commit 7f45ce9

Browse files
committed
work on catalogs and fixed content for v2 instances
1 parent 454d318 commit 7f45ce9

10 files changed

Lines changed: 443 additions & 29 deletions

File tree

.gitignore

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,9 @@ src/MyDevFinalV4Credentials.java
66
src/MyProdBetaV4Credentials.java
77
src/resources/esso.cpo
88
src/resources/mymodel.sav
9-
src/MyDevFinalV4Credentials.java
109
src/MyQAFinalV4Credentials.java
10+
/src/private/
11+
src/MyProdFinalV4Credentials.java
12+
/src/PrivateTests.java
13+
src/MyProdFinalV4Credentials.java
14+
src/MyProdFinalV4Credentials.java

src/FoodManufact.java

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
/* --------------------------------------------------------------------------
2+
* File: FoodManufact.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+
* FoodManufact.java - An implementation of an example from H.P.
15+
* Williams' book Model Building in Mathematical
16+
* Programming. This example solves a
17+
* food production planning problem. It
18+
* demonstrates the use of CPLEX's
19+
* linearization capability.
20+
*/
21+
22+
import com.ibm.wmlconnector.WMLConnector;
23+
import ilog.concert.*;
24+
import ilog.cplex.*;
25+
26+
public class FoodManufact {
27+
final static int v1 = 0;
28+
final static int v2 = 1;
29+
final static int o1 = 2;
30+
final static int o2 = 3;
31+
final static int o3 = 4;
32+
33+
final static double[][] cost = {{110.0, 120.0, 130.0, 110.0, 115.0},
34+
{130.0, 130.0, 110.0, 90.0, 115.0},
35+
{110.0, 140.0, 130.0, 100.0, 95.0},
36+
{120.0, 110.0, 120.0, 120.0, 125.0},
37+
{100.0, 120.0, 150.0, 110.0, 105.0},
38+
{ 90.0, 100.0, 140.0, 80.0, 135.0}};
39+
40+
public static void main (String args[]) {
41+
int nMonths = cost.length;
42+
int nProducts = cost[0].length;
43+
44+
//try (IloCplex cplex = new IloCplex()) {
45+
try (IloCplex cplex = new CplexWithWML(new MyProdBetaV4Credentials())) {
46+
IloNumVar[] produce = cplex.numVarArray(nMonths, 0, Double.MAX_VALUE);
47+
IloNumVar[][] use = new IloNumVar[nMonths][];
48+
IloNumVar[][] buy = new IloNumVar[nMonths][];
49+
IloNumVar[][] store = new IloNumVar[nMonths][];
50+
51+
for (int i = 0; i < nMonths; i++) {
52+
use[i] = cplex.numVarArray(nProducts, 0, Double.MAX_VALUE);
53+
buy[i] = cplex.numVarArray(nProducts, 0, Double.MAX_VALUE);
54+
store[i] = cplex.numVarArray(nProducts, 0, 1000.0);
55+
}
56+
57+
for (int p = 0; p < nProducts; p++) {
58+
store[nMonths-1][p].setLB(500.0);
59+
store[nMonths-1][p].setUB(500.0);
60+
}
61+
62+
IloNumExpr profit = cplex.numExpr();
63+
for (int i = 0; i < nMonths; i++) {
64+
// Not more than 200 tons of vegetable oil can be refined
65+
cplex.addLe(cplex.sum(use[i][v1], use[i][v2]), 200.0);
66+
67+
// Not more than 250 tons of non-vegetable oil can be refined
68+
cplex.addLe(cplex.sum(use[i][o1], use[i][o2], use[i][o3]), 250.0);
69+
70+
// Constraints on food composition
71+
cplex.addLe(cplex.prod(3., produce[i]),
72+
cplex.sum(cplex.prod(8.8, use[i][v1]),
73+
cplex.prod(6.1, use[i][v2]),
74+
cplex.prod(2.0, use[i][o1]),
75+
cplex.prod(4.2, use[i][o2]),
76+
cplex.prod(5.0, use[i][o3])));
77+
cplex.addGe(cplex.prod(6., produce[i]),
78+
cplex.sum(cplex.prod(8.8, use[i][v1]),
79+
cplex.prod(6.1, use[i][v2]),
80+
cplex.prod(2.0, use[i][o1]),
81+
cplex.prod(4.2, use[i][o2]),
82+
cplex.prod(5.0, use[i][o3])));
83+
cplex.addEq(produce[i], cplex.sum(use[i]));
84+
85+
// Raw oil can be stored for later use
86+
if (i == 0) {
87+
for (int p = 0; p < nProducts; p++)
88+
cplex.addEq(cplex.sum(500.0, buy[i][p]),
89+
cplex.sum(use[i][p], store[i][p]));
90+
}
91+
else {
92+
for (int p = 0; p < nProducts; p++)
93+
cplex.addEq(cplex.sum(store[i-1][p], buy[i][p]),
94+
cplex.sum(use[i][p], store[i][p]));
95+
}
96+
97+
// Logical constraints:
98+
// The food cannot use more than 3 oils
99+
// (or at least two oils must not be used)
100+
cplex.addGe(cplex.sum(cplex.eq(use[i][v1], 0),
101+
cplex.eq(use[i][v2], 0),
102+
cplex.eq(use[i][o1], 0),
103+
cplex.eq(use[i][o2], 0),
104+
cplex.eq(use[i][o3], 0)), 2);
105+
106+
// When an oil is used, the quantity must be at least 20 tons
107+
for (int p = 0; p < nProducts; p++)
108+
cplex.add(cplex.or(cplex.eq(use[i][p], 0),
109+
cplex.ge(use[i][p], 20)));
110+
111+
// If products v1 or v2 are used, then product o3 is also used
112+
cplex.add(cplex.ifThen(cplex.or(cplex.ge(use[i][v1], 20),
113+
cplex.ge(use[i][v2], 20)),
114+
cplex.ge(use[i][o3], 20)));
115+
116+
// Objective function
117+
profit = cplex.sum (profit, cplex.prod(150, produce[i]));
118+
profit = cplex.diff(profit, cplex.scalProd(cost[i], buy[i]));
119+
profit = cplex.diff(profit, cplex.prod(5, cplex.sum(store[i])));
120+
}
121+
122+
cplex.addMaximize(profit);
123+
124+
if ( cplex.solve() ) {
125+
System.out.println("Solution status: " + cplex.getStatus());
126+
System.out.println(" Maximum profit = " + cplex.getObjValue());
127+
for (int i = 0; i < nMonths; i++) {
128+
System.out.println(" Month " + i);
129+
System.out.print(" . buy ");
130+
for (int p = 0; p < nProducts; p++)
131+
System.out.print(cplex.getValue(buy[i][p]) + "\t ");
132+
System.out.println();
133+
System.out.print(" . use ");
134+
for (int p = 0; p < nProducts; p++)
135+
System.out.print(cplex.getValue(use[i][p]) + "\t ");
136+
System.out.println();
137+
System.out.print(" . store ");
138+
for (int p = 0; p < nProducts; p++)
139+
System.out.print(cplex.getValue(store[i][p]) + "\t ");
140+
System.out.println();
141+
}
142+
}
143+
144+
}
145+
catch (IloException e) {
146+
System.err.println("Concert exception caught: " + e);
147+
System.exit(-1);
148+
}
149+
}
150+
}

src/Sample.java

Lines changed: 53 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
import java.nio.file.Files;
33
import java.nio.file.Paths;
44
import java.util.*;
5+
import java.util.logging.ConsoleHandler;
6+
import java.util.logging.Level;
57
import java.util.logging.Logger;
68

79
import com.ibm.wmlconnector.Credentials;
@@ -17,8 +19,9 @@ public class Sample {
1719
private static final Logger LOGGER = Logger.getLogger(Sample.class.getName());
1820

1921
//private static final Credentials CREDENTIALS = new MyDevFinalV4Credentials();
20-
private static final Credentials CREDENTIALS = new MyQAFinalV4Credentials();
22+
//private static final Credentials CREDENTIALS = new MyQAFinalV4Credentials();
2123
//private static final Credentials CREDENTIALS = new MyProdBetaV4Credentials();
24+
private static final Credentials CREDENTIALS = new MyProdFinalV4Credentials();
2225

2326

2427
public String createAndDeployEmptyModel(WMLConnector wml, WMLConnector.ModelType type, WMLConnector.TShirtSize size, int nodes) {
@@ -76,7 +79,8 @@ public void fullDietPythonFlow(boolean useOutputDataReferences, int nJobs) {
7679
if (useOutputDataReferences) {
7780
getLogFromCOS(cos); // Don't log
7881
} else {
79-
getLogFromJob(job); // Don't log
82+
LOGGER.info("Log:" + getLogFromJob(job)); // Don't log
83+
LOGGER.info("Solution:" + getSolutionFromJob(job));
8084
}
8185
long endTime = System.nanoTime();
8286
long totalTime = endTime - startTime;
@@ -268,6 +272,19 @@ public void fullCPOFlow(String modelName) {
268272
deleteDeployment(wml, deployment_id);
269273
}
270274

275+
public String createAndDeployScalableWarehouseOPLModel(WMLConnector wml) {
276+
277+
LOGGER.info("Create Warehouse OPL Model");
278+
279+
String model_id = wml.createNewModel("Warehouse", WMLConnector.ModelType.OPL_12_9,"src/resources/scalableWarehouse_yes.zip");
280+
LOGGER.info("model_id = "+ model_id);
281+
282+
String deployment_id = wml.deployModel("warehouse-opl-test-wml-2", model_id, WMLConnector.TShirtSize.S,1);
283+
LOGGER.info("deployment_id = "+ deployment_id);
284+
285+
return deployment_id;
286+
}
287+
271288
public String createAndDeployWarehouseOPLModel(WMLConnector wml) {
272289

273290
LOGGER.info("Create Warehouse OPL Model");
@@ -281,6 +298,7 @@ public String createAndDeployWarehouseOPLModel(WMLConnector wml) {
281298
return deployment_id;
282299
}
283300

301+
284302
public void fullWarehouseOPLFlow(boolean useOutputDataReferences) {
285303

286304
LOGGER.info("Full Warehouse with OPL");
@@ -308,6 +326,20 @@ public void fullWarehouseOPLFlow(boolean useOutputDataReferences) {
308326
deleteDeployment(wml, deployment_id);
309327
}
310328

329+
public void fullScalableWarehouseOPLFlow() {
330+
331+
LOGGER.info("Full Scalable Warehouse with OPL");
332+
333+
WMLConnectorImpl wml = new WMLConnectorImpl(CREDENTIALS);
334+
String deployment_id = createAndDeployScalableWarehouseOPLModel(wml);
335+
336+
337+
WMLJob job = wml.createAndRunJob(deployment_id, null, null, null, null);
338+
LOGGER.info("Log:" + getLogFromJob(job));
339+
340+
deleteDeployment(wml, deployment_id);
341+
}
342+
311343

312344
public String createAndDeployDietOPLModel(WMLConnector wml) {
313345

@@ -584,10 +616,16 @@ void testV4final() {
584616
LOGGER.info("Test v4 final.");
585617
WMLConnector wml = new WMLConnectorImpl(CREDENTIALS);
586618

587-
588619
//LOGGER.info("Instances: " + wml.getInstances());
620+
589621
LOGGER.info("Spaces: " + wml.getDeploymentSpaces());
590622

623+
String space_id = wml.getDeploymentSpaceIdByName("test_v4_space");
624+
625+
LOGGER.info("space_id: " + space_id);
626+
627+
LOGGER.info("Assetfiles: " + wml.getAssetFiles(space_id));
628+
591629
//LOGGER.info("Software Specifications: " + wml.getSoftwareSpecifications());
592630

593631
//wml.createDeploymentSpace("test_space");
@@ -599,17 +637,25 @@ void testV4final() {
599637
public static void main(String[] args) {
600638
Sample main = new Sample();
601639

640+
WMLConnectorImpl.LOGGER.setLevel(Level.FINER);
641+
ConsoleHandler handler = new ConsoleHandler();
642+
// PUBLISH this level
643+
handler.setLevel(Level.FINER);
644+
WMLConnectorImpl.LOGGER.addHandler(handler);
645+
646+
602647
//main.createSpace("test_space_2");
603648

604649
//main.testV4final();
605650

606651
//main.testPerfs(1);
607652

608653
// Python
609-
//main.fullDietPythonFlow(false, 1);
654+
main.fullDietPythonFlow(false, 1);
610655

611656
// OPL
612657
//main.fullWarehouseOPLFlow(true);
658+
//main.fullScalableWarehouseOPLFlow();
613659
//main.fullDietOPLWithDatFlow(false);
614660
//main.fullDietOPLWithCSVFlow(false);
615661

@@ -626,9 +672,9 @@ public static void main(String[] args) {
626672

627673
//main.fullLPInlineFLow("bigone.mps", 1 );
628674
//main.fullLPInlineFLow("diet.lp", 1 );
629-
main.parallelFullLPInlineFlow("diet.lp", 5, 20 );
630-
//main.fullLPInlineFLow("acc-tight4.lp", 20 );
631-
//main.parallelFullLPInlineFlow("acc-tight4.lp", 5, 100 );
675+
//main.parallelFullLPInlineFlow("diet.lp", 5, 20 );
676+
//main.fullLPInlineFLow("acc-tight4.lp", 1 );
677+
//main.parallelFullLPInlineFlow("acc-tight4.lp", 3, 10 );
632678

633679
// main.fullInfeasibleLPFLow();
634680

src/com/ibm/wmlconnector/WMLConnector.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ public String toString(){
7474
public JSONObject getSoftwareSpecifications();
7575
public String createDeploymentSpace(String name);
7676
public JSONObject getDeploymentSpaces();
77+
public String getDeploymentSpaceIdByName(String spaceName);
78+
public JSONObject getAssetFiles(String space_id);
7779

7880
public String createNewModel(String modelName, ModelType type, String modelAssetFilePath, Runtime runtime);
7981
public String createNewModel(String modelName, ModelType type, String modelAssetFilePath);

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

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

33

44
import java.io.*;
5+
import java.nio.charset.StandardCharsets;
56
import java.nio.file.Files;
67
import java.nio.file.Paths;
78
import java.util.logging.Logger;
@@ -237,15 +238,29 @@ public static byte[] getBinaryFileContent(String inputFilename) {
237238
return encoded;
238239
}
239240

240-
public static String getFileContent(String inputFilename) {
241+
public static String getFileContent(String inputFilename) {
241242
String res = "";
243+
/*
242244
try {
243245
List<String> lines = Files.readAllLines(Paths.get(inputFilename));
244246
for (Iterator<String> it = lines.iterator(); it.hasNext();)
245247
res += it.next() + "\n";
246248
} catch (IOException e) {
247249
LOGGER.severe("Error getting text file" + e.getStackTrace());
248250
}
251+
*/
252+
253+
try {
254+
final BufferedReader in = new BufferedReader(
255+
new InputStreamReader(new FileInputStream(inputFilename), StandardCharsets.UTF_8));
256+
String line;
257+
while ((line = in.readLine()) != null) {
258+
res += line + "\n";
259+
}
260+
in.close();
261+
} catch (IOException e) {
262+
LOGGER.severe("Error getting text file" + e.getStackTrace());
263+
}
249264

250265
return res;
251266
}

0 commit comments

Comments
 (0)