• How to solve 0/1-QP by Gurobi


    I will introduce how to use gurobi to solve a QP with 0/1 variables on python env.

    Gurobi is a canonical solver to solve integer programming. I postulate you have mastered basic operations for python. This blog mainly contains three part:

    1) Present QP with 0/1 variables;

    2) Introduce how to install Gurobi and apply Academic license;

    3) Give a specific code to solve given problem.

    Now, let's begin.

    I Problem model

    We focus on this QP problem with 0/1 variables(01-QP):

    \min_{x\in R^n} \|Ax-b\|^2\\ s.t., x_i\in\{0,1\}.                                                                  (1)

    The main troble of this problem is 0/1 variables. Strictly, this is a NP hard problem. However, there are many commercial  software that can solve it with heuristic algorithm, such as Gurobi and cplex. By the way, Gurobi is the most famous solver.

    II. How to install Gurobi and apply Academic license

    I recommaned you to use colab provied by Google, since Gurobi is very nicely  embedded in colab. Next we just need to apply a academic license to solve large scale problem.

    Following the instruction step by step of https://support.gurobi.com/hc/en-us/articles/4409582394769-Google-Colab-Installation-and-Licensing

    More specifically, apply an account in https://www.gurobi.com/account/    

    Sign in and click on the following red circle

    Then click on the following red circle 

     Notice that your school maybe unable to allow to get a License sometimes.

    III Sovle it.

    In this part, we solve 01-QP(1) by python use gurobi on colab with m=n=50.

    1. !pip install gurobipy # install gurobipy, if not already installed
    2. import gurobipy as gp
    3. # Create environment with WLS license
    4. import pandas as pd
    5. from gurobipy import GRB
    6. e = gp.Env(empty=True)
    7. e.setParam('WLSACCESSID', '****************')
    8. e.setParam('WLSSECRET', '***************')
    9. e.setParam('LICENSEID', ******)
    10. e.start()
    11. # # Create the model within the Gurobi environment
    12. model = gp.Model(env=e)
    13. m = gp.Model("miqp", env=e)
    14. # m = gp.Model("qp")
    15. import numpy as np
    16. # Create variables
    17. n = 50
    18. np.random.seed(2)
    19. A0 = 10 * np.random.uniform(-2 / 10, 2 / 3, size=(n, n))
    20. A = np.ceil(A0)
    21. x_or = 10 * np.random.uniform(-5, 5, size=(n, 1))
    22. x_or = np.ceil(x_or)
    23. id_in = (np.where(x_or > 0.5))[0]
    24. x_or[id_in] /= (np.linalg.norm(x_or[id_in], axis = 1)).reshape((len(id_in),1))
    25. x_or *= (x_or > 0)
    26. b = np.ceil(np.dot(A, x_or))
    27. # A = 10000 * np.array([[2, 0, 9], [7, 1, 10], [0, 1, 1]])
    28. # b0 = 10000 * np.array([9, 11, 2])
    29. Q = np.dot(A.T, A)
    30. b1 = np.dot(b.T, A)
    31. x = m.addMVar(n, vtype=GRB.BINARY)
    32. print("Test{}.".format(np.dot(b.T, b)))
    33. obj = x @ Q @ x - 2 * b1 @ x + np.dot(b.T, b)
    34. m.setObjective(obj, gp.GRB.MINIMIZE)
    35. m.update()
    36. # # Add constraint: x + 2 y + 3 z >= 4
    37. # m.addConstr(x + 2 * y + 3 * z >= 4, "c0")
    38. # # Add constraint: x + y >= 1
    39. # m.addConstr(x + y >= 1, "c1")
    40. m.optimize()
    41. print('Obj: %g' % obj.getValue())
    42. x_best = (x.x).reshape((n,1))
    43. print("error:{}.".format(np.linalg.norm(x_or - x_best)))

    That's the end. Have a good day, dude.

  • 相关阅读:
    深入JVM:探索Java虚拟机
    【软考:系统集成项目管理】之 项目干系人管理
    Ubuntu中不能使用ifconfig命令
    kubenates的傻瓜式部署教程(K8S部署教程)
    Metabase学习教程:模型-2
    yolo配置(windows)
    【MySQL】并发事务产生的问题及事务隔离级别
    数仓建模—埋点设计与管理
    腾讯面试 Java 高频 210 题解析:Spirng+ 设计模式 +Redis+MySQL
    基于Springboot的网课管理系统
  • 原文地址:https://blog.csdn.net/nobles007820/article/details/127117047