Decision Optimization

Decision Optimization

Delivers prescriptive analytics capabilities and decision intelligence to improve decision-making.

Β View Only
Expand all | Collapse all

CP Optimizer + CMake + C++

  • 1.  CP Optimizer + CMake + C++

    Posted Fri May 03, 2024 09:33 PM

    Hello. 

    I am trying to setup a C++ project with CMake for the compilation. However, I am having some issues when linking the Cplex CPO. For linking the Cplex dependecies, I used the FindCPLEX.cmake.

    The complete setup can be seen in the repository, where I try to compile the cpoptimizer/examples/src/cpp/alloc.cpp. Since I am not sure if I can share this file, I left an empty src/main.cpp in the repository, so if someone wants to reproduce the experiment, you will have to ctrl+c/ctrl+v the content from your cplex folder.

    Below follows my CMakeLists.txt:

    cmake_minimum_required(VERSION 3.23)
    project(CplexCP)
    
    ##### SETs
    set (Project_PATH "${PROJECT_SOURCE_DIR}")
    set (CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_CURRENT_SOURCE_DIR}/support/cmake)
    set (CMAKE_BUILD_TYPE Release) 
    set (CMAKE_CXX_STANDARD 20)
    set (BUILD_SHARED_LIBS On)
    
    ##### Packages
    find_package (CPLEX)
    
    ##### Include Dirs 
    
    link_directories(${CPLEX_CP_LIBRARY})
    
    include_directories(
        SYSTEM
        includes
        "${PROJECT_BINARY_DIR}"
        "${CPLEX_CONCERT_INCLUDE_DIR}"
        "${CPLEX_ILOCPLEX_INCLUDE_DIR}"
        "${CPLEX_CP_INCLUDE_DIR}"
        )
    
    
    ##### Compile options 
    
    add_compile_options(-Wall
        #
        -DIL_STD
        -Wfatal-errors
        -pedantic
        -fopenmp
        -DDEBUG
        -fdiagnostics-color=always
        -lz
        -std=c++20
        # CPLEX
        -lconcert 
        -lilocplex 
        -lcplex 
        -lcplex-cp
        -lcplex-concert
        -lcplex-library
        #
        -lm 
        -lpthread 
        -ldl
        -Wno-sign-compare
        )
    
    file(GLOB_RECURSE SOURCES "src/*.cpp")
    
    add_executable(
        main 
        ${SOURCES} 
        )
    
    target_link_libraries(
        main 
        ${CPLEX_CONCERT_LIBRARY}
        ${CPLEX_ILOCPLEX_LIBRARY}
        ${CPLEX_LIBRARY}
        ${CPLEX_CP_LIBRARY}
        ${CPLEX_CP_LIBRARY_DEBUG}
        pthread 
        m 
        dl
        )

    The cmake command works just fine, but the make, results in the following. The complete log can be seen here.

    /usr/bin/ld: /opt/ibm/ILOG/CPLEX_Studio2211/cpoptimizer/lib/x86-64_linux/static_pic/libcp.a(cpengine.o): in the function `IlcCPEngineI::IlcCPEngineI(IlcCPI*, IlcAllocator*, long, IlcCPOManagerI*)':
    cpengine.cpp:(.text+0x8ae6): reference to `CPXEclocksync' not defined
    ...
    /usr/bin/ld: /opt/ibm/ILOG/CPLEX_Studio2211/cpoptimizer/lib/x86-64_linux/static_pic/libcp.a(linoptcplex.o): in the function `IlcLinOptCplex::parameterConfiguration(long, double)':
    linoptcplex.cpp:(.text+0x5ad1): reference to `CPXsetintparam' not defined
    collect2: error: ld returned 1 exit status
    make[2]: *** [CMakeFiles/main.dir/build.make:102: main] Error 1
    make[1]: *** [CMakeFiles/Makefile2:83: CMakeFiles/main.dir/all] Error 2
    make: *** [Makefile:91: all] Error 2

    What am I missing in my CMakeLists.txt?

    Thanks and regards.



    ------------------------------
    Matheus Andrade
    ------------------------------


  • 2.  RE: CP Optimizer + CMake + C++

    Posted Sat May 04, 2024 12:52 PM

    Nevermind. Just found the proper CMake setup: https://github.com/martinWANG2014/CplexCPlusPlusDemo/tree/master.



    ------------------------------
    Matheus Andrade
    ------------------------------



  • 3.  RE: CP Optimizer + CMake + C++

    Posted Mon May 06, 2024 11:47 PM

    Good morning

    I am building a docplex scheduling model

    It is all working well until I added a minimize statement:

     

    model.minimize( itv.end for itv in intervals)

    OR

    model.add(model.minimize( itv.end for itv in intervals))

     

    where intervals is a list of all interval variables in the model

     

    These thoow an error:

    Argument 'exprs' should be a float expression

     

    Can anyone help me to understand the issue here please?

     

    Ross Dye

    0400669880

     






  • 4.  RE: CP Optimizer + CMake + C++

    Posted Tue May 07, 2024 04:55 AM

    Hello Ross,

    You can use :

    mdl.minimize(sum([end_of(itv) for itv in intervals]))

    or

    mdl.minimize(sum(end_of(itv) for itv in intervals))

    Best regards,



    ------------------------------
    Thierry Sola
    ------------------------------



  • 5.  RE: CP Optimizer + CMake + C++

    Posted Tue May 07, 2024 05:37 AM

    Thank you Thierry

    Should I place a condition on intervals to ensure they are in the solution as they may be alternatives that are not instantiated?

     

    Ross Dye

    0400669880

     






  • 6.  RE: CP Optimizer + CMake + C++

    Posted Tue May 07, 2024 06:21 AM

    Ross,

    You can add a value in case of absence as follow:

    mdl.minimize(sum([end_of(itv, horizon) for itv in intervals]))

    If the itv is absent,  the value returned by end_of(itv) is equal to horizon. Where horizon is an integer.

    Regards,



    ------------------------------
    Thierry Sola
    ------------------------------



  • 7.  RE: CP Optimizer + CMake + C++

    Posted Tue May 07, 2024 07:41 AM

    Thank you Thierry

    It's late here now so I'll try your suggestions tomorrow

     

    Regards

     

    Ross Dye

    0400669880

     






  • 8.  RE: CP Optimizer + CMake + C++

    Posted Tue May 07, 2024 07:49 AM

    Hi Thierry

    How do I add parameters to the solver, eg timelimit, verbosity, etc?

    The doco I found explains what they do, but not the format for how to apply them

     

    Ross Dye

    0400669880

     






  • 9.  RE: CP Optimizer + CMake + C++

    Posted Tue May 07, 2024 08:12 AM

    Ross,

       Here is one possible way: mdl.solve(TimeLimit=60, LogPeriod='Quiet', Workers=1, ...).

    Regards,



    ------------------------------
    Thierry Sola
    ------------------------------



  • 10.  RE: CP Optimizer + CMake + C++

    Posted Wed May 08, 2024 02:04 AM

     I would like to define a transition matrix for Interval Variables
    In some documentation I found a statement: "Transition times can be modeled using tuples with three elements. The first element is the interval variable type of one task, the second is the interval variable type of the other task and the third element of the tuple is the transition time from the first to the second. An integer interval variable type can be associated with each interval variable."
    I have defined many interval variables in my model, but I cannot find out what an "interval variable type" is nor how to define it



    ------------------------------
    Ross Dye
    ------------------------------



  • 11.  RE: CP Optimizer + CMake + C++

    Posted Wed May 08, 2024 10:22 AM

    Combining IBM's CP Optimizer with CMake for C++ projects streamlines optimization tasks within a robust build environment. Leveraging CMake's flexibility in managing complex build configurations, developers can seamlessly integrate CP Optimizer into their projects, ensuring efficient resource allocation, scheduling, and constraint satisfaction. This synergy empowers teams to tackle intricate optimization challenges while maintaining a structured, manageable codebase, enhancing productivity and solution quality.



    ------------------------------
    adam robin
    ------------------------------



  • 12.  RE: CP Optimizer + CMake + C++

    Posted Thu May 09, 2024 12:46 AM

    My apologies for the lack of clarity in my earlier question.

    I will try & do better:

     

    The problem I am trying to model is a manufacturing facility with multiple interconnected machines, and multiple alternative fixed routes that a product can move between machines.

    Products have multiple attributes (let's say red, white, blue) that affect both production rates (activity lengths) and changeover (transitions). Transitions differ between machines.

     

    I have successfully modelled the interval_variables for activities and implemented an objective function to minimise schedule makespan. This works fine.

     

    I now need to model transition times on each machine.

    As I understand I need a 3x3 transition matrix of integer_vars representing the Red-White-Blue transition times.

    I would need one of these matrices per machine.

     

    BUT I don't know how to:

    1. Associate each transition matrix the relevant machine. The solver decides which machine it will assign to the activity
    2. Associate attributes Red-White-Blue to activities (interval_vars)

     

    Any suggestions on where to start is appreciated.

     

    Ross Dye

    0400669880

     






  • 13.  RE: CP Optimizer + CMake + C++

    Posted Fri May 10, 2024 09:06 AM

    Hello Ross,

    Here is a link that should help you: https://www.ibm.com/docs/en/icos/22.1.1?topic=models-modeling-sequence-dependent-setup-times

    Here is a link to an example: https://ibmdecisionoptimization.github.io/docplex-doc/cp/visu.setup_times.py.html

    Regards,



    ------------------------------
    Thierry Sola
    ------------------------------



  • 14.  RE: CP Optimizer + CMake + C++

    Posted Tue March 25, 2025 09:32 AM

    An "interval variable type" in this context is essentially an integer label that categorizes different types of interval variables. This type is used to define a transition matrix, specifying transition times between different interval types.

    How to Define Interval Variable Types

    1. Assign an Integer Type to Each Interval Variable:
      You need to associate each interval variable with an integer type. This integer serves as an identifier for that interval in the transition matrix.

    2. Define the Transition Matrix Using Tuples:
      The transition times are then modeled as tuples:
      (interval_type_1, interval_type_2, transition_time) Here, interval_type_1 and interval_type_2 are the integer types of the interval variables.

    Example:

    Let's assume you have three types of interval variables in a scheduling model:

    • Task A (Type 1)

    • Task B (Type 2)

    • Task C (Type 3)

    You can define transition times as:

    python
    transition_times = [ (1, 2, 5), # 5 time units from Type 1 (Task A) to Type 2 (Task B) (2, 3, 10), # 10 time units from Type 2 (Task B) to Type 3 (Task C) (1, 3, 7) # 7 time units from Type 1 (Task A) to Type 3 (Task C) ]

    In your model, you should ensure that each interval variable has a corresponding type, and use this type to look up transition times in the matrix.

    Would you like more details on how to integrate this with a specific solver (e.g., CPLEX, Google OR-Tools)? πŸš€



    ------------------------------
    Eric Smock
    ------------------------------