From 085d8d0becdfdcbbd0d9b8791ed8d6337f3d2701 Mon Sep 17 00:00:00 2001 From: Gleb Belov Date: Tue, 28 May 2024 19:28:21 +1000 Subject: [PATCH] Multi-obj: SCIP, GCG #239 --- solvers/gcgmp/gcgmpcommon.cc | 3 +-- solvers/gcgmp/gcgmpcommon.h | 7 ++++--- solvers/gcgmp/gcgmpmodelapi.cc | 8 ++++++-- solvers/scipmp/scipmpcommon.cc | 3 +-- solvers/scipmp/scipmpcommon.h | 7 ++++--- solvers/scipmp/scipmpmodelapi.cc | 8 ++++++-- 6 files changed, 22 insertions(+), 14 deletions(-) diff --git a/solvers/gcgmp/gcgmpcommon.cc b/solvers/gcgmp/gcgmpcommon.cc index b41ee6b18..dbb8b63ab 100644 --- a/solvers/gcgmp/gcgmpcommon.cc +++ b/solvers/gcgmp/gcgmpcommon.cc @@ -9,13 +9,12 @@ SCIP_DECL_PROBDELORIG(probdataDelOrigNl) int i; assert((*probdata)->vars != NULL || (*probdata)->nvars == 0); - assert((*probdata)->linconss != NULL || (*probdata)->nlinconss == 0); + assert((*probdata)->linconss.size() || (*probdata)->nlinconss == 0); for( i = 0; i < (*probdata)->nlinconss; ++i ) { SCIP_CALL( SCIPreleaseCons(scip, &(*probdata)->linconss[i]) ); } - SCIPfreeBlockMemoryArray(scip, &(*probdata)->linconss, (*probdata)->nlinconss); for( i = 0; i < (*probdata)->nvars; ++i ) { diff --git a/solvers/gcgmp/gcgmpcommon.h b/solvers/gcgmp/gcgmpcommon.h index b806a4796..0648a5123 100644 --- a/solvers/gcgmp/gcgmpcommon.h +++ b/solvers/gcgmp/gcgmpcommon.h @@ -1,6 +1,7 @@ #ifndef GCGCOMMON_H #define GCGCOMMON_H +#include #include #include "mp/backend-to-model-api.h" @@ -18,9 +19,9 @@ struct SCIP_ProbData SCIP_VAR** vars; /**< variables in the order given by AMPL */ int nvars; /**< number of variables */ - SCIP_CONS** linconss; /**< linear constraints in the order given by AMPL */ - int i; /**< shows free slot of linear constraints */ - int nlinconss; /**< number of linear constraints */ + std::vector linconss; /**< linear constraints in the order given by AMPL */ + int i = 0; /**< shows free slot of linear constraints */ + int nlinconss = 0; /**< number of linear constraints */ gcg::PARTIALDECOMP* decomp; /**< user partialdecomp */ }; diff --git a/solvers/gcgmp/gcgmpmodelapi.cc b/solvers/gcgmp/gcgmpmodelapi.cc index 94f09b5cf..4dc39690e 100644 --- a/solvers/gcgmp/gcgmpmodelapi.cc +++ b/solvers/gcgmp/gcgmpmodelapi.cc @@ -6,9 +6,10 @@ namespace mp { void GcgModelAPI::InitProblemModificationPhase(const FlatModelInfo* flat_model_info) { // Allocate storage if needed: int n_linear_cons = flat_model_info->GetNumberOfConstraintsOfGroup(CG_Linear); + if (getPROBDATA()->nlinconss) + GCG_CCALL( SCIPfreeTransform(getSCIP()) ); // allow model update getPROBDATA()->nlinconss = n_linear_cons; - getPROBDATA()->i = 0; - GCG_CCALL( SCIPallocBlockMemoryArray(getSCIP(), &getPROBDATA()->linconss, getPROBDATA()->nlinconss) ); + getPROBDATA()->linconss.resize(n_linear_cons); } void GcgModelAPI::AddVariables(const VarArrayDef& v) { @@ -43,6 +44,9 @@ void GcgModelAPI::SetLinearObjective( int iobj, const LinearObjective& lo ) { GCG_CCALL( SCIPsetObjsense(getSCIP(), obj::Type::MAX==lo.obj_sense() ? SCIP_OBJSENSE_MAXIMIZE : SCIP_OBJSENSE_MINIMIZE) ); SCIP_VAR** vars = getPROBDATA()->vars; + for (int i = 0; i < getPROBDATA()->nvars; i++) { + GCG_CCALL( SCIPchgVarObj(getSCIP(), vars[i], 0.0) ); // zero out + } for (int i = 0; i < lo.num_terms(); i++) { GCG_CCALL( SCIPchgVarObj(getSCIP(), vars[lo.vars()[i]], lo.coefs()[i]) ); } diff --git a/solvers/scipmp/scipmpcommon.cc b/solvers/scipmp/scipmpcommon.cc index d71611cb2..a53875a9d 100644 --- a/solvers/scipmp/scipmpcommon.cc +++ b/solvers/scipmp/scipmpcommon.cc @@ -7,13 +7,12 @@ SCIP_DECL_PROBDELORIG(probdataDelOrigNl) int i; assert((*probdata)->vars != NULL || (*probdata)->nvars == 0); - assert((*probdata)->linconss != NULL || (*probdata)->nlinconss == 0); + assert((*probdata)->linconss.size() || (*probdata)->nlinconss == 0); for( i = 0; i < (*probdata)->nlinconss; ++i ) { SCIP_CALL( SCIPreleaseCons(scip, &(*probdata)->linconss[i]) ); } - SCIPfreeBlockMemoryArray(scip, &(*probdata)->linconss, (*probdata)->nlinconss); for( i = 0; i < (*probdata)->nvars; ++i ) { diff --git a/solvers/scipmp/scipmpcommon.h b/solvers/scipmp/scipmpcommon.h index fc2b68bb4..995ffd0cb 100644 --- a/solvers/scipmp/scipmpcommon.h +++ b/solvers/scipmp/scipmpcommon.h @@ -1,6 +1,7 @@ #ifndef SCIPCOMMON_H #define SCIPCOMMON_H +#include #include #include "mp/backend-to-model-api.h" @@ -16,9 +17,9 @@ struct SCIP_ProbData SCIP_VAR** vars; /**< variables in the order given by AMPL */ int nvars; /**< number of variables */ - SCIP_CONS** linconss; /**< linear constraints in the order given by AMPL */ - int i; /**< shows free slot of linear constraints */ - int nlinconss; /**< number of linear constraints */ + std::vector linconss; /**< linear constraints in the order given by AMPL */ + int i = 0; /**< shows free slot of linear constraints */ + int nlinconss = 0; /**< number of linear constraints */ }; namespace mp { diff --git a/solvers/scipmp/scipmpmodelapi.cc b/solvers/scipmp/scipmpmodelapi.cc index 1642cc69f..c123b673e 100644 --- a/solvers/scipmp/scipmpmodelapi.cc +++ b/solvers/scipmp/scipmpmodelapi.cc @@ -5,9 +5,10 @@ namespace mp { void ScipModelAPI::InitProblemModificationPhase(const FlatModelInfo* flat_model_info) { // Allocate storage if needed: int n_linear_cons = flat_model_info->GetNumberOfConstraintsOfGroup(CG_Linear); + if (getPROBDATA()->nlinconss) + SCIP_CCALL( SCIPfreeTransform(getSCIP()) ); // allow model update getPROBDATA()->nlinconss = n_linear_cons; - getPROBDATA()->i = 0; - SCIP_CCALL( SCIPallocBlockMemoryArray(getSCIP(), &getPROBDATA()->linconss, getPROBDATA()->nlinconss) ); + getPROBDATA()->linconss.resize(n_linear_cons); } void ScipModelAPI::AddVariables(const VarArrayDef& v) { @@ -42,6 +43,9 @@ void ScipModelAPI::SetLinearObjective( int iobj, const LinearObjective& lo ) { SCIP_CCALL( SCIPsetObjsense(getSCIP(), obj::Type::MAX==lo.obj_sense() ? SCIP_OBJSENSE_MAXIMIZE : SCIP_OBJSENSE_MINIMIZE) ); SCIP_VAR** vars = getPROBDATA()->vars; + for (int i = 0; i < getPROBDATA()->nvars; i++) { + SCIP_CCALL( SCIPchgVarObj(getSCIP(), vars[i], 0.0) ); // zero out + } for (int i = 0; i < lo.num_terms(); i++) { SCIP_CCALL( SCIPchgVarObj(getSCIP(), vars[lo.vars()[i]], lo.coefs()[i]) ); }