IBM Support

Running CPLEX in a script

Question & Answer


Question

How can I launch CPLEX in a script?

Answer

If you want to use a batch file that executes specific CPLEX commands, we first recommend that you make use of CPLEX's Python API, available starting with CPLEX 12.1. Since Python is an interpreted language, the CPLEX Python API gives you a powerful scripting capability with much more flexibility and control than the Interactive CPLEX Optimizer. However, if you prefer to use a batch file to execute interactive CPLEX commands, this is what you should do.

Create a simple text file containing the list of interactive optimizer commands you want to execute through the batch file. For example, the file could be called mycplexcommands and contain the following text:

read mytest.mps
optimize
display solution variables 1-10
quit

Then, you need to create a batch file or a shell script (depending on your platform).

The following example runs a series of 100 tests on files named from mytest1.mps to mytest100.mps

--------------------------------------
Windows batch file version:
@echo off
for /L %%i in (1,1,100) do (
move mytest%i.mps mytest.mps
cplex < mycplexcommands
move cplex.log mytest%i.log
)
--------------------------------------
Unix shell script version:
#!/bin/sh
for ((i=0; i<100; i++))
do
mv mytest$i.mps mytest.mps;
./cplex < mycplexcommands;
mv cplex.log mytest$i.log;
done

Note that, starting with version 12.5, CPLEX can process sequences of command using either the "-f" or "-c" command line options. For example, "cplex -f mycplexcommands" will execute the list of instructions in the file mycplexcommands, providing an alternative to the redirection command "cplex < mycplexcommands" used above. Or, you can use the "-c" option to specify a sequence of strings containing the instructions to execute. For the instructions in the file mycplexcommands above, the instructions could be executed on the command line with the command

cplex -c "read mytest.mps" "optimize" "display solution variables 1-10".

[{"Product":{"code":"SSSA5P","label":"IBM ILOG CPLEX Optimization Studio"},"Business Unit":{"code":"BU059","label":"IBM Software w\/o TPS"},"Component":"Not Applicable","Platform":[{"code":"PF025","label":"Platform Independent"}],"Version":"9.2;9.1.3;9.1.2;9.1;9.0;8.1;8.0;7.5;7.1;7.0;6.6;12.5;12.4;12.3;12.2;12.1;12.0;11.2.1;11.2;11.1.1;11.1;11.0.1;11.0;10.3;10.2.1;10.2;10.1.1;10.1;10.0","Edition":"","Line of Business":{"code":"LOB10","label":"Data and AI"}},{"Product":{"code":"SSSA5P","label":"IBM ILOG CPLEX Optimization Studio"},"Business Unit":{"code":"BU059","label":"IBM Software w\/o TPS"},"Component":"General","Platform":[{"code":"PF002","label":"AIX"},{"code":"PF010","label":"HP-UX"},{"code":"PF016","label":"Linux"},{"code":"PF027","label":"Solaris"},{"code":"PF033","label":"Windows"},{"code":"PF017","label":"Mac OS"}],"Version":"12.2","Edition":"All Editions","Line of Business":{"code":"LOB10","label":"Data and AI"}}]

Historical Number

cplex/FAQ/151

Document Information

Modified date:
16 June 2018

UID

swg21400068