From 9f029ef7e46d7f642298b3879a222fee9572eba8 Mon Sep 17 00:00:00 2001 From: hit_lu Date: Mon, 25 Aug 2025 13:33:35 +0800 Subject: [PATCH] =?UTF-8?q?Linux=E5=B9=B3=E5=8F=B0=E4=B8=8B=E7=BC=96?= =?UTF-8?q?=E8=AF=91=E5=8F=8A=E5=86=97=E4=BD=99=E6=96=87=E4=BB=B6=E5=88=A0?= =?UTF-8?q?=E9=99=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 6 +++- FindGUROBI.cmake | 73 ------------------------------------------------ inc/node.hpp | 17 ----------- src/solver.cpp | 33 +++++++++++++--------- 4 files changed, 25 insertions(+), 104 deletions(-) delete mode 100644 FindGUROBI.cmake delete mode 100644 inc/node.hpp diff --git a/.gitignore b/.gitignore index c795b05..7a9d609 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,5 @@ -build \ No newline at end of file +build +.vscode +CMakeFiles +CMakeCache.* +Makefile \ No newline at end of file diff --git a/FindGUROBI.cmake b/FindGUROBI.cmake deleted file mode 100644 index 21a1a93..0000000 --- a/FindGUROBI.cmake +++ /dev/null @@ -1,73 +0,0 @@ -# ------------------------------------------------------------------------------ -# This file sets up Gurobi for CMake. Once done this will define -# -# GUROBI_FOUND - system has GUROBI -# GUROBI_INCLUDE_DIRS - the GUROBI include directories -# GUROBI_LIBRARIES - Link these to use GUROBI -# -# In your CMakeLists file, you need to add, e.g. (modify it if necessary): -# if (GUROBI_FOUND) -# message(STATUS "Gurobi include dir: " ${GUROBI_INCLUDE_DIRS}) -# message(STATUS "Gurobi libraries: " ${GUROBI_LIBRARIES}) -# target_compile_definitions(${PROJECT_NAME} PUBLIC HAS_GUROBI) -# target_include_directories(${PROJECT_NAME} PRIVATE ${GUROBI_INCLUDE_DIRS}) -# target_link_libraries(${PROJECT_NAME} PRIVATE ${GUROBI_LIBRARIES}) -# endif() -# ------------------------------------------------------------------------------ - - -# Is it already configured? -if (NOT GUROBI_FOUND) - - # Hardcoded search paths - set(SEARCH_PATHS_FOR_HEADERS - "$ENV{GUROBI_HOME}/include" - "/Library/gurobi1001/mac64/include" - "C:\\gurobi1001\\win64\\include" - ) - - set(SEARCH_PATHS_FOR_LIBRARIES - "$ENV{GUROBI_HOME}/lib" - "/Library/gurobi1001/mac64/lib" - "C:\\gurobi1001\\win64\\lib" - ) - - find_path(GUROBI_INCLUDE_DIR gurobi_c++.h - PATHS ${SEARCH_PATHS_FOR_HEADERS} - ) - - - find_library( GUROBI_C_LIBRARY - NAMES gurobi100 gurobi105 libgurobi - PATHS ${SEARCH_PATHS_FOR_LIBRARIES} - ) - - find_library( GUROBI_CXX_LIBRARY_DEBUG - NAMES gurobi_c++ gurobi_c++mdd2017 - PATHS ${SEARCH_PATHS_FOR_LIBRARIES} - ) - - find_library( GUROBI_CXX_LIBRARY_RELEASE - NAMES gurobi_c++ gurobi_c++md2017 - PATHS ${SEARCH_PATHS_FOR_LIBRARIES} - ) - - # setup header file directories - set(GUROBI_INCLUDE_DIRS ${GUROBI_INCLUDE_DIR}) - - # setup libraries files - set(GUROBI_LIBRARIES - debug ${GUROBI_CXX_LIBRARY_DEBUG} - optimized ${GUROBI_CXX_LIBRARY_RELEASE} - ${GUROBI_C_LIBRARY} - ) - -endif() - -# Check that Gurobi was successfully found -include(FindPackageHandleStandardArgs) -find_package_handle_standard_args(GUROBI DEFAULT_MSG GUROBI_INCLUDE_DIRS) -find_package_handle_standard_args(GUROBI DEFAULT_MSG GUROBI_LIBRARIES) - -# Hide variables from CMake-Gui options -mark_as_advanced(GUROBI_LIBRARIES GUROBI_INCLUDE_DIRS GUROBI_INCLUDE_DIR) diff --git a/inc/node.hpp b/inc/node.hpp deleted file mode 100644 index e58aa9d..0000000 --- a/inc/node.hpp +++ /dev/null @@ -1,17 +0,0 @@ -#pragma once -#include - -namespace sv { - class Var; - class Model; - class Node - { - public: - Model* mdl; - double lower_bound; - double upper_bound; - - bool is_integer; - std::vector branch_list; - }; -} diff --git a/src/solver.cpp b/src/solver.cpp index 971270a..bb7be80 100644 --- a/src/solver.cpp +++ b/src/solver.cpp @@ -6,6 +6,9 @@ #include #include #include +#include +#include +#include using namespace sv; using std::make_pair; @@ -64,14 +67,14 @@ LinSolver& sv::LinSolver::operator=(const LinSolver& solver) if (this == &solver) { return *this; } - delete[] vars; // ÊÍ·ÅÔ­ÓÐÄÚ´æ + delete[] vars; // �ͷ�ԭ���ڴ� vars = nullptr; cn = solver.cn; if (cn > 0) { vars = new Var[cn]; for (int i = 0; i < cn; i++) { - vars[i] = solver.vars[i]; // É±´ + vars[i] = solver.vars[i]; // ��� } } table = solver.table; @@ -101,7 +104,7 @@ Var* LinSolver::addVars(int num, VarType type) const Var& sv::LinSolver::getVar(int idx) { - assert(idx >= 0 && idx < cn); // Ìí¼ÓÔ½½ç¼ì²é + assert(idx >= 0 && idx < cn); // ����Խ���� return vars[idx]; } @@ -203,7 +206,11 @@ rtn Model::optimize() std::stack list_; - Node root_node(solver, 0, solver.obj_); + Node root_node; + root_node.solver = solver; + root_node.lower_bound = 0; + root_node.upper_bound = solver.obj_; + Node incumbent_node = root_node; list_.push(root_node); @@ -325,7 +332,7 @@ rtn LinSolver::feasible_solution() basic.push_back(cn - bn + i); } - // === Åжϳõʼ½âÊÇ·ñΪ¿ÉÐнâ === + // === �жϳ�ʼ���Ƿ�Ϊ���н� === bool initial_feasible = true; for (int row = 1; row < bn; row++) { if (ope_table.at(row).front() < 0) { @@ -334,7 +341,7 @@ rtn LinSolver::feasible_solution() } } - // === ¹¹Ôì³õʼ¿ÉÐнâ === + // === �����ʼ���н� === if (!initial_feasible) { vector coeff = ope_table.front(); ope_table.front() = vector(cn, .0); @@ -386,10 +393,10 @@ rtn LinSolver::feasible_solution() rtn LinSolver::_pivot(pair& p) { p = make_pair(0, 0); - double cmin = INT_MAX; + double cmin = DBL_MAX; vector coef = ope_table.front(); - // === ·ÇÖ÷ÖáÔªËØÖÐÕÒ×îСֵ === + // === ������Ԫ��������Сֵ === for (size_t col = 1; col < coef.size(); col++) { if (cmin > coef.at(col) && find(basic.begin(), basic.end(), col) == basic.end()) { cmin = coef.at(col); @@ -399,7 +406,7 @@ rtn LinSolver::_pivot(pair& p) if (cmin >= 0) { return OPTIMAL; } - double bmin = INT_MAX; + double bmin = DBL_MAX; for (size_t row = 1; row < bn; row++) { double tmp = ope_table.at(row).front() / ope_table.at(row).at(p.second); if (ope_table.at(row).at(p.second) > 0 && bmin > tmp) { @@ -408,7 +415,7 @@ rtn LinSolver::_pivot(pair& p) } } - if (abs(bmin - INT_MAX) < 1e-10) { + if (abs(bmin - DBL_MAX) < 1e-10) { return UNBOUNDED; } @@ -426,13 +433,13 @@ void LinSolver::_gaussian(pair p) { size_t x = p.first, y = p.second; - // === Ö÷ÐйéÒ»»¯ === + // === ���й�һ�� === double norm = ope_table.at(x).at(y); for (size_t col = 0; col < ope_table.at(x).size(); col++) { ope_table.at(x).at(col) /= norm; } - // === ÆäÓàÐб任 === + // === �����б任 === for (size_t row = 0; row < bn; row++) { if (row == x) { continue; @@ -444,6 +451,6 @@ void LinSolver::_gaussian(pair p) } } } - basic.at(x - 1) = y; // »»Ôª + basic.at(x - 1) = y; // ��Ԫ }