Skip to content

Commit

Permalink
Modeler 2.4c: PortFieldSum and substitution (#2415)
Browse files Browse the repository at this point in the history
Co-authored-by: Florian OMNES <[email protected]>
  • Loading branch information
payetvin and flomnes authored Sep 26, 2024
1 parent f3922b2 commit 8ce6107
Show file tree
Hide file tree
Showing 35 changed files with 498 additions and 47 deletions.
13 changes: 10 additions & 3 deletions src/solver/expressions/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ project(Expressions)

set(SRC_Expressions
nodes/PortFieldNode.cpp
nodes/PortFieldSumNode.cpp
nodes/ComponentNode.cpp
nodes/BinaryNode.cpp
nodes/UnaryNode.cpp
Expand All @@ -16,10 +17,13 @@ set(SRC_Expressions
visitors/TimeIndexVisitor.cpp
visitors/PrintVisitor.cpp
visitors/SubstitutionVisitor.cpp
visitors/PortFieldSubstitutionVisitor.cpp
visitors/PortFieldSumSubstitutionVisitor.cpp
visitors/AstDOTStyleVisitor.cpp
visitors/PortfieldSubstitutionVisitor.cpp
visitors/InvalidNode.cpp

hashable.cpp

include/antares/solver/expressions/nodes/SumNode.h
include/antares/solver/expressions/nodes/BinaryNode.h
include/antares/solver/expressions/nodes/ComparisonNode.h
Expand All @@ -37,6 +41,7 @@ set(SRC_Expressions
include/antares/solver/expressions/nodes/NodesForwardDeclaration.h
include/antares/solver/expressions/nodes/ParameterNode.h
include/antares/solver/expressions/nodes/PortFieldNode.h
include/antares/solver/expressions/nodes/PortFieldSumNode.h
include/antares/solver/expressions/nodes/SubtractionNode.h
include/antares/solver/expressions/nodes/UnaryNode.h
include/antares/solver/expressions/nodes/VariableNode.h
Expand All @@ -52,12 +57,14 @@ 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/PortFieldSumSubstitutionVisitor.h
include/antares/solver/expressions/visitors/AstDOTStyleVisitor.h
include/antares/solver/expressions/visitors/PortfieldSubstitutionVisitor.h
include/antares/solver/expressions/visitors/InvalidNode.h

include/antares/solver/expressions/Registry.hxx
include/antares/solver/expressions/IName.h
include/antares/solver/expressions/hashable.h
)

source_group("expressions" FILES ${SRC_Expressions})
Expand All @@ -72,7 +79,7 @@ target_link_libraries(antares-solver-expressions
PUBLIC
Antares::logs
Boost::headers
)
)


add_library(antares-solver-expressions-iterators
Expand Down
49 changes: 49 additions & 0 deletions src/solver/expressions/hashable.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
** 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/hashable.h>

namespace Antares::Solver
{

Hashable::Hashable(const std::string& s1, const std::string& s2):
s1(s1),
s2(s2)
{
}

bool Hashable::operator==(const Hashable& other) const
{
return s1 == other.s1 && s2 == other.s2;
}

std::size_t PortFieldHash::operator()(const Hashable& n) const
{
std::size_t seed = 0;

boost::hash_combine(seed, boost::hash_value(n.s1));
boost::hash_combine(seed, boost::hash_value(n.s2));

return seed;
}

} // namespace Antares::Solver
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
** 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 <string>

namespace Antares::Solver
{
class Hashable
{
public:
Hashable(const std::string& s1, const std::string& s2);
~Hashable() = default;

bool operator==(const Hashable& other) const;

const std::string& s1;
const std::string& s2;
};

struct PortFieldHash
{
std::size_t operator()(const Hashable& n) const;
};

} // namespace Antares::Solver
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include <antares/solver/expressions/nodes/NegationNode.h>
#include <antares/solver/expressions/nodes/ParameterNode.h>
#include <antares/solver/expressions/nodes/PortFieldNode.h>
#include <antares/solver/expressions/nodes/PortFieldSumNode.h>
#include <antares/solver/expressions/nodes/SubtractionNode.h>
#include <antares/solver/expressions/nodes/SumNode.h>
#include <antares/solver/expressions/nodes/VariableNode.h>
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,5 @@ class ComponentParameterNode;
class ParameterNode;
class VariableNode;
class PortFieldNode;
class PortFieldSumNode;
} // namespace Antares::Solver::Nodes
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,15 @@
#pragma once
#include <string>

#include <antares/solver/expressions/hashable.h>
#include <antares/solver/expressions/nodes/Leaf.h>

namespace Antares::Solver::Nodes
{
/**
* @brief Represents a port field node in a syntax tree.
*/
class PortFieldNode: public Node
class PortFieldNode: public Node, public Hashable
{
public:
/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
** 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 <string>

#include <antares/solver/expressions/hashable.h>
#include <antares/solver/expressions/nodes/Leaf.h>

namespace Antares::Solver::Nodes
{
/**
* @brief Represents a port field node where the expression is a sum.
*/
class PortFieldSumNode: public Node, public Hashable
{
public:
/**
* @brief Constructs a port field sum node with the specified port and field names.
*
* @param port_name The port name.
* @param field_name The field name.
*/
explicit PortFieldSumNode(const std::string& port_name, const std::string& field_name);

/**
* @brief Retrieves the port name.
*
* @return The port name.
*/
const std::string& getPortName() const;

/**
* @brief Retrieves the field name.
*
* @return The field name.
*/
const std::string& getFieldName() const;

bool operator==(const PortFieldSumNode& other) const = default;

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

private:
std::string port_name_;
std::string field_name_;
};
} // namespace Antares::Solver::Nodes
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ class AstDOTStyleVisitor: public NodeVisitor<void, std::ostream&>
void visit(const Nodes::ParameterNode* node, std::ostream& os) override;
void visit(const Nodes::LiteralNode* node, std::ostream& os) override;
void visit(const Nodes::PortFieldNode* node, std::ostream& os) override;
void visit(const Nodes::PortFieldSumNode* node, std::ostream& os) override;
void visit(const Nodes::ComponentVariableNode* node, std::ostream& os) override;
void visit(const Nodes::ComponentParameterNode* node, std::ostream& os) override;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ class CloneVisitor: public NodeVisitor<Nodes::Node*>
Nodes::Node* visit(const Nodes::ParameterNode* node) override;
Nodes::Node* visit(const Nodes::LiteralNode* node) override;
Nodes::Node* visit(const Nodes::PortFieldNode* node) override;
Nodes::Node* visit(const Nodes::PortFieldSumNode* node) override;
Nodes::Node* visit(const Nodes::ComponentVariableNode* node) override;
Nodes::Node* visit(const Nodes::ComponentParameterNode* node) override;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ class CompareVisitor: public NodeVisitor<bool, const Nodes::Node*>
bool visit(const Nodes::ParameterNode* param, const Nodes::Node* other) override;
bool visit(const Nodes::LiteralNode* param, const Nodes::Node* other) override;
bool visit(const Nodes::PortFieldNode* port_field_node, const Nodes::Node* other) override;
bool visit(const Nodes::PortFieldSumNode* port_field_node, const Nodes::Node* other) override;
bool visit(const Nodes::ComponentVariableNode* component_node,
const Nodes::Node* other) override;
bool visit(const Nodes::ComponentParameterNode* component_node,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ class EvalVisitor: public NodeVisitor<double>
double visit(const Nodes::ParameterNode* node) override;
double visit(const Nodes::LiteralNode* node) override;
double visit(const Nodes::PortFieldNode* node) override;
double visit(const Nodes::PortFieldSumNode* node) override;
double visit(const Nodes::ComponentVariableNode* node) override;
double visit(const Nodes::ComponentParameterNode* node) override;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ class LinearityVisitor: public NodeVisitor<LinearStatus>
LinearStatus visit(const Nodes::ParameterNode* param) override;
LinearStatus visit(const Nodes::LiteralNode* lit) override;
LinearStatus visit(const Nodes::PortFieldNode* port_field_node) override;
LinearStatus visit(const Nodes::PortFieldSumNode* port_field_node) override;
LinearStatus visit(const Nodes::ComponentVariableNode* component_variable_node) override;
LinearStatus visit(const Nodes::ComponentParameterNode* component_parameter_node) override;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ class NodeVisitor: public IName
Nodes::VariableNode,
Nodes::LiteralNode,
Nodes::PortFieldNode,
Nodes::PortFieldSumNode,
Nodes::ComponentVariableNode,
Nodes::ComponentParameterNode>();

Expand Down Expand Up @@ -239,6 +240,16 @@ class NodeVisitor: public IName
*/
virtual R visit(const Nodes::PortFieldNode*, Args... args) = 0;

/**
* @brief Visits a PortFieldSumNode.
*
* @param node A pointer to the PortFieldSumNode to be visited.
* @param args Additional arguments to be passed to the visitor's methods.
*
* @return The result of processing the PortFieldSumNode.
*/
virtual R visit(const Nodes::PortFieldSumNode*, Args... args) = 0;

/**
* @brief Visits a ComponentVariableNode.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,29 +27,24 @@
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
struct PortFieldSubstitutionContext
{
std::unordered_map<Nodes::PortFieldNode, Nodes::Node*, KeyHasher> portfield;
std::unordered_map<Nodes::PortFieldNode, Nodes::Node*, PortFieldHash> portfield;
};

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

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

private:
Expand Down
Loading

0 comments on commit 8ce6107

Please sign in to comment.