Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

C++ Hello World - Segmentation Fault #134

Open
hlefebvr opened this issue Feb 1, 2024 · 3 comments
Open

C++ Hello World - Segmentation Fault #134

hlefebvr opened this issue Feb 1, 2024 · 3 comments

Comments

@hlefebvr
Copy link

hlefebvr commented Feb 1, 2024

Hello,

I am trying to build a minimal example for using MibS with C++.

Context

I am trying to model the following problem using the C++ API:

 min  -x − 7y
 s.t.   -3x + 2y ≤ 12
             x + 2y ≤ 20
             x ≤ 10
             y ∈ arg min {z : 2x - z ≤ 7,
                              -2x + 4z ≤ 16,
                              z ≤ 5}

Setting

  • MibS 1.2 as built using coinbrew and linked with Cplex - though the problem I am facing seems indifferent to CPLEX.
  • Ubuntu 22.04 LTS
  • CMake as build system
  • C++17

My Attempt

So far, I have tried with the following code.

    std::unique_ptr<OsiSolverInterface> solver(new OsiCpxSolverInterface());

    MibSModel model;
    model.setSolver(solver.get());

   // AUXILIARY DATA
    std::vector<int> upper_level_variables_indices = { 0,  };
    std::vector<int> lower_level_variables_indices = { 1,  };
    std::vector<int> upper_level_constraints_indices = { 0, 1,  };
    std::vector<int> lower_level_constraints_indices = { 2, 3,  };
    std::vector<double> lower_level_objective_coefficients = { 1,  };

    model.loadAuxiliaryData(
            (int) lower_level_variables_indices.size(),
            (int) lower_level_constraints_indices.size(),
            lower_level_variables_indices.data(),
            lower_level_constraints_indices.data(),
            1.,
            lower_level_objective_coefficients.data(),
            (int) upper_level_variables_indices.size(),
            (int) upper_level_constraints_indices.size(),
            upper_level_variables_indices.data(),
            upper_level_constraints_indices.data(),
            0,
            nullptr,
            0,
            nullptr,
            nullptr,
            nullptr
    );

   // PROBLEM DATA

    std::vector<double> variable_lower_bounds = { 0, 0,  };
    std::vector<double> variable_upper_bounds = { 10, 5,  };
    std::vector<char> variable_types = { 'I', 'I',  };
    std::vector<double> constraint_lower_bounds = { -1e+20, -1e+20, -1e+20, -1e+20,  };
    std::vector<double> constraint_upper_bounds = { 12, 20, 7, 16,  };
    std::vector<char> constraint_types = { 'L', 'L', 'L', 'L',  };
    std::vector<double> objective = { -1, -7,  };

    CoinPackedMatrix matrix(false, 0, 2);

    CoinPackedVector row1;
    row1.insert(0, -3);
    row1.insert(1, 2);
    matrix.appendRow(row1);

    CoinPackedVector row2;
    row2.insert(0, 1);
    row2.insert(1, 2);
    matrix.appendRow(row2);

    CoinPackedVector row3;
    row3.insert(0, 2);
    row3.insert(1, -1);
    matrix.appendRow(row3);

    CoinPackedVector row4;
    row4.insert(0, -2);
    row4.insert(1, 4);
    matrix.appendRow(row4);

    model.loadProblemData(
            matrix,
            variable_lower_bounds.data(),
            variable_upper_bounds.data(),
            objective.data(),
            constraint_lower_bounds.data(),
            constraint_upper_bounds.data(),
            variable_types.data(),
            1.,
            1e+20,
            constraint_types.data()
    );

Quesitons and Problems

  • I get a Segmentation Fault when running this code. This happens in the call of loadProblemData and, in particular, seems to be in setUpperRowData.
  • What does the arguments structRowXX relate to ?

Thank you,

Henri.

@hlefebvr
Copy link
Author

Up?

@tkralphs
Copy link
Member

The seg fault was because the matrix you constructed was row-ordered, but MibS expects a column-ordered matrix for some reason. This is obviously not carefully documented and shouldn't be required in any case. For some reason, we are using the getMinorDim() and getMajorDim() methods to figure out the number of rows and columns, but this fails when the ordering is not as expected. We should be error-checking this to see if the dimensions make sense, but we are not. Very sorry about this! Adding

   matrix.reverseOrdering();

before the call to loadProblemData() fixes the seg fault and adding

   AlpsKnowledgeBrokerSerial broker(argc, argv, model, false);
   broker.search(&model);

after results in what seems to be a correct solve call returning

=======================================
Problem structure
=======================================

Number of UL Variables (total): 1
Number of UL Variables (integer): 1
Number of LL Variables (total): 1
Number of LL Variables (integer): 1
Number of UL Rows: 2
Number of LL Rows: 2
This instance is a pure integer problem.
Coefficient matrix of lower level variables in upper level problem is non-positive.
Degree of objective alignment: 0

=======================================
Parameter Settings
=======================================

Branching strategy is to branch only on variable with fractional values.
Hypercube intersection cut generator is on.
Second level problem will be solved when linking variables are fixed
  or first and second level variables are integer.
Upper bounding problem will be solved when linking variables are fixed.
Linking solution pool will be used.

Blis0043I Objective coefficients are multiples of 1
Alps0250I Starting search ...
Clp0006I 0  Obj 0 Dual inf 7.9999998 (2)
Clp0006I 1  Obj -41
Clp0000I Optimal - objective value -41
         1           -41                  0.00%      0          0

Alps0208I Search completed.
Alps0260I Best solution found had quality -41
Alps0264I Number of nodes processed:                1
Alps0267I Number of nodes branched:                 0
Alps0268I Number of nodes pruned before processing: 0
Alps0270I Number of nodes left:                     0
Alps0272I Tree depth: 0
Alps0274I Search CPU time: 0.00 seconds
Alps0278I Search wall-clock time: 0.00 seconds
Blis0040I Checking feasibility took 0.0004 seconds
Blis0058I Relative optimality gap is 0.00%

@tkralphs
Copy link
Member

@yuxies Can you take a look at a fix for this? It should be easy to check if the ordering is correct and to reverse it if not.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants