[SCIP] R: R: R: Quadratic Constraint Modification
Enrico Calandrini
e.calandrini at studenti.unipi.it
Sat Aug 17 13:03:45 CEST 2024
Hi Stefan,
Thank you once again for your invaluable assistance.
I can confirm that using SCIPfreeExprQuadratic() indeed allows the coefficients to be correctly modified! However, I'm still struggling with one final issue: how to properly add or remove terms from a quadratic constraint. I've tried several approaches, but none seem to work. Is this even possible? Or is it that the constraint handler cons_nonlinear doesn’t support such modifications?
Regarding the addition of new coefficients (e.g., a simple term like x^2), I attempted to create a new expression using SCIPcreateExprQuadratic with the new term, and then add it using SCIPappendExprChild(scip, qexpr, new_term), where qexpr is the existing quadratic expression obtained via SCIPgetExprNonlinear and new_term represents the term I want to add. However, when I follow this procedure and attempt to solve, I encounter the following error:
/scipoptsuite-8.0.3/scip/src/scip/solve.c:4920: SCIPsolveCIP: Assertion `BMSgetNUsedBufferMemory(mem->buffer) == 0' failed.
I also tried using SCIPaddExprNonlinear(scip, q_con, new_term, 1), where q_con is the initial quadratic constraint. In this case, I receive this error:
scip/src/scip/cons_nonlinear.c:12632: SCIPaddExprNonlinear: Assertion `consdata->nvarexprs == 0' failed.
On the other hand, I'm unsure how to go about removing terms. Setting a coefficient to zero results in the following error:
scipoptsuite-8.0.3/scip/src/scip/expr.c:3425: SCIPexprCheckQuadratic: Assertion `coef != 0.0' failed.
Could you provide any additional guidance on this?
Thank you,
Enrico
________________________________
Da: Stefan Vigerske <svigerske at gams.com>
Inviato: venerdì 16 agosto 2024 05:01
A: Enrico Calandrini <e.calandrini at studenti.unipi.it>; scip at zib.de <scip at zib.de>
Oggetto: Re: R: R: [SCIP] Quadratic Constraint Modification
Hi,
make sure you run SCIPfreeTransform() after the first solve.
You could also try calling SCIPfreeExprQuadratic() on the expression
after you did the modifications. That would free a datastructure that
stores a representation of the expression as quadratic form. But I don't
remember why SCIP would do this on the constraints in the original problem.
Stefan
On 15/08/2024 16:59, Enrico Calandrini wrote:
> Hi Stefan,
>
> I hope you're doing well.
>
> I recently had the opportunity to work on and test the functionalities you described. The process of retrieving and changing the coefficients works perfectly. However, these changes are not reflected when calling SCIPsolve again. To confirm this, I printed the model using SCIPwriteOrigProblem. Although other parts of the model (such as bounds and objective function coefficients) are updating correctly, the coefficients modified through the previous procedure remain unchanged.
>
> For further context, I store a pointer to the NonLinear constraint during the construction phase. After calling SCIPsolve, I retrieve the expression using SCIPgetExprNonlinear and the coefficients with SCIPgetCoefsExprSum. I then directly modify the vector of these coefficients. I'm confident the changes are being made correctly because when I repeat the same procedure after the new call to SCIPsolve, the coefficients are updated. It seems as though I'm not prompting SCIP to internally update its version of the constraint. Could you please let me know if I'm missing something?
>
> Thank you very much.
>
> Best regards,
> Enrico
> ________________________________
> Da: Stefan Vigerske <svigerske at gams.com>
> Inviato: martedì 16 luglio 2024 15:57
> A: Enrico Calandrini <e.calandrini at studenti.unipi.it>; scip at zib.de <scip at zib.de>
> Oggetto: Re: R: [SCIP] Quadratic Constraint Modification
>
> Hi,
>
> On 16/07/2024 15:41, Enrico Calandrini wrote:
>> Hi Stefan,
>> Thank you very much for your reply! That is exactly what I was looking for!
>> I just have a quick follow-up question on what you mentioned. Let's say I have an expression like x + 2xy + 3y². Following your procedure, I should be able to retrieve all the terms of the sum (x, xy, y²) using SCIPexprGetChildren(). However, I'm now wondering how I can directly access the individual expressions to determine which variables are active within each one. Is there a method to retrieve the indices of the active variables in the expression?
>
> You may want to inspect the expression in each child to see whether it
> is x, xy, or y^2. Use functions like SCIPisExprVar(),
> SCIPisExprProduct(), and SCIPisExprPower(). Then use SCIPgetVarExprVar()
> to get the variable in a var-expression or SCIPgetExponentExprPow() to
> get the exponent in a power expression. For a product, use
> SCIPexprGetNChildren() to get the number of children and check that each
> is a var-expression.
>
>> Assuming this is possible, if I retrieve the vector of coefficients with SCIPgetCoefsExprSum() and modify it, is it sufficient to make the change active in SCIP? Do I need to "provide back" the new vector to SCIP in any way?
>
> Modifying the values in SCIPgetCoefsExprSum() should be sufficient, as
> the array returned is a pointer to internal data. It is not so clean to
> do this, but there is currently no elegant way (as far as I remember).
>
> Stefan
>
>> Thank you very much for your time!
>> Enrico
>> ________________________________
>> Da: Stefan Vigerske <svigerske at gams.com>
>> Inviato: martedì 16 luglio 2024 15:24
>> A: Enrico Calandrini <e.calandrini at studenti.unipi.it>; scip at zib.de <scip at zib.de>
>> Oggetto: Re: [SCIP] Quadratic Constraint Modification
>>
>> Hi,
>>
>> SCIPcreateConsBasicQuadraticNonlinear() is correct. This is a wrapper
>> around SCIPcreateConsBasicNonlinear(), which makes it easier to create
>> nonlinear constraints that are quadratic.
>>
>> There is no SCIPchgLinearCoefQuadratic() in current SCIP versions anymore.
>>
>> Since SCIP 8, quadratic constraints are no longer distinguished from
>> other nonlinear constraints. The function of a quadratic constraint is
>> stored as an expression that is a sum of variables, squares of
>> variables, and products of two variables, each term in the sum can have
>> a coefficient. If you just want to change a coefficient in the sum, then
>> you can get the expression of the constraint (SCIPgetExprNonlinear()),
>> check with SCIPisExprSum() that it is indeed a sum, and use
>> SCIPgetCoefsExprSum() to get the coefficients vector. I think it should
>> be ok to change this vector when SCIP is in problem stage. To get the
>> expressions that make up the terms of sum, use SCIPexprGetChildren().
>> For the number of terms, use SCIPexprGetNChildren().
>>
>> To change the problem after a solve, call SCIPfreeTransform(). That
>> should bring SCIP back into problem stage.
>>
>> Hope that helps,
>> Stefan
>>
>> On 15/07/2024 14:59, Enrico Calandrini wrote:
>>> Hi everyone,
>>> First of all, thank you all for your amazing work!
>>> I was wondering if someone could help me understand how to properly modify a quadratic constraint, if this is possible. Specifically, I'm adding a quadratic constraint using the function SCIPcreateConsBasicQuadraticNonlinear. Firstly, is this method the correct one in the current version of SCIP? Additionally, it is possible that after the model has been solved, I may need to modify that constraint.
>>> I have noticed that in the constraint handler for quadratic constraints (cons_quadratic.h), there are functions like SCIPchgLinearCoefQuadratic() to modify both linear and quadratic coefficients. However, I'm unsure whether these methods can be used for a constraint created as described above. Moreover, I have seen in the documentation that these changes are allowed only in the problem creation stage, meaning that after a call to SCIPsolve, this constraint can't be modified anymore?
>>> I'm a little bit confused, so thank you in advance for your support!
>>> Enrico Calandrini
>>>
>>>
>>> _______________________________________________
>>> Scip mailing list
>>> Scip at zib.de
>>> https://listserv.zib.de/mailman/listinfo/scip
>>
>>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://listserv.zib.de/pipermail/scip/attachments/20240817/8174189b/attachment.html>
More information about the Scip
mailing list