Skip to content

Commit

Permalink
Modeler 2.4c: portfield substitution (#2406)
Browse files Browse the repository at this point in the history
Co-authored-by: OMNES Florian <[email protected]>
  • Loading branch information
payetvin and OMNES Florian authored Sep 19, 2024
1 parent e24124d commit 8f62902
Show file tree
Hide file tree
Showing 5 changed files with 178 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/solver/expressions/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ set(SRC_Expressions
visitors/TimeIndexVisitor.cpp
visitors/PrintVisitor.cpp
visitors/SubstitutionVisitor.cpp
visitors/PortfieldSubstitutionVisitor.cpp
visitors/InvalidNode.cpp

include/antares/solver/expressions/nodes/SumNode.h
Expand Down Expand Up @@ -50,6 +51,7 @@ set(SRC_Expressions
include/antares/solver/expressions/visitors/TimeIndexVisitor.h
include/antares/solver/expressions/visitors/TimeIndex.h
include/antares/solver/expressions/visitors/SubstitutionVisitor.h
include/antares/solver/expressions/visitors/PortfieldSubstitutionVisitor.h
include/antares/solver/expressions/visitors/InvalidNode.h

include/antares/solver/expressions/Registry.hxx
Expand All @@ -67,6 +69,7 @@ target_include_directories(antares-solver-expressions
target_link_libraries(antares-solver-expressions
PUBLIC
Antares::logs
Boost::headers
)


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
** Copyright 2007-2024, RTE (https://www.rte-france.com)
** See AUTHORS.txt
** SPDX-License-Identifier: MPL-2.0
** This file is part of Antares-Simulator,
** Adequacy and Performance assessment for interconnected energy networks.
**
** Antares_Simulator is free software: you can redistribute it and/or modify
** it under the terms of the Mozilla Public Licence 2.0 as published by
** the Mozilla Foundation, either version 2 of the License, or
** (at your option) any later version.
**
** Antares_Simulator is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** Mozilla Public Licence 2.0 for more details.
**
** You should have received a copy of the Mozilla Public Licence 2.0
** along with Antares_Simulator. If not, see <https://opensource.org/license/mpl-2-0/>.
*/
#pragma once

#include <map>

#include "antares/solver/expressions/visitors/CloneVisitor.h"

namespace Antares::Solver::Visitors
{

struct KeyHasher
{
std::size_t operator()(const Nodes::PortFieldNode& n) const;
};

/**
* @brief Represents the context for performing substitutions in a syntax tree.
*/
struct PortfieldSubstitutionContext
{
std::unordered_map<Nodes::PortFieldNode, Nodes::Node*, KeyHasher> portfield;
};

/**
* @brief Represents a visitor for substituting portfield nodes in a syntax tree.
*/
class PortfieldSubstitutionVisitor: public CloneVisitor
{
public:
PortfieldSubstitutionVisitor(Registry<Nodes::Node>& registry,
PortfieldSubstitutionContext& ctx);

PortfieldSubstitutionContext& ctx_;
std::string name() const override;

private:
// Only override visit method for PortField, clone the rest
Nodes::Node* visit(const Nodes::PortFieldNode* node) override;
};
} // namespace Antares::Solver::Visitors
61 changes: 61 additions & 0 deletions src/solver/expressions/visitors/PortfieldSubstitutionVisitor.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
** Copyright 2007-2024, RTE (https://www.rte-france.com)
** See AUTHORS.txt
** SPDX-License-Identifier: MPL-2.0
** This file is part of Antares-Simulator,
** Adequacy and Performance assessment for interconnected energy networks.
**
** Antares_Simulator is free software: you can redistribute it and/or modify
** it under the terms of the Mozilla Public Licence 2.0 as published by
** the Mozilla Foundation, either version 2 of the License, or
** (at your option) any later version.
**
** Antares_Simulator is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** Mozilla Public Licence 2.0 for more details.
**
** You should have received a copy of the Mozilla Public Licence 2.0
** along with Antares_Simulator. If not, see <https://opensource.org/license/mpl-2-0/>.
*/
#include <boost/functional/hash.hpp>

#include <antares/solver/expressions/nodes/ExpressionsNodes.h>
#include <antares/solver/expressions/visitors/PortfieldSubstitutionVisitor.h>

namespace Antares::Solver::Visitors
{

PortfieldSubstitutionVisitor::PortfieldSubstitutionVisitor(Registry<Nodes::Node>& registry,
PortfieldSubstitutionContext& ctx):
CloneVisitor(registry),
ctx_(ctx)
{
}

Nodes::Node* PortfieldSubstitutionVisitor::visit(const Nodes::PortFieldNode* node)
{
if (auto it = ctx_.portfield.find(*node); it != ctx_.portfield.end())
{
return it->second;
}

return CloneVisitor::visit(node);
}

std::string PortfieldSubstitutionVisitor::name() const
{
return "PortfieldSubstitutionVisitor";
}

std::size_t KeyHasher::operator()(const Nodes::PortFieldNode& n) const
{
std::size_t seed = 0;

boost::hash_combine(seed, boost::hash_value(n.getPortName()));
boost::hash_combine(seed, boost::hash_value(n.getFieldName()));

return seed;
}

} // namespace Antares::Solver::Visitors
1 change: 1 addition & 0 deletions src/tests/cucumber/conf.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
antares-solver : /home/payetvin/Antares_Simulator/_build/solver/antares-solver
54 changes: 54 additions & 0 deletions src/tests/src/solver/expressions/test_SubstitutionVisitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@

#include <antares/solver/expressions/Registry.hxx>
#include <antares/solver/expressions/nodes/ExpressionsNodes.h>
#include <antares/solver/expressions/visitors/CompareVisitor.h>
#include <antares/solver/expressions/visitors/PortfieldSubstitutionVisitor.h>
#include <antares/solver/expressions/visitors/PrintVisitor.h>
#include <antares/solver/expressions/visitors/SubstitutionVisitor.h>

Expand Down Expand Up @@ -87,3 +89,55 @@ BOOST_FIXTURE_TEST_CASE(SubstitutionVisitor_name, Registry<Node>)
BOOST_CHECK_EQUAL(substitutionVisitor.name(), "SubstitutionVisitor");
}
BOOST_AUTO_TEST_SUITE_END()

BOOST_AUTO_TEST_SUITE(_PortfieldSubstitutionVisitor_)

class SubstitutionFixture: public Registry<Node>
{
public:
Node* originalExpression()
{
Node* port1 = create<PortFieldNode>("port", "literal");
Node* port2 = create<PortFieldNode>("another port", "not a literal");
Node* root = create<SumNode>(port1, port2);
return root;
}

Node* expectedExpressionAfterSubstitution()
{
Node* node1 = create<LiteralNode>(10);
Node* port2 = create<PortFieldNode>("another port", "not a literal");
Node* root = create<SumNode>(node1, port2);
return root;
}

Node* substitute(Node* original)
{
PortfieldSubstitutionContext ctx;
ctx.portfield.emplace(PortFieldNode("port", "literal"), create<LiteralNode>(10));

PortfieldSubstitutionVisitor sub(*this, ctx);
return sub.dispatch(original);
}
};

BOOST_FIXTURE_TEST_CASE(PortfieldSubstitutionVisitor_simple, SubstitutionFixture)

{
Node* original = originalExpression();
Node* substituted = substitute(original);
Node* expected = expectedExpressionAfterSubstitution();

CompareVisitor cmp;
BOOST_CHECK(cmp.dispatch(substituted, expected));
}

BOOST_FIXTURE_TEST_CASE(PortfieldSubstitutionVisitor_name, Registry<Node>)
{
PortfieldSubstitutionContext ctx;

PortfieldSubstitutionVisitor substitutionVisitor(*this, ctx);
BOOST_CHECK_EQUAL(substitutionVisitor.name(), "PortfieldSubstitutionVisitor");
}

BOOST_AUTO_TEST_SUITE_END()

0 comments on commit 8f62902

Please sign in to comment.