Decision Optimization

 View Only
  • 1.  I couldn't run CPLEX in my Python.

    Posted Tue February 11, 2025 08:15 AM

    I'm using IBM(R) ILOG(R) CPLEX(R) Interactive Optimizer 22.1.2.0 (Academic Initiative) on Ubuntu 22.04.5 LTS with Python 3.10.12. The following is my Python code. 

    '''

    import cplex
    from cplex import Cplex
    from docplex.mp.model import Model
    import os, fileinput

    milp_model = docplex.Cplex()
    objective = 1.0
    index = 0

    while objective == 1.0:
        
        milp_model.read("model.lp")

        milp_model.solve()
        objective = milp_model.solution.get_objective_value()

        print("OBJECTIVE " + str(index) + ": " + str(objective) + "\n")

        #print("VARIABLES: " + str(milp_model.solution.get_values()) + "\n")

        if objective == 1.0:
        
            for i in range(1,6):

                variable_value = milp_model.solution.get_values("x" + str(i))

                print("x" + str(i) + ": " + str(variable_value) + "\n")

                if variable_value == 1.0:

                    for line in fileinput.FileInput("model.lp", inplace=True):

                        if "Binary" in line:
                            print("x" + str(i) + " = 0")
                            
                        print(line, end = "")

    '''

    This code works in Community version, but not in Academic Initiative. It throws error message: 

    Traceback (most recent call last):
      File "/home/insfornet/MILP/Cube_Permutation_3/main.py", line 1, in <module>
        import cplex
    ModuleNotFoundError: No module named 'cplex'

    Please help. Thank you. 



    ------------------------------
    Kok-An Pang
    ------------------------------


  • 2.  RE: I couldn't run CPLEX in my Python.

    Posted 22 days ago

    Hi

    have you done ?

    pip install cplex
    pip install docplex


    ------------------------------
    [Alex] [Fleischer]
    [Data and AI Technical Sales]
    [IBM]
    ------------------------------



  • 3.  RE: I couldn't run CPLEX in my Python.

    Posted 22 days ago

    I have tried "pip install cplex", but it installed CPlex Community Edition. But what I want is IBM CPLEX Academic Initiative, not Community edition. The community edition does not run my script due to its large size. 



    ------------------------------
    Kok-An Pang
    ------------------------------



  • 4.  RE: I couldn't run CPLEX in my Python.

    Posted 22 days ago
    Edited by Cédric Doens 22 days ago

    Hello @Kok-An Pang.
    To upgrade your cplex python module to the level of your installed Academic Initiative, you have to :

    (python-env-3.11) ~$ docplex config --upgrade $COS_INSTALL_DIR

    May this help

    see https://community.ibm.com/community/user/question/python-installation-for-cplex-optimization-studio-2212 for details.



    ------------------------------
    Cédric Doens
    ------------------------------