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

Multi-threaded CP-SAT solver is crashed #2001

Closed
vaidzelevich opened this issue May 3, 2020 · 11 comments
Closed

Multi-threaded CP-SAT solver is crashed #2001

vaidzelevich opened this issue May 3, 2020 · 11 comments
Assignees
Labels
Bug Lang: C++ Native implementation issue OS: Windows Windows OS Solver: CP-SAT Solver Relates to the CP-SAT solver
Projects
Milestone

Comments

@vaidzelevich
Copy link

What version of OR-tools and what language are you using?
Version: stable/v7.6
Language: C++ (Visual Studio 2019, version 16.5.4)

Which solver are you using (e.g. CP-SAT, Routing Solver, GLOP, BOP, Gurobi)
CP-SAT

What operating system (Linux, Windows, ...) and version?
Windows 10, build 18363.778

What did you do?
Solved the following multiple knapsacks problem using the CP-SAT solver

#include "ortools/sat/cp_model.h"

struct DataModel {
    const std::vector<int> weights = { 48, 30, 42, 36, 36, 48, 42, 42,
        36, 24, 30, 30, 42, 36, 36 };
    const std::vector<int> values = { 10, 30, 25, 50, 35, 30, 15, 40,
        30, 35, 45, 10, 20, 30, 25 };
    const int num_items = (int)weights.size();
    const std::vector<int> bin_capacities = { 100, 100, 100, 100, 100 };
    const int num_bins = (int)bin_capacities.size();
};

namespace operations_research {
    namespace sat {

        void MulitpleKnapsackSat() {
            DataModel data;
            std::vector<BoolVar> variables(data.num_items * data.num_bins);
            auto x = [&](int item_index, int bin_index) -> BoolVar& {
                return variables[item_index * data.num_bins + bin_index];
            };
            CpModelBuilder cp_model;
            for (auto i = 0; i < data.num_items; ++i) {
                for (auto j = 0; j < data.num_bins; ++j) {
                    x(i, j) = cp_model.NewBoolVar();
                }
            }
            for (auto i = 0; i < data.num_items; ++i) {
                LinearExpr sum;
                for (auto j = 0; j < data.num_bins; ++j) {
                    sum.AddTerm(x(i, j), 1);
                }
                cp_model.AddLessOrEqual(sum, 1);
            }
            for (auto j = 0; j < data.num_bins; ++j) {
                LinearExpr sum;
                for (auto i = 0; i < data.num_items; ++i) {
                    sum.AddTerm(x(i, j), data.weights[i]);
                }
                cp_model.AddLessOrEqual(sum, data.bin_capacities[j]);
            }
            LinearExpr obj;
            for (auto i = 0; i < data.num_items; ++i) {
                for (auto j = 0; j < data.num_bins; ++j) {
                    obj.AddTerm(x(i, j), data.values[i]);
                }
            }
            cp_model.Maximize(obj);

            SatParameters parameters;
            parameters.set_num_search_workers(16);
            const CpSolverResponse responce = SolveWithParameters(cp_model.Build(),
                parameters);           
        }
    }  // namespace sat
}  // namespace operations_research

int main() {
    operations_research::sat::MulitpleKnapsackSat();
    return EXIT_SUCCESS;
}

What did you expect to see
Successful solving of the problem as the single-threaded solver does

What did you see instead?
The solver is crashed in the destructor LinearConstraintManager::~LinearConstraintManager() while trying to check the flag sat_parameters_.log_search_progress()

Make sure you include information that can help us debug (full error message, model Proto).

Anything else we should know about your project / environment
The library was built from the sources using cmake

@lperron
Copy link
Collaborator

lperron commented May 3, 2020 via email

@Mizux Mizux added Bug OS: Windows Windows OS Solver: CP-SAT Solver Relates to the CP-SAT solver Lang: C++ Native implementation issue labels May 3, 2020
@Mizux Mizux added this to To do in ToDo via automation May 3, 2020
@Mizux
Copy link
Collaborator

Mizux commented May 3, 2020

could be related to #1958 aka 929f3ea which is not in v7.6 but on master

@vaidzelevich
Copy link
Author

Thank you very much for so quick answer.
I have tried the master branch. Unfortunately, the CP-SAT solver still crashes with error.

@lperron
Copy link
Collaborator

lperron commented May 3, 2020 via email

@ghost ghost mentioned this issue May 5, 2020
@lperron
Copy link
Collaborator

lperron commented Aug 19, 2020

I believe this was just fixed.
Can you rebuild from master and try it ?

@Mizux Mizux added this to the v7.9 milestone Aug 19, 2020
@vaidzelevich
Copy link
Author

Thank you for your response. I’ve tried to build the latest version from the master branch, but there is a problem, namely, the compiler says that files ortools/sat/cp_model.pb.h, ortools/sat/sat_parameters.pb.h, … are missed. I will try to fix the problem and provide the answer as soon as possible.

@lperron
Copy link
Collaborator

lperron commented Aug 22, 2020 via email

@vaidzelevich
Copy link
Author

I use or-tools by incorporating it into my cmake project. It turned out that missed files are in the build directory, so using cmake’s command include_directories I’ve solved the mentioned problem. I rebuilt and checked the test. Everything is good. Thank you very much.

ToDo automation moved this from To do to Done Aug 22, 2020
@Mizux
Copy link
Collaborator

Mizux commented Aug 22, 2020

@vaidzelevich out of curiosity how did you incorporate or-tools ? i.e. FetchContent, Add_subdirectory(), ExternalProject() ?

note: Soon I would like to add some ci tests to test or-tools integration in a super build

@vaidzelevich
Copy link
Author

I use add_subdirectory, to be more precise, the following commands

set(BUILD_DEPS ON CACHE BOOL "" FORCE)
set(USE_SCIP OFF CACHE BOOL "" FORCE)
add_subdirectory(third-party/or-tools EXCLUDE_FROM_ALL)
target_link_libraries(myapp ortools::ortools)
include_directories(${CMAKE_BINARY_DIR}/third-party/or-tools)

@Mizux
Copy link
Collaborator

Mizux commented Aug 23, 2020

Thanks !
This line shouldn't be needed include_directories(${CMAKE_BINARY_DIR}/third-party/or-tools) since ortools::ortools should provide all needed BUILD_INTERFACE include directories, otherwise there is a bug in OR-Tools CMakeLists.txt...
On my way to fix them !
note: I also found my rusty project https://github.com/Mizux/cmake-ortools I need to clean it but should be good as a starting point ;)

BTW many thanks for your feedback, it's always cool to see people integrating it in their process...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Lang: C++ Native implementation issue OS: Windows Windows OS Solver: CP-SAT Solver Relates to the CP-SAT solver
Projects
ToDo
  
Done
Development

No branches or pull requests

3 participants