Skip to content

Copy a SCIP Model #1001

@adecholaA1

Description

@adecholaA1

Hello,
I'm encountering an error when trying to copy the scip model (following the directions in the documentation). My aim is to copy the current state of the scip model, then add a constraint and obtain information such as the value of the LP objective function and the status of the model at the end of this resolution independently (without modifying the state of the current scip model). Proceeding as described in the code below, I get the message “zsh: segmentation fault”. Is there an error in the way I'm proceeding? Is there another way of copying the state of a scip model to which to make changes, resolve it and obtain information without modifying the original scip model?

Thank you in advance.

from pyscipopt import Model

class Copy_scip():
    def __init__(self, scip):
        self.scip = scip

    def getInfosUsingCopyScip(self, list_1, rhs):

        # Get back to the original problem 
        self.scip.freeTransform()

        # Model copy
        scip_copy = Model(sourceModel=self.scip)

        # Copy model variables
        cols = scip_copy.getLPColsData()
        scip_vars = [col.getVar() for col in cols]

        # Add constraint
        expr = sum(c*v for c, v in zip(list_1, scip_vars))
        scip_copy.addCons(expr <= rhs)

        #Optimize
        scip_copy.hideOutput()
        scip_copy.optimize()

        status = scip_copy.getStatus()
        lperror = (status=="lperror")
        cutoff = (status == "infeseable")
        LPObj = scip_copy.getLPObjVal() 

        scip_copy.freeProb()

        return LPObj, cutoff, lperror

if __name__=="__main__":
    scip = Model()
    x1 = scip.addVar("x1", vtype="I", lb=0)
    x2 = scip.addVar("x2", vtype="I", lb=0)

    scip.addCons(-x1 - 2*x2 <= -1)
    scip.addCons(-x1 + 2*x2 <= 1)
    scip.addCons(x1  + 2 * x2 <= 3)
    scip.addCons(x1  - 2 * x2 <= 1)

    scip.setObjective(2* x1 - x2, sense="minimize")

    #----------------------------------------

    copy = Copy_scip(scip)
    rhs = 1
    list_1 = [1.2, 2.5]

    LPObj, cutoff, lperror = copy.getInfosUsingCopyScip(list_1, rhs)

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