google / or-tools

Google's Operations Research tools:
https://developers.google.com/optimization/
Apache License 2.0
11.25k stars 2.13k forks source link

Multi-threaded CP-SAT solver is crashed #2001

Closed vaidzelevich closed 4 years ago

vaidzelevich commented 4 years ago

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 satparameters.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 commented 4 years ago

Works flawlessly on mac. Laurent Perron | Operations Research | lperron@google.com | (33) 1 42 68 53 00

Le dim. 3 mai 2020 à 18:10, vaidzelevich notifications@github.com a écrit :

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 weights = { 48, 30, 42, 36, 36, 48, 42, 42, 36, 24, 30, 30, 42, 36, 36 }; const std::vector 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 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 satparameters.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

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/google/or-tools/issues/2001, or unsubscribe https://github.com/notifications/unsubscribe-auth/ACUPL3IKBQLDRJOX424SIDLRPWJPJANCNFSM4MYF6V4Q .

Mizux commented 4 years ago

could be related to #1958 aka https://github.com/google/or-tools/commit/929f3ea8733843ce75b005905870a48dc5900a11 which is not in v7.6 but on master

vaidzelevich commented 4 years ago

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

lperron commented 4 years ago

The fix is useful when you run multiple solvers in parallel (as in a server). Laurent Perron | Operations Research | lperron@google.com | (33) 1 42 68 53 00

Le dim. 3 mai 2020 à 23:52, vaidzelevich notifications@github.com a écrit :

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

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/google/or-tools/issues/2001#issuecomment-623188144, or unsubscribe https://github.com/notifications/unsubscribe-auth/ACUPL3PPDRR4HHHOPOKSCR3RPXRQPANCNFSM4MYF6V4Q .

lperron commented 4 years ago

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

vaidzelevich commented 4 years ago

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 commented 4 years ago

make all_protos make

Laurent Perron | Operations Research | lperron@google.com | (33) 1 42 68 53 00

Le sam. 22 août 2020 à 10:07, vaidzelevich notifications@github.com a écrit :

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.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/google/or-tools/issues/2001#issuecomment-678611456, or unsubscribe https://github.com/notifications/unsubscribe-auth/ACUPL3JGVEOHFPTUPE3S473SB54FVANCNFSM4MYF6V4Q .

vaidzelevich commented 4 years ago

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.

Mizux commented 4 years ago

@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 commented 4 years ago

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 commented 4 years ago

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...