Skip to content

Commit

Permalink
[ANT-2206] yaml to model (#2431)
Browse files Browse the repository at this point in the history
# Description
Expose `Antares::Solver::ObjectModel::Library
Antares::Solver::ModelConverter::convert(const
Antares::Solver::ModelParser::Library& library)`

# Implementation details
Convert a Library resulting from yaml parsing to a library in the
context ob ObjectModel

Use builder pattern to construct complexes object such as library and
models

Declaring a lib ModelConverter as the same level as ObjectModel and
ObjectParser prevent either from depending on both. Conceptually they're
independent from each others

Defines two ValueType enums in two different contexts. Allow for
diverging evolution

# Limitations

- [ANT-2228](https://gopro-tickets.rte-france.com/browse/ANT-2228) Gérer
les exception de champs superflus
- [ANT-2230](https://gopro-tickets.rte-france.com/browse/ANT-2230) Gérer
les champs optionnels
- [ANT-2233](https://gopro-tickets.rte-france.com/browse/ANT-2233) Pour
les valueType, gérer la capitalisation
- [ANT-2233](https://gopro-tickets.rte-france.com/browse/ANT-2233)
value_type pour les Parameters
- [ANT-2235](https://gopro-tickets.rte-france.com/browse/ANT-2235)
contraintes et binding_constraints
- [ANT-2236](https://gopro-tickets.rte-france.com/browse/ANT-2236)
Constraintes et expressions, inadéquation models/yaml
- Ports are not handled in the scope of this PR/ANT-2206

---------

Co-authored-by: Vincent Payet <[email protected]>
Co-authored-by: payetvin <[email protected]>
Co-authored-by: guilpier-code <[email protected]>
Co-authored-by: Florian OMNES <[email protected]>
  • Loading branch information
5 people authored Oct 8, 2024
1 parent 39b5c29 commit af9f559
Show file tree
Hide file tree
Showing 24 changed files with 1,227 additions and 103 deletions.
1 change: 1 addition & 0 deletions src/solver/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ add_subdirectory(infeasible-problem-analysis)
add_subdirectory(libModelObject)
add_subdirectory(lps)
add_subdirectory(misc)
add_subdirectory(modelConverter)
add_subdirectory(modelParser)
add_subdirectory(modeler)
add_subdirectory(optimisation)
Expand Down
2 changes: 2 additions & 0 deletions src/solver/libModelObject/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
project(LibObjectModel)

set(SRC_model
library.cpp
model.cpp

include/antares/solver/libObjectModel/library.h
Expand All @@ -19,6 +20,7 @@ set(SRC_model
source_group("libObjectModel" FILES ${SRC_model})
add_library(antares-solver-libObjectModel
${SRC_model})
add_library(Antares::antares-solver-libObjectModel ALIAS antares-solver-libObjectModel)

target_include_directories(antares-solver-libObjectModel
PUBLIC
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,24 @@ namespace Antares::Solver::ObjectModel
class Constraint
{
public:
Constraint();
~Constraint() = default;
Constraint(std::string name, Expression expression):
id_(std::move(name)),
expression_(std::move(expression))
{
}

const std::string& Id() const
{
return id_;
}

Expression expression() const
{
return expression_;
}

private:
std::string name_;
std::string id_;
Expression expression_;
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,20 @@ namespace Antares::Solver::ObjectModel
class Expression
{
public:
Expression();
~Expression() = default;
Expression() = default;

explicit Expression(std::string value):
value_(std::move(value))
{
}

const std::string& Value() const
{
return value_;
}

private:
std::string value_;
};

} // namespace Antares::Solver::ObjectModel
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
*/
#pragma once

#include <map>
#include <string>
#include <unordered_map>

#include "model.h"
#include "portType.h"
Expand All @@ -32,15 +33,56 @@ namespace Antares::Solver::ObjectModel
class Library
{
public:
Library();
Library() = default;
~Library() = default;

const std::string& Id() const
{
return id_;
}

const std::string& Description() const
{
return description_;
}

const std::unordered_map<std::string, PortType>& PortTypes() const
{
return portTypes_;
}

const std::unordered_map<std::string, Model>& Models() const
{
return models_;
}

private:
friend class LibraryBuilder;

std::string id_;
std::string description_;

std::map<std::string, PortType> portTypes_;
std::map<std::string, Model> models_;
std::unordered_map<std::string, PortType> portTypes_;
std::unordered_map<std::string, Model> models_;
};

/**
* @brief Builder for the Library class
* Follow builder pattern:
* builder.Library().withId("id").withDescription("description").withPortTypes(portList).withModels(modelList).build();
*/
class LibraryBuilder
{
public:
LibraryBuilder& withId(const std::string& id);
LibraryBuilder& withDescription(const std::string& description);
LibraryBuilder& withPortTypes(std::vector<PortType>&& portTypes);
LibraryBuilder& withModels(std::vector<Model>&& models);

Library build();

private:
Library library_;
};

} // namespace Antares::Solver::ObjectModel
Original file line number Diff line number Diff line change
Expand Up @@ -39,22 +39,61 @@ namespace Antares::Solver::ObjectModel
class Model
{
public:
Model();
~Model() = default;
const std::string& Id() const
{
return id_;
}

std::vector<Constraint*> getConstraints();
Expression Objective() const
{
return objective_;
}

const std::map<std::string, Constraint>& getConstraints() const
{
return constraints_;
}

const std::map<std::string, Parameter>& Parameters() const
{
return parameters_;
}

const std::map<std::string, Variable>& Variables() const
{
return variables_;
}

const std::map<std::string, Port>& Ports() const
{
return ports_;
}

private:
friend class ModelBuilder;
std::string id_;
Expression objective_;

std::map<std::string, Parameter> parameters_;
std::map<std::string, Variable> variables_;

std::map<std::string, Constraint> constraints_;
std::map<std::string, Constraint> bindingConstraints_;

std::map<std::string, Port> ports_;
};

class ModelBuilder
{
public:
ModelBuilder& withId(std::string_view id);
ModelBuilder& withObjective(Expression objective);
ModelBuilder& withParameters(std::vector<Parameter>&& parameters);
ModelBuilder& withVariables(std::vector<Variable>&& variables);
ModelBuilder& withPorts(std::vector<Port>&& ports);
Model build();

ModelBuilder& withConstraints(std::vector<Constraint>&& constraints);

private:
Model model_;
};

} // namespace Antares::Solver::ObjectModel
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,59 @@ namespace Antares::Solver::ObjectModel
class Parameter
{
public:
Parameter();
~Parameter() = default;
/** Using enum class to avoid primitive obsession. Mainly prevent headhaches when reading
* Parameter("Param", ValueType::FLOAT, false, true)
* Avoid mixing wich value is which boolean parameter
*/

enum class TimeDependent : bool
{
NO = false,
YES = true
};

enum class ScenarioDependent : bool
{
NO = false,
YES = true
};

explicit Parameter(std::string id,
ValueType type,
TimeDependent timeDependent,
ScenarioDependent scenarioDependent):
id_(std::move(id)),
type_(type),
timeDependent_(timeDependent),
scenarioDependent_(scenarioDependent)
{
}

const std::string& Id() const
{
return id_;
}

ValueType Type() const
{
return type_;
}

bool isTimeDependent() const
{
return timeDependent_ == TimeDependent::YES;
}

bool isScenarioDependent() const
{
return scenarioDependent_ == ScenarioDependent::YES;
}

private:
std::string name_;
std::string id_;
ValueType type_;
bool timeDependent_ = true; // optional at construction
bool scenarioDependent_ = true; // optional at construction
TimeDependent timeDependent_ = TimeDependent::YES; // optional at construction
ScenarioDependent scenarioDependent_ = ScenarioDependent::YES; // optional at construction
};

} // namespace Antares::Solver::ObjectModel
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,18 @@ namespace Antares::Solver::ObjectModel
class Port
{
public:
Port();
~Port() = default;
const std::string& Id() const
{
return id_;
}

PortType Type() const
{
return type_;
}

private:
std::string name_;
std::string id_;
PortType type_;
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,19 @@ namespace Antares::Solver::ObjectModel

class PortField
{
public:
explicit PortField(const std::string& id):
id_(id)
{
}

const std::string& Id() const
{
return id_;
}

private:
std::string name;
std::string id_;
};

} // namespace Antares::Solver::ObjectModel
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,29 @@ namespace Antares::Solver::ObjectModel
class PortType
{
public:
PortType();
~PortType() = default;
PortType(const std::string& id,
const std::string& description,
std::vector<PortField>&& fields):
id_(id),
description_(description),
fields_(std::move(fields))
{
}

const std::string& Id() const
{
return id_;
}

const std::string& Description() const
{
return description_;
}

const std::vector<PortField>& Fields() const
{
return fields_;
}

private:
std::string id_;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,36 @@ namespace Antares::Solver::ObjectModel
class Variable
{
public:
Variable();
~Variable() = default;
Variable(std::string id, Expression lower_bound, Expression upper_bound, ValueType type):
id_(std::move(id)),
type_(type),
lowerBound_(lower_bound),
upperBound_(upper_bound)
{
}

const std::string& Id() const
{
return id_;
}

ValueType Type() const
{
return type_;
}

Expression LowerBound() const
{
return lowerBound_;
}

Expression UpperBound() const
{
return upperBound_;
}

private:
std::string name_;
std::string id_;
ValueType type_;
Expression lowerBound_;
Expression upperBound_;
Expand Down
Loading

0 comments on commit af9f559

Please sign in to comment.