├── README.md ├── linear_programming_sample.py └── mixed_integer_linear_programming_sample.py /README.md: -------------------------------------------------------------------------------- 1 | **Cplex Python API Sample File** 2 | 3 | In this repository, you can know how to use Cplex Python API to solve 4 | common mathematical programming problems. I create this repository 5 | because I have developed the way to import data into the 6 | mathematical programming - by using matrices. 7 | Further, you can also create the matrices via Database, 8 | Excel file or any other file type which you prefer. On the other hand, it is 9 | not easy to find a good sample on Internet. 10 | 11 | **CATALOG** 12 | 13 | 1. Linear Programming 14 | 15 | 2. Mixed Integer Linear Programming 16 | 17 | New models are coming soon... -------------------------------------------------------------------------------- /linear_programming_sample.py: -------------------------------------------------------------------------------- 1 | import cplex 2 | 3 | # ============================================================ 4 | # This file gives us a sample to use Cplex Python API to 5 | # establish a Linear Programming model and then solve it. 6 | # The Linear Programming problem displayed bellow is as: 7 | # min z = cx 8 | # subject to: Ax = b 9 | # ============================================================ 10 | 11 | # ============================================================ 12 | # Input all the data and parameters here 13 | num_decision_var = 3 14 | num_constraints = 3 15 | 16 | A = [ 17 | [1.0, -2.0, 1.0], 18 | [-4.0, 1.0, 2.0], 19 | [-2.0, 0, 1.0], 20 | ] 21 | b = [11.0, 3.0, 1.0] 22 | c = [-3.0, 1.0, 1.0] 23 | 24 | constraint_type = ["L", "G", "E"] # Less, Greater, Equal 25 | # ============================================================ 26 | 27 | # Establish the Linear Programming Model 28 | myProblem = cplex.Cplex() 29 | 30 | # Add the decision variables and set their lower bound and upper bound (if necessary) 31 | myProblem.variables.add(names= ["x"+str(i) for i in range(num_decision_var)]) 32 | for i in range(num_decision_var): 33 | myProblem.variables.set_lower_bounds(i, 0.0) 34 | 35 | # Add constraints 36 | for i in range(num_constraints): 37 | myProblem.linear_constraints.add( 38 | lin_expr= [cplex.SparsePair(ind= [j for j in range(num_decision_var)], val= A[i])], 39 | rhs= [b[i]], 40 | names = ["c"+str(i)], 41 | senses = [constraint_type[i]] 42 | ) 43 | 44 | # Add objective function and set its sense 45 | for i in range(num_decision_var): 46 | myProblem.objective.set_linear([(i, c[i])]) 47 | myProblem.objective.set_sense(myProblem.objective.sense.minimize) 48 | 49 | # Solve the model and print the answer 50 | myProblem.solve() 51 | print(myProblem.solution.get_values()) -------------------------------------------------------------------------------- /mixed_integer_linear_programming_sample.py: -------------------------------------------------------------------------------- 1 | import cplex 2 | 3 | # ============================================================ 4 | # This file gives us a sample to use Cplex Python API to 5 | # establish a Mixed Integer Linear Programming model and then solve it. 6 | # The problem displayed bellow is as: 7 | # min z = cx 8 | # subject to: Ax = b 9 | # and some of x is integer or binary 10 | # ============================================================ 11 | 12 | # ============================================================ 13 | # Input all the data and parameters here 14 | num_decision_var = 2 15 | num_constraints = 2 16 | 17 | A = [ 18 | [1, 9/14], 19 | [-2, 1] 20 | ] 21 | b = [51/14, 1/3] 22 | c = [1, 1] 23 | 24 | constraint_type = ["L", "L"] # Less, Greater, Equal 25 | # ============================================================ 26 | 27 | # Establish the Linear Programming Model 28 | myProblem = cplex.Cplex() 29 | 30 | # Add the decision variables and set their lower bound and upper bound (if necessary) 31 | myProblem.variables.add(names= ["x"+str(i) for i in range(num_decision_var)]) 32 | for i in range(num_decision_var): 33 | myProblem.variables.set_lower_bounds(i, 0.0) 34 | 35 | # Set the type of each variables 36 | myProblem.variables.set_types(0, myProblem.variables.type.integer) 37 | myProblem.variables.set_types(1, myProblem.variables.type.continuous) 38 | 39 | # Add constraints 40 | for i in range(num_constraints): 41 | myProblem.linear_constraints.add( 42 | lin_expr= [cplex.SparsePair(ind= [j for j in range(num_decision_var)], val= A[i])], 43 | rhs= [b[i]], 44 | names = ["c"+str(i)], 45 | senses = [constraint_type[i]] 46 | ) 47 | 48 | # Add objective function and set its sense 49 | for i in range(num_decision_var): 50 | myProblem.objective.set_linear([(i, c[i])]) 51 | myProblem.objective.set_sense(myProblem.objective.sense.maximize) 52 | 53 | # Solve the model and print the answer 54 | myProblem.solve() 55 | print(myProblem.solution.get_values()) --------------------------------------------------------------------------------