0

I'm trying to solve the MWIS problem exactly using CPLEX. When I try the .populate() I get the error that docplex.mp.utils.DOcplexException: Model.populate_solution_pool only for MILP problems, model 'Exact Solution' is a MIQP.

Is there a way to use the free Python Download of CPLEX to get more than one possible solutions ?


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)
    

This is the code I am using in combination with the solver !

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)
 
jolene
  • 373
  • 2
  • 15
  • 1) cplex says this feature is not available for MIQPs, no matter if free/non-free python or non-python. 2) I would expect a MWIS problem to be formulated as MILP and not MIQP. Are you sure your model is what you want? 3) Your model is MIQP != because of bin-var products. Do you need those? (classic formulation: no!) You could (not that it's always a good idea) linearize this by hand or maybe use a cplex param to linearize this automaticlly which should render the problem an MILP again, such that your feature is back. – sascha Apr 07 '21 at 20:30

1 Answers1

1

When solution pools , calling many solves and removing previous solutions is an option.

Example in 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))

which gives

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
Alex Fleischer
  • 9,276
  • 2
  • 12
  • 15
  • Thanks for this answer, I found a different way (I'm calculating all possible values anyway so can just do a search) ! – jolene Apr 10 '21 at 17:48