【问题标题】:Populate() Equivalent for MIQP in CPLEXCPLEX 中 MIQP 的 Populate() 等效项
【发布时间】:2021-04-07 19:00:34
【问题描述】:

我正在尝试使用 CPLEX 完全解决 MWIS 问题。当我尝试.populate() 时,我收到docplex.mp.utils.DOcplexException: Model.populate_solution_pool only for MILP problems, model 'Exact Solution' is a MIQP 的错误。

有没有办法使用 CPLEX 的免费 Python 下载来获得多个可能的解决方案?


def get_exact_solution(Graph,model_name ='Exact Solution (s)'):


    model = new_docplex_generator(Graph,model_name)
    solution = model.solve() # this works 
    solutions = model.populate() # this doesn't 
    print("Multi?")
    print(solutions)
    

这是我与求解器结合使用的代码!

def weighted_erdos_graph(nodes, prob, seed =None):
    """Generates an erdos graph with weighted nodes
    https://en.wikipedia.org/wiki/Erd%C5%91s%E2%80%93R%C3%A9nyi_model
    Node weights randomly assigned with the same seed as the erdos graph
    """
    graph = nx.erdos_renyi_graph(n=nodes, p =prob, seed=seed, directed=False)
    np.random.seed(seed)
    graph_weights = np.random.randint(1,high=11,size =nodes)
    name = str("Erdos Graph "+str(nodes)+" nodes weighted "+str(list(graph_weights)))

    graph.nodes[0]["graph_name"] = name
    for i in range(0,nodes):
        graph.nodes[i]["node_weight"] = graph_weights[i]
    #print(list(graph.nodes(data=True)))
    return graph

def new_docplex_generator(G,model_name):
    '''

    Takes in a networkx graph with weighted nodes and creates the docplex model for the
    MWIS

    '''
    mdl = Model(model_name)

    n = G.number_of_nodes()
    x = mdl.binary_var_list('x_{}'.format(i) for i in range(n)) #creates list of variables for each node
    node_list = list(G.nodes())
    node_weights = G.nodes(data='node_weight')
    just_weights = [weight[1] for weight in node_weights] #gets just the node weight
    scale = max(just_weights) # used as J_i,j must be greater than weight of node; all node weights are scaled to below 0 and J_ij is put as 2


    edge_list = list(G.edges())

    #node_weight_terms  = mdl.sum([x[i] * -1*(just_weights[i]/scale) for i in node_list])
    node_weight_terms  = mdl.sum([x[i] * -1*(just_weights[i]) for i in node_list])

    edge_indepedence_terms  = mdl.sum([20*x[i]*x[j] for (i,j) in edge_list])
    mdl.minimize(node_weight_terms + edge_indepedence_terms)  # does this need to be minimise ?
    print("auto_docplex_function")
    mdl.prettyprint()

    return mdl


get_exact_solution(unweighted_erdos_graph(7,0.5,seed = 3)
 

【问题讨论】:

  • 1) cplex 表示此功能不适用于 MIQP,无论是免费/非免费 python 还是非 python。 2) 我希望将 MWIS 问题表述为 MILP 而不是 MIQP。你确定你的模型是你想要的吗? 3) 你的模型是 MIQP != 因为 bin-var 产品。你需要那些吗? (经典公式:不!)您可以(并不是说它总是一个好主意)手动对其进行线性化,或者使用 cplex 参数自动对其进行线性化,这应该再次将问题呈现为 MILP,这样您的功能就会恢复。跨度>

标签: python cplex quadratic-programming docplex


【解决方案1】:

当解决方案池时,调用多个解决方案并删除以前的解决方案是一种选择。

https://github.com/AlexFleischerParis/zoodocplex/blob/master/zooenumeratewithoutsolutionpool.py 中的示例

from docplex.mp.model import Model


mdl = Model(name='buses')
nbbus40 = mdl.integer_var(name='nbBus40')
nbbus30 = mdl.integer_var(name='nbBus30')
mdl.add_constraint(nbbus40*40 + nbbus30*30 >= 300, 'kids')
mdl.minimize(nbbus40**2*500 + nbbus30**2*400)

nb_iter=5

for iter in range(0,nb_iter):
    mdl.solve()
    nbbus40sol=int(nbbus40.solution_value)
    nbbus30sol=int(nbbus30.solution_value)
    print(int(nbbus40sol)," buses 40 seats")
    print(int(nbbus30sol)," buses 30 seats")
    print("cost : ",mdl.objective_value)
    print()
    mdl.add_constraint(mdl.logical_or((nbbus40sol!=nbbus40),
            nbbus30sol!=nbbus30))

给了

4  buses 40 seats
5  buses 30 seats
cost :  18000.0
5  buses 40 seats
4  buses 30 seats
cost :  18900.0
3  buses 40 seats
6  buses 30 seats
cost :  18900.0
6  buses 40 seats
2  buses 30 seats
cost :  19600.0
6  buses 40 seats
3  buses 30 seats
cost :  21600.0

【讨论】:

  • 感谢您的回答,我找到了另一种方法(我正在计算所有可能的值,所以可以进行搜索)!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-05-10
  • 2012-07-06
  • 2013-12-16
  • 2018-06-25
  • 2015-07-09
  • 2017-10-24
  • 2014-07-04
相关资源
最近更新 更多