干货 | 10分钟掌握branch and cut算法原理附带C++求解TSP问题代码

简介: 干货 | 10分钟掌握branch and cut算法原理附带C++求解TSP问题代码

branch and cut其实还是和branch and bound脱离不了干系的。所以,在开始本节的学习之前,请大家还是要务必掌握branch and bound算法的原理

微信图片_20220422143035.gif


01 应用背景


Branch and cut is a method of combinatorial optimization for solving integer linear programs (ILPs), that is, linear programming (LP) problems where some or all the unknowns are restricted to integer values.
Branch and cut involves running a branch and bound algorithm and using cutting planes to tighten the linear programming relaxations.
Note that if cuts are only used to tighten the initial LP relaxation, the algorithm is called cut and branch.[1]


02 总体描述


前面说过,branch and cut其实还是和branch and bound脱离不了干系。其实是有很大干系的。在应用branch and bound求解整数规划问题的时候,如下图(好好复习一下该过程):

微信图片_20220422143042.png


假如,我们现在求一个整数规划最大化问题,在分支定界过程中,求解整数规划模型的LP松弛模型得到的非整数解作为上界,而此前找到的整数解作为下界。如果出现某个节点upper bound低于现有的lower bound,则可以剪掉该节点。否则,如果不是整数解继续分支。


此外,在求解整数规划模型的LP松弛时,If cutting planes are used to tighten LP relaxations。那么这时候的branch and bound就变成了branch and cut。


那么,什么是cutting planes呢?如下图:


微信图片_20220422143045.png


红色部分是整数规划的可行解空间。

蓝色部分是整数规划的LP松弛可行解空间。

在求解LP松弛时,加入橙色的cut,缩小解空间,同时又不影响整数解的解空间,可使解收敛得更快。


这就是branch and cut的过程了。比branch and bound高明之处就在于多了一个cutting planes,可能使branch and bound的效率变得更高。至于cutting planes是什么,等下一篇推文吧~


03 举个例子


为了让大家更好了解到branch and cut的精髓,必须得举一个简单的例子。对于同一个问题:

微信图片_20220422143047.png

branch and cut(左)和branch and bound(右)求解过程如下:


微信图片_20220422143050.jpg


可以看到,两者的不同之处就在子问题P2的处理上。


  • 对于branch and bound来说,求解线性松弛得到的Z = -29.5 < Z = -28。可知该支是可能隐含有更优解的,于是二话不说分支。无奈,分了两支以后发现居然没更优解,这种付出了却没有回报的感觉就像是受到了欺骗一样。

微信图片_20220422143053.jpg


  • 对于branch and cut来说,在求解线性松弛得到的Z = -29.5 < Z = -28时,并没有被兴奋冲昏头脑,它尝试着在线性松弛的解空间上砍下一块,但又不能影响到整数解的解空间范围。琢磨半天终于找到一块能砍的,于是Add cut: 2x1 + x2 <= 7。砍下来以后,形成新的子问题P3,赶紧看看P3的最优解是多少。在P3中,Z=-27.8 > -28,这一支果然不可取。


从上面的算法过程我们可以看到,求解同一个问题,branch and cut只用了3步,而branch and bound却用了4步。


There are many methods to solve the mixed-integer linear programming. Gomory Cutting Planes is fast, but unreliable. Branch and Bound is reliable but slow. The Branch and cut combine the advantages from these two methods and improve the defects.


It has proven to be a very successful approach for solving a wide variety of integer programming problems.


We can solve the MILP by taking some cutting planes before apply whole system to the branch and bound, Branch and cut is not only reliable, but faster than branch and bound alone. Finally, we understand that using branch and cut is more efficient than using branch and bound.[2]


04 算法过程


关于branch and cut的过程,可以总结如下:[1]


微信图片_20220422143055.png


相比branch and bound,其多了一个Cutting Planes的过程,先用Cutting Planes tighten LP relaxations,然后求解LP relaxations再判断是否有分支的必要。


其伪代码如下:

// ILP branch and cut solution pseudocode, assuming objective is to be maximized
ILP_solution branch_and_cut_ILP(IntegerLinearProgram initial_problem) {
    queue active_list; // L, above
    active_list.enqueue(initial_problem); // step 1
    // step 2
    ILP_solution optimal_solution; // this will hold x* above
    double best_objective = -std::numeric_limits<double>::infinity; // will hold v* above
    while (!active_list.empty()) { // step 3 above
        LinearProgram& curr_prob = active_list.dequeue(); // step 3.1
        do { // steps 3.2-3.7
            RelaxedLinearProgram& relaxed_prob = LP_relax(curr_prob); // step 3.2
            LP_solution curr_relaxed_soln = LP_solve(relaxed_prob); // this is x above
            bool cutting_planes_found = false;
            if (!curr_relaxed_soln.is_feasible()) { // step 3.3
                continue; // try another solution; continues at step 3
            }
            double current_objective_value = curr_relaxed_soln.value(); // v above
            if (current_objective_value <= best_objective) { // step 3.4
                continue; // try another solution; continues at step 3
            }
            if (curr_relaxed_soln.is_integer()) { // step 3.5
                best_objective = current_objective_value;
                optimal_solution = cast_as_ILP_solution(curr_relaxed_soln);
                continue; // continues at step 3
            }
            // current relaxed solution isn't integral
            if (hunting_for_cutting_planes) { // step 3.6
                violated_cutting_planes = search_for_violated_cutting_planes(curr_relaxed_soln);
                if (!violated_cutting_planes.empty()) { // step 3.6
                    cutting_planes_found = true; // will continue at step 3.2
                    for (auto&& cutting_plane : violated_cutting_planes) {
                        active_list.enqueue(LP_relax(curr_prob, cutting_plane));
                    }
                    continue; // continues at step 3.2
                }
            }
            // step 3.7: either violated cutting planes not found, or we weren't looking for them
            auto&& branched_problems = branch_partition(curr_prob);
            for (auto&& branch : branched_problems) {
                active_list.enqueue(branch);
            }
            continue; // continues at step 3
        } while (hunting_for_cutting_planes /* parameter of the algorithm; see 3.6 */
               && cutting_planes_found);
        // end step 3.2 do-while loop
    } // end step 3 while loop
    return optimal_solution; // step 4
}


相关代码


关于branch and cut 求解TSP问题的代码,请关注公众号【程序猿声】,在后台回复【bctsp】不包括【】即可获取。代码用了Gurobi,编译前配置好,不要出问题满世界跑火车说代码有问题。至于Gurobi,有时间再出教程吧。


注:对文中或者代码有疑问可联系小编,可提供有偿辅导服务。

// tsp.cc - traveling salesman code based on Gurobi using branch and cut
#include <string>
#include <iostream>
#include <iomanip>
#include <vector>
#include <cmath>
#include <chrono>
#include <vector>
#include <deque>
#include <limits>
// Gurobi
#include "gurobi_c++.h"
// Data structure to represent an edge of the input graph
struct Edge
{
  int end1;
  int end2;
  double weight;
};
// If memory is limited, a soft limit for the maximum number of LPs in the
// queue can be set for the branch and cut algorithm.
// This is effectively disabled by default.
const int lp_soft_limit = 1000000;
// Tolerance to determine whether or not a number is integral
const double tol = 1.0e-9;
// Auxiliary function to decide whether or not a number is integral
inline bool is_integral(double d)
{
  return std::fabs(d - std::round(d)) < tol;
}
void find_components(int                       num_nodes,
                     int                       num_edges,
                     std::vector<Edge>         edges,
                     const std::vector<double> &x,
                     int                       &num_components,
                     std::vector<int>          &components)
{
  // Mark all unassigned nodes with -1.
  components.resize(num_nodes);
  for (int n = 0; n < num_nodes; n++) {
    components[n] = -1;
  }
  // Component index
  int c = 0;
  // Node indices
  int n1, n2;
  // Find all connected components.
  while (true) {
    // Find an unassigned node.
    n1 = -1;
    for (int n = 0; n < num_nodes; n++) {
      if (components[n] == -1) {
        n1 = n;
        break;
      }
    }
    if (n1 == -1) {
      // All nodes have been assigned.
      break;
    }
    // Assign node to current component.
    components[n1] = c;
    // Mark the entire connected component.
    while (true) {
      // Find an unassigned connected node.
      n2 = -1;
      for (int e = 0; e < num_edges; e++) {
        // Skip edges that are not used in the current solution.
        if (x[e] < tol) continue;
        if (edges[e].end1 == n1 && components[edges[e].end2] == -1) {
          n2 = edges[e].end2;
          break;
        }
        if (edges[e].end2 == n1 && components[edges[e].end1] == -1) {
          n2 = edges[e].end1;
          break;
        }
      }
      // No connected node found. Continue with next component.
      if (n2 == -1) break;
      // Assign the connected node to the current component.
      components[n2] = c;
      // Merge the two nodes.
      for (int e = 0; e < num_edges; e++) {
        if (edges[e].end1 == n2) {
          edges[e].end1 = n1;
        }
        if (edges[e].end2 == n2) {
          edges[e].end2 = n1;
        }
      }
    }
    c++;
  }
  num_components = c;
}
void solve_TSP(int                     num_nodes,
               int                     num_edges,
               const std::vector<Edge> &edges,
               bool                    integral_weights,
               std::vector<double>     &x_opt,
               int                     &lp_solves,
               int                     &subtour_constraints)
{
  // Allocate memory for the solution.
  std::vector<double> x;
  x.resize(num_edges);
  // Set up environment.
  GRBEnv env;
  // Create initial model.
  GRBModel initial_model(env);
  // Add variables.
  initial_model.addVars(num_edges, GRB_CONTINUOUS);
  initial_model.update();
  GRBVar *vars = initial_model.getVars();
  // Set up objective function.
  GRBLinExpr obj;
  for (int e = 0; e < num_edges; e++) {
    obj += GRBLinExpr(vars[e], edges[e].weight);
  }
  initial_model.setObjective(obj, GRB_MINIMIZE);
  // Add initial constraints.
  for (int n = 0; n < num_nodes; n++) {
    GRBLinExpr lhs;
    // Add all edges that are adjacent to the current node.
    for (int e = 0; e < num_edges; e++) {
      if (edges[e].end1 == n || edges[e].end2 == n) {
        lhs += GRBLinExpr(vars[e], 1.0);
      }
    }
    initial_model.addConstr(lhs, GRB_EQUAL, GRBLinExpr(2.0));
  }
  for (int e = 0; e < num_edges; e++) {
    initial_model.addConstr(GRBLinExpr(vars[e], 1.0),
                            GRB_LESS_EQUAL,
                            GRBLinExpr(1.0));
  }
  initial_model.update();
  lp_solves = 0;
  subtour_constraints = 0;
  // Branch and cut.
  double cost;
  double cost_opt = std::numeric_limits<double>::infinity();
  int num_components;
  std::vector<int> components;
  std::deque<GRBModel> problems;
  problems.push_back(initial_model);
  while (problems.size() > 0) {
    // Get next problem in queue.
    GRBModel model = problems.front();
    problems.pop_front();
    // In this loop, the LP is solved repeatedly until a solution without
    // subtours is found.
    bool skipped = false;
    while (true) {
      // Solve current model.
      model.optimize();
      lp_solves++;
      if (model.get(GRB_IntAttr_Status) != GRB_OPTIMAL) {
        // Do not continue branch if problem is infeasible.
        skipped = true;
        break;
      }
      // Check cost. If it is too high, stop following this branch.
      cost = model.get(GRB_DoubleAttr_ObjVal);
      // We can cut off branches more aggressively when the weights,
      // and thus the optimal cost, are integral.
      if (integral_weights && cost > cost_opt - 1.0 + tol) {
          skipped = true;
          break;
      }
      if (cost > cost_opt) {
        skipped = true;
        break;
      }
      // Get current solution.
      delete[] vars;
      vars = model.getVars();
      for (int e = 0; e < num_edges; e++) {
        x[e] = vars[e].get(GRB_DoubleAttr_X);
      }
      // Find connected components of the solution and eliminate subtours.
      find_components(num_nodes,
                      num_edges,
                      edges,
                      x,
                      num_components,
                      components);
      if (num_components == 1) {
        // There are no more subtours that could be eliminated.
        break;
      }
      // We will add one constraint per connected component.
      std::vector<GRBLinExpr> lhs;
      lhs.resize(num_components);
      for (int e = 0; e < num_edges; e++) {
        // Identify the component this edge belongs to.
        const int c1 = components[edges[e].end1];
        const int c2 = components[edges[e].end2];
        // Skip edges that connect two different components.
        if (c1 != c2) continue;
        // Add edge to the subtour elimination constraint.
        lhs[c1] += GRBLinExpr(vars[e], 1.0);
      }
      // Compute the size of each component.
      // This is required for the right-hand side of the constraints.
      std::vector<int> component_sizes;
      component_sizes.resize(num_components);
      for (int c = 0; c < num_components; c++) {
        component_sizes[c] = 0;
      }
      for (int n = 0; n < num_nodes; n++) {
        component_sizes[components[n]]++;
      }
      // Add constraints to model.
      for (int c = 0; c < num_components; c++) {
        model.addConstr(lhs[c],
                        GRB_LESS_EQUAL,
                        GRBLinExpr(component_sizes[c] - 1.0));
        subtour_constraints++;
      }
      std::cout << "Added " << num_components
                << " subtour elimination constraints." << std::endl;
      model.update();
    }
    if (skipped) continue;
    // Branch using a fractional variable.
    bool integral_sol = true;
    for (int e = 0; e < num_edges; e++) {
      if (!is_integral(x[e])) {
        integral_sol = false;
        // Add model with <= constraint for fractional variable.
        GRBModel model_le(model);
        model_le.addConstr(GRBLinExpr(vars[e], 1.0),
                           GRB_LESS_EQUAL,
                           GRBLinExpr(std::floor(x[e])));
        model_le.update();
        // Add model with >= constraint for fractional variable.
        GRBModel model_ge(model);
        model_ge.addConstr(GRBLinExpr(vars[e], 1.0),
                           GRB_GREATER_EQUAL,
                           GRBLinExpr(std::ceil(x[e])));
        model_ge.update();
        // Check if the soft limit for the number of LPs is hit.
        if (problems.size() < lp_soft_limit) {
          // Add new problems at the end of the queue.
          // This corresponds to breadth-first search.
          problems.push_back(model_le);
          problems.push_back(model_ge);
        }
        else {
          // Add new problems at the beginning of the queue.
          // This corresponds to depth-first search.
          problems.push_front(model_le);
          problems.push_front(model_ge);
        }
        // Print information about the queue.
        std::cout << "Branching; there are now "
                  << problems.size()
                  << " models in the queue."
                  << std::endl << std::endl;
        // Stop after creating one branch!
        break;
      }
    }
    // Update optimal cost and optimal solution if integral solution was found.
    if (integral_sol) {
      cost_opt = cost;
      x_opt = x;
    }
  }
}
int main(int argc, char **argv)
{
  // Read problem from stdin.
  std::string line;
  // Read problem size.
  std::getline(std::cin, line);
  // Remove leading spaces.
  while (line[0] == ' ') line = line.substr(1);
  const int num_nodes = std::stoi(line.substr(0, line.find(" ")));
  const int num_edges = std::stoi(line.substr(line.find(" ") + 1));
  // Read graph.
  std::vector<Edge> edges;
  edges.resize(num_edges);
  for (int e = 0; e < num_edges; e++) {
    std::getline(std::cin, line);
    // Remove leading spaces.
    while (line[0] == ' ') line = line.substr(1);
    edges[e].end1 = std::stoi(line.substr(0, line.find(" ")));
    line = line.substr(line.find(" ") + 1);
    edges[e].end2 = std::stoi(line.substr(0, line.find(" ")));
    line = line.substr(line.find(" ") + 1);
    edges[e].weight = std::stod(line);
  }
  std::cout << "Loaded TSP with " << num_nodes << " nodes and "
                                  << num_edges << " edges.\n";
  // Check if the edge weights are integral.
  // If so, we can optimize a bit more aggressively in some places.
  bool integral_weights = true;
  for (int e = 0; e < num_edges; e++) {
    if (!is_integral(edges[e].weight)) {
      integral_weights = false;
      break;
    }
  }
  std::cout << "Computation begins.\n";
  // Start timer.
  const auto t_start = std::chrono::high_resolution_clock::now();
  // Solve TSP using Gurobi (for the LPs).
  std::vector<double> x_opt;
  int lp_solves;
  int subtour_constraints;
  try {
    solve_TSP(num_nodes,
              num_edges,
              edges,
              integral_weights,
              x_opt,
              lp_solves,
              subtour_constraints);
  }
  catch (const GRBException &e) {
    std::cerr << "Gurobi exception: " << e.getMessage() << std::endl;
    std::exit(1);
  }
  // Stop timer.
  const auto t_end = std::chrono::high_resolution_clock::now();
  const std::chrono::duration<double> dtime = t_end - t_start;
  std::cout << "Computation finished (" 
            << std::fixed << std::setprecision(3)
            << dtime.count() << "s).\n";
  // Print additional information.
  std::cout << "Solved a total of " << lp_solves << " LPs." << std::endl;
  std::cout << "Added a total of " << subtour_constraints
            << " subtour elimination constraints." << std::endl;
  // Print optimal solution.
  std::cout << "The best tour is:\n";
  double c_optimal = 0.0;
  // Set output format.
  if (integral_weights) {
    std::cout << std::setprecision(0);
  }
  else {
    std::cout << std::setprecision(1);
  }
  for (int e = 0; e < num_edges; e++) {
    // See if the edge is used.
    if (x_opt[e] > 0.0) {
      std::cout << edges[e].end1 << " "
                << edges[e].end2 << " "
                << edges[e].weight << std::endl;
      c_optimal += x_opt[e]*edges[e].weight;
    }
  }
  std::cout << "The cost of the best tour is: " << c_optimal << std::endl;
  return 0;
}

reference


  • [1]  (https://en.wikipedia.org/wiki/Branch_and_cut)
  • [2] (https://optimization.mccormick.northwestern.edu/index.php/Branch_and_cut)


相关文章
|
14天前
|
算法 容器
令牌桶算法原理及实现,图文详解
本文介绍令牌桶算法,一种常用的限流策略,通过恒定速率放入令牌,控制高并发场景下的流量,确保系统稳定运行。关注【mikechen的互联网架构】,10年+BAT架构经验倾囊相授。
令牌桶算法原理及实现,图文详解
|
23天前
|
负载均衡 算法 应用服务中间件
5大负载均衡算法及原理,图解易懂!
本文详细介绍负载均衡的5大核心算法:轮询、加权轮询、随机、最少连接和源地址散列,帮助你深入理解分布式架构中的关键技术。关注【mikechen的互联网架构】,10年+BAT架构经验倾囊相授。
5大负载均衡算法及原理,图解易懂!
|
12天前
|
算法
分享一些提高二叉树遍历算法效率的代码示例
这只是简单的示例代码,实际应用中可能还需要根据具体需求进行更多的优化和处理。你可以根据自己的需求对代码进行修改和扩展。
|
17天前
|
存储 算法 决策智能
基于免疫算法的TSP问题求解matlab仿真
旅行商问题(TSP)是一个经典的组合优化问题,目标是寻找经过每个城市恰好一次并返回起点的最短回路。本文介绍了一种基于免疫算法(IA)的解决方案,该算法模拟生物免疫系统的运作机制,通过克隆选择、变异和免疫记忆等步骤,有效解决了TSP问题。程序使用MATLAB 2022a版本运行,展示了良好的优化效果。
|
23天前
|
算法 测试技术 开发者
在Python开发中,性能优化和代码审查至关重要。性能优化通过改进代码结构和算法提高程序运行速度,减少资源消耗
在Python开发中,性能优化和代码审查至关重要。性能优化通过改进代码结构和算法提高程序运行速度,减少资源消耗;代码审查通过检查源代码发现潜在问题,提高代码质量和团队协作效率。本文介绍了一些实用的技巧和工具,帮助开发者提升开发效率。
25 3
|
22天前
|
分布式计算 Java 开发工具
阿里云MaxCompute-XGBoost on Spark 极限梯度提升算法的分布式训练与模型持久化oss的实现与代码浅析
本文介绍了XGBoost在MaxCompute+OSS架构下模型持久化遇到的问题及其解决方案。首先简要介绍了XGBoost的特点和应用场景,随后详细描述了客户在将XGBoost on Spark任务从HDFS迁移到OSS时遇到的异常情况。通过分析异常堆栈和源代码,发现使用的`nativeBooster.saveModel`方法不支持OSS路径,而使用`write.overwrite().save`方法则能成功保存模型。最后提供了完整的Scala代码示例、Maven配置和提交命令,帮助用户顺利迁移模型存储路径。
|
28天前
|
缓存 分布式计算 监控
优化算法和代码需要注意什么
【10月更文挑战第20天】优化算法和代码需要注意什么
18 0
|
6天前
|
存储 编译器 C++
【c++】类和对象(下)(取地址运算符重载、深究构造函数、类型转换、static修饰成员、友元、内部类、匿名对象)
本文介绍了C++中类和对象的高级特性,包括取地址运算符重载、构造函数的初始化列表、类型转换、static修饰成员、友元、内部类及匿名对象等内容。文章详细解释了每个概念的使用方法和注意事项,帮助读者深入了解C++面向对象编程的核心机制。
28 5
|
12天前
|
存储 编译器 C++
【c++】类和对象(中)(构造函数、析构函数、拷贝构造、赋值重载)
本文深入探讨了C++类的默认成员函数,包括构造函数、析构函数、拷贝构造函数和赋值重载。构造函数用于对象的初始化,析构函数用于对象销毁时的资源清理,拷贝构造函数用于对象的拷贝,赋值重载用于已存在对象的赋值。文章详细介绍了每个函数的特点、使用方法及注意事项,并提供了代码示例。这些默认成员函数确保了资源的正确管理和对象状态的维护。
40 4
|
13天前
|
存储 编译器 Linux
【c++】类和对象(上)(类的定义格式、访问限定符、类域、类的实例化、对象的内存大小、this指针)
本文介绍了C++中的类和对象,包括类的概念、定义格式、访问限定符、类域、对象的创建及内存大小、以及this指针。通过示例代码详细解释了类的定义、成员函数和成员变量的作用,以及如何使用访问限定符控制成员的访问权限。此外,还讨论了对象的内存分配规则和this指针的使用场景,帮助读者深入理解面向对象编程的核心概念。
37 4