Skip to content

Commit

Permalink
Simplify ComponentFiller::addVariables
Browse files Browse the repository at this point in the history
  • Loading branch information
flomnes committed Jan 30, 2025
1 parent caa5406 commit b652da2
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 27 deletions.
38 changes: 15 additions & 23 deletions src/solver/optim-model-filler/ComponentFiller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,45 +44,37 @@ bool checkTimeSteps(Solver::Modeler::Api::FillContext& ctx)
return ctx.getFirstTimeStep() <= ctx.getLastTimeStep();
}

void ComponentFiller::addVariables_(Solver::Modeler::Api::ILinearProblem& pb,
const std::unique_ptr<Solver::Visitors::EvalVisitor>& evaluator,
unsigned int nb_vars) const
void ComponentFiller::addVariables(Solver::Modeler::Api::ILinearProblem& pb,
Solver::Modeler::Api::LinearProblemData& data,
Solver::Modeler::Api::FillContext& ctx)
{
if (!checkTimeSteps(ctx))
{
// exception?
return;
}

Solver::Visitors::EvalVisitor evaluator(evaluationContext_);
for (const auto& variable: component_.getModel()->Variables() | std::views::values)
{
if (variable.isTimeDependent())
{
pb.addVariable(evaluator->dispatch(variable.LowerBound().RootNode()),
evaluator->dispatch(variable.UpperBound().RootNode()),
pb.addVariable(evaluator.dispatch(variable.LowerBound().RootNode()),
evaluator.dispatch(variable.UpperBound().RootNode()),
variable.Type() != Study::SystemModel::ValueType::FLOAT,
component_.Id() + "." + variable.Id(),
nb_vars);
ctx.getNumberOfTimestep());
}
else
{
pb.addVariable(evaluator->dispatch(variable.LowerBound().RootNode()),
evaluator->dispatch(variable.UpperBound().RootNode()),
pb.addVariable(evaluator.dispatch(variable.LowerBound().RootNode()),
evaluator.dispatch(variable.UpperBound().RootNode()),
variable.Type() != Study::SystemModel::ValueType::FLOAT,
component_.Id() + "." + variable.Id());
}
}
}

void ComponentFiller::addVariables(Solver::Modeler::Api::ILinearProblem& pb,
Solver::Modeler::Api::LinearProblemData& data,
Solver::Modeler::Api::FillContext& ctx)
{
auto evaluator = std::make_unique<Solver::Visitors::EvalVisitor>(evaluationContext_);
if (checkTimeSteps(ctx))
{
addVariables_(pb, evaluator, ctx.getNumberOfTimestep());
}
else
{
// exception?
}
}

void ComponentFiller::addStaticConstraint(Solver::Modeler::Api::ILinearProblem& pb,
const LinearConstraint& linear_constraint,
const std::string& constraint_id) const
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,6 @@ class ComponentFiller: public Solver::Modeler::Api::LinearProblemFiller
/// Create a ComponentFiller for a Component
explicit ComponentFiller(const Study::SystemModel::Component& component);

void addVariables_(Solver::Modeler::Api::ILinearProblem& pb,
const std::unique_ptr<Solver::Visitors::EvalVisitor>& evaluator,
unsigned int nb_vars) const;

void addVariables(Solver::Modeler::Api::ILinearProblem& pb,
Solver::Modeler::Api::LinearProblemData& data,
Solver::Modeler::Api::FillContext& ctx) override;
Expand Down

0 comments on commit b652da2

Please sign in to comment.