CplexSolverError:CPLEX错误1222:重复的条目或条目

时间:2017-07-15 09:52:59

标签: python-2.7 cplex

我是CPLEX-Python集成的新手。我在标题中写了Cplex-Python集成的错误。我的代码如下。错误源是最后一个约束集。我愿意接受任何建议。 提前致谢。 Mathematical formulation of the last constraint set

#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
Created on Sun Jul  9 10:06:38 2017

@author: nazmisener
"""
from __future__ import print_function
import pandas as pd
import cplex
from cplex.exceptions import CplexSolverError
import numpy as np
import xlrd


noph=25
noz=[ 25 for i in range(noph) ] 
#acquiring data from excel
dem = [ [ 1000 for i in range(noph) ] for j in range(noph) ]

fc = [ [ 0.5 for i in range(noph) ] for j in range(noph) ]

c=[ [ 0 for i in range(noph) ] for j in range(noph) ]
for i in range(noph):
    for k in range(noph):
        c[i][k]=fc[i][k]/25000

hoc=[ 10000 for i in range(noph) ] 
#determining link with maximum length
maxar=max(fc)
maxlin=max(maxar)

#coverage percentage
perc=0.7
meas=perc*maxlin


A=[ [ 0 for i in range(noph) ] for j in range(noph) ]
objt=[ [ 0 for i in range(noph) ] for j in range(noph) ]
#calculating matrix A
for d1 in range(noph):
    for d2 in range(noph):
        if meas<fc[d1][d2] or d1==d2:
            A[d1][d2]=0
        else:
            A[d1][d2]=1

O=[ 0 for i in range(noph) ]
D=[ 0 for i in range(noph) ]
for i in range(noph):
    for j in range(noph):
        O[i]=+dem[i][j]
        D[i]=+dem[j][i]

for i in range(noph):
    for k in range(noph):
        if i!=k:
            objt[i][k] = c[i][k]*(O[i]+D[i])   
        else:
            objt[i][k]=0


def dethub():
    model=cplex.Cplex()
    model.objective.set_sense(model.objective.sense.minimize)

#defining decision variables
    x=["x{0}".format(i+1) for i in range(noph)]
    model.variables.add( obj=hoc, lb=[0.0]*noph, ub=[1.0] *noph,
                            types=["B"] *noph , names=x )

    z=[]
    for i in range(noph):
        z.append([])
        for j in range(noph):
            varName = "z." + str(i) + "." + str(j)
            z[i].append(model.variables.get_num())
            model.variables.add(obj=[objt[i][j]],
                              lb=[0.0], ub=[1.0], 
                              names=[varName])

#    colname_y = ["y{0}{1}{2}".format(i+1,j+1,z+1) for i in range(noph) 
#    for j in range(noph) for z in range(noph)]
#    model.variables.add(obj=c)
    y = []
    for i in range(noph):
        y.append([])
        for k in range(noph):
            y[i].append([])
            for j in range(noph):
                yvarName = "y." + str(i) + "." + str(k) + "." + str(j)
                y[i][k].append(model.variables.get_num())
                model.variables.add(obj=[c[i][k]],
                                     lb=[0.0],
                                     ub=[cplex.infinity],
                                     names=[yvarName])

#defining constraints
#the coverage constraints
    for i in range(noph):
        for k in range(noph):
            thevars=[]
            thecoefs=[]
            thevars.append(z[i][k])
            thecoefs.append(1)
            thevars.append(A[i][k]*z[k][k])
            thecoefs.append(-1)
            model.linear_constraints.add(
                            lin_expr=[cplex.SparsePair(thevars, thecoefs)],
                            senses=["L"],
                            rhs=[0.0])

#summation of flow constraints
    for i in range(noph):
        summation_constraint = cplex.SparsePair(ind=[z[i][k] for k in
                                                      range(noph)],
                                                 val=[1.0] * noph)
        model.linear_constraints.add(lin_expr=[summation_constraint],
                                     senses=["E"],
                                     rhs=[1])

#flow ensure constraints        
    for i in range(noph):
        for k in range(noph):
            thevarws=[z[i][k]]
            thecoefws=[-O[i]]
            for l in [q for q in xrange(noph) if q!=k]:
                thevarws.append(y[i][k][l])
                thecoefws.append(1)
            model.linear_constraints.add(
                            lin_expr=[cplex.SparsePair(thevarws, thecoefws)],
                            senses=["L"],
                            rhs=[0.0])

#x equality constraints
    for k in range(noph):
        thevarys=[]
        thecoefys=[]
        thevarys.append(z[k][k])
        thecoefys.append(1)
        thevarys.append(x[k])
        thecoefys.append(-1)
        model.linear_constraints.add(
                            lin_expr=[cplex.SparsePair(thevarys, thecoefys)],
                            senses=["E"],
                            rhs=[0.0])

#flow conservation constraints
    for i in range(noph):
        for k in range(noph):
            thevarxs=[z[i][k]]
            thecoefxs=[O[i]]
            for l in [q for q in xrange(noph) if q!=k]:
                thevarxs.append(y[i][k][l])
                thecoefxs.append(-1)
                thevarxs.append(y[i][l][k])
                thecoefxs.append(1)
            for j in range(noph):
                thevarxs.append(z[j][k])
                thecoefxs.append(-dem[k][j])
            model.linear_constraints.add(
                            lin_expr=[cplex.SparsePair(thevarxs, thecoefxs)],
                            senses=["E"],
                            rhs=[0.0]) 

    try:
        model.solve()
    except CplexSolverError as e:
        print("Exception raised during solve: " + e)
    else:
        solution = model.solution

        # solution.get_status() returns an integer code
        print("Solution status = ", solution.get_status(), ":", end=' ')
        # the following line prints the corresponding string
        print(solution.status[solution.get_status()])

        # Display solution.
        print("Total cost = ", solution.get_objective_value())

if __name__ == "__main__":
    dethub()

1 个答案:

答案 0 :(得分:0)

正如错误所述,问题在于您正在添加重复条目。在最后一个约束集中,在调用model.linear_constraints.add之前添加以下行:

print("thevarxs: ", sorted(thevarxs))

有了这个,我们看到以下输出:

thevarxs:  [25, 25, 50, 75, 100, 125, 150, 175, 200, 225, 250, 275, 300, 325, 350, 375, 400, 425, 450, 475, 500, 525, 550, 575, 600, 625, 651, 652, 653, 654, 655, 656, 657, 658, 659, 660, 661, 662, 663, 664, 665, 666, 667, 668, 669, 670, 671, 672, 673, 674, 675, 700, 725, 750, 775, 800, 825, 850, 875, 900, 925, 950, 975, 1000, 1025, 1050, 1075, 1100, 1125, 1150, 1175, 1200, 1225, 1250]

索引 25 有两个条目。如果你解决这个问题,问题应该消失。

为了让自己更轻松,我强烈建议您使用较小版本的模型。然后,当你开始工作时(一步一步),开始尝试更大的模型。