From b86d022af151c007d7dd84290f8937918dde2b9c Mon Sep 17 00:00:00 2001 From: Tim Quatmann Date: Mon, 29 Apr 2019 09:29:29 +0200 Subject: [PATCH] GurobiLpSolver: Fixed an issue when popping and pushing variables with the same name. --- src/storm/solver/GurobiLpSolver.cpp | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/src/storm/solver/GurobiLpSolver.cpp b/src/storm/solver/GurobiLpSolver.cpp index 3fef398f0..caa6b945e 100644 --- a/src/storm/solver/GurobiLpSolver.cpp +++ b/src/storm/solver/GurobiLpSolver.cpp @@ -93,69 +93,72 @@ namespace storm { template storm::expressions::Variable GurobiLpSolver::addBoundedContinuousVariable(std::string const& name, ValueType lowerBound, ValueType upperBound, ValueType objectiveFunctionCoefficient) { - storm::expressions::Variable newVariable = this->manager->declareVariable(name, this->manager->getRationalType()); + storm::expressions::Variable newVariable = this->manager->declareOrGetVariable(name, this->manager->getRationalType()); this->addVariable(newVariable, GRB_CONTINUOUS, lowerBound, upperBound, objectiveFunctionCoefficient); return newVariable; } template storm::expressions::Variable GurobiLpSolver::addLowerBoundedContinuousVariable(std::string const& name, ValueType lowerBound, ValueType objectiveFunctionCoefficient) { - storm::expressions::Variable newVariable = this->manager->declareVariable(name, this->manager->getRationalType()); + storm::expressions::Variable newVariable = this->manager->declareOrGetVariable(name, this->manager->getRationalType()); this->addVariable(newVariable, GRB_CONTINUOUS, lowerBound, GRB_INFINITY, objectiveFunctionCoefficient); return newVariable; } template storm::expressions::Variable GurobiLpSolver::addUpperBoundedContinuousVariable(std::string const& name, ValueType upperBound, ValueType objectiveFunctionCoefficient) { - storm::expressions::Variable newVariable = this->manager->declareVariable(name, this->manager->getRationalType()); + storm::expressions::Variable newVariable = this->manager->declareOrGetVariable(name, this->manager->getRationalType()); this->addVariable(newVariable, GRB_CONTINUOUS, -GRB_INFINITY, upperBound, objectiveFunctionCoefficient); return newVariable; } template storm::expressions::Variable GurobiLpSolver::addUnboundedContinuousVariable(std::string const& name, ValueType objectiveFunctionCoefficient) { - storm::expressions::Variable newVariable = this->manager->declareVariable(name, this->manager->getRationalType()); + storm::expressions::Variable newVariable = this->manager->declareOrGetVariable(name, this->manager->getRationalType()); this->addVariable(newVariable, GRB_CONTINUOUS, -GRB_INFINITY, GRB_INFINITY, objectiveFunctionCoefficient); return newVariable; } template storm::expressions::Variable GurobiLpSolver::addBoundedIntegerVariable(std::string const& name, ValueType lowerBound, ValueType upperBound, ValueType objectiveFunctionCoefficient) { - storm::expressions::Variable newVariable = this->manager->declareVariable(name, this->manager->getIntegerType()); + storm::expressions::Variable newVariable = this->manager->declareOrGetVariable(name, this->manager->getIntegerType()); this->addVariable(newVariable, GRB_INTEGER, lowerBound, upperBound, objectiveFunctionCoefficient); return newVariable; } template storm::expressions::Variable GurobiLpSolver::addLowerBoundedIntegerVariable(std::string const& name, ValueType lowerBound, ValueType objectiveFunctionCoefficient) { - storm::expressions::Variable newVariable = this->manager->declareVariable(name, this->manager->getIntegerType()); + storm::expressions::Variable newVariable = this->manager->declareOrGetVariable(name, this->manager->getIntegerType()); this->addVariable(newVariable, GRB_INTEGER, lowerBound, GRB_INFINITY, objectiveFunctionCoefficient); return newVariable; } template storm::expressions::Variable GurobiLpSolver::addUpperBoundedIntegerVariable(std::string const& name, ValueType upperBound, ValueType objectiveFunctionCoefficient) { - storm::expressions::Variable newVariable = this->manager->declareVariable(name, this->manager->getIntegerType()); + storm::expressions::Variable newVariable = this->manager->declareOrGetVariable(name, this->manager->getIntegerType()); this->addVariable(newVariable, GRB_INTEGER, -GRB_INFINITY, upperBound, objectiveFunctionCoefficient); return newVariable; } template storm::expressions::Variable GurobiLpSolver::addUnboundedIntegerVariable(std::string const& name, ValueType objectiveFunctionCoefficient) { - storm::expressions::Variable newVariable = this->manager->declareVariable(name, this->manager->getIntegerType()); + storm::expressions::Variable newVariable = this->manager->declareOrGetVariable(name, this->manager->getIntegerType()); this->addVariable(newVariable, GRB_INTEGER, -GRB_INFINITY, GRB_INFINITY, objectiveFunctionCoefficient); return newVariable; } template storm::expressions::Variable GurobiLpSolver::addBinaryVariable(std::string const& name, ValueType objectiveFunctionCoefficient) { - storm::expressions::Variable newVariable = this->manager->declareVariable(name, this->manager->getIntegerType()); + storm::expressions::Variable newVariable = this->manager->declareOrGetVariable(name, this->manager->getIntegerType()); this->addVariable(newVariable, GRB_BINARY, 0, 1, objectiveFunctionCoefficient); return newVariable; } template void GurobiLpSolver::addVariable(storm::expressions::Variable const& variable, char variableType, ValueType lowerBound, ValueType upperBound, ValueType objectiveFunctionCoefficient) { + // Assert whether the variable does not exist yet. + // Due to incremental usage (push(), pop()), a variable might be declared in the manager but not in the lp model. + STORM_LOG_ASSERT(variableToIndexMap.count(variable) == 0, "Variable " << variable << " exists already in the model."); // Check for valid variable type. STORM_LOG_ASSERT(variableType == GRB_CONTINUOUS || variableType == GRB_INTEGER || variableType == GRB_BINARY, "Illegal type '" << variableType << "' for Gurobi variable.");