Skip to content

AssertionError when building model #212

@cmjdxy

Description

@cmjdxy

"AssertionError" is raised when I run my program. I have checked my code time and again, but I just cannot find the bug. Can anyone help me out? You have my gratitude.
The code:

import numpy as np
from pyscipopt import Model, quicksum

def QP_model(A, w, c, name='QP'):
    '''
    min : x'Ax/2
    s.t.: w'x <= c, 0 <= x <= 1
    '''
    n = len(A)
    model = Model(name)
    x = dict()
    for i in range(n):
        x[i] = model.addVar('x(%d)' % i, vtype='C', lb=0.0, ub=1.0)
    model.addCons(quicksum(w[i]*x[i] for i in range(n)) <= c)
    objfun = quicksum(A[i][j]*x[i]*x[j]/2 for i, j in np.ndindex(n, n))
    model.setObjective(objfun, 'minimize')
    model.data = x
    return model

def main():
    '''
    Main interface.
    '''
    # data
    A = np.random.rand(3, 3)
    w = np.random.rand(3)
    c = np.random.rand()
    # model
    model = QP_model(A, w, c)
    model.optimize()

if __name__ == '__main__':
    print('running...')
    main()

The error message:

Traceback (most recent call last):

  File "<ipython-input-50-4fc84a02b499>", line 1, in <module>
    runfile('D:/Essays/QMKP/example/temp.py', wdir='D:/Essays/QMKP/example')

  File "d:\ProgramData\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 705, in runfile
    execfile(filename, namespace)

  File "d:\ProgramData\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 102, in execfile
    exec(compile(f.read(), filename, 'exec'), namespace)

  File "D:/Essays/QMKP/example/temp.py", line 41, in <module>
    main()

  File "D:/Essays/QMKP/example/temp.py", line 35, in main
    model = QP_model(A, w, c)

  File "D:/Essays/QMKP/example/temp.py", line 21, in QP_model
    model.setObjective(objfun, 'minimize')

  File "src\pyscipopt\scip.pyx", line 779, in pyscipopt.scip.Model.setObjective

AssertionError

N.B. I am using Anaconda3 and Windows 10.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions