Antares Simulator
Power System Simulator
Loading...
Searching...
No Matches
LinearExpression.h
1/*
2 * Copyright 2007-2024, RTE (https://www.rte-france.com)
3 * See AUTHORS.txt
4 * SPDX-License-Identifier: MPL-2.0
5 * This file is part of Antares-Simulator,
6 * Adequacy and Performance assessment for interconnected energy networks.
7 *
8 * Antares_Simulator is free software: you can redistribute it and/or modify
9 * it under the terms of the Mozilla Public Licence 2.0 as published by
10 * the Mozilla Foundation, either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * Antares_Simulator is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * Mozilla Public Licence 2.0 for more details.
17 *
18 * You should have received a copy of the Mozilla Public Licence 2.0
19 * along with Antares_Simulator. If not, see <https://opensource.org/license/mpl-2-0/>.
20 */
21
22#pragma once
23
24#include <string>
25#include <unordered_map>
26
27#include <antares/solver/optim-model-filler/FullKey.h>
28
30{
31
32template<typename T>
34{
35 const T& operator()(const T& t)
36 {
37 return t;
38 }
39};
40
41using FullKeyMap = std::unordered_map<FullKey, double, FullKeyHash>;
42
51template<typename Key,
52 typename Value,
53 typename UnaryOp = IdentityFunction<Value>,
54 typename HashType = std::hash<Key>>
55std::unordered_map<Key, Value, HashType> add_maps(
56 const std::unordered_map<Key, Value, HashType>& left,
57 const std::unordered_map<Key, Value, HashType>& right,
58 UnaryOp op = IdentityFunction<Value>{})
59{
60 auto result(left);
61 for (auto [key, value]: right)
62 {
63 if (result.contains(key))
64 {
65 result[key] += op(value);
66 }
67 else
68 {
69 result[key] = op(value);
70 }
71 }
72 return result;
73}
74
82FullKeyMap scale_map(const FullKeyMap& map, double scale);
83
92{
93public:
95 LinearExpression() = default;
98 LinearExpression(double offset, FullKeyMap coef_per_var);
100 LinearExpression operator+(const LinearExpression& other) const;
102 LinearExpression operator-(const LinearExpression& other) const;
105 LinearExpression operator*(const LinearExpression& other) const;
108 LinearExpression operator/(const LinearExpression& other) const;
111
113 double offset() const;
114
116 const FullKeyMap& coefPerVar() const;
117
118 LinearExpression& operator+=(const LinearExpression& value);
119
120private:
121 double offset_ = 0;
122 FullKeyMap coef_per_var_;
123};
124
125} // namespace Antares::Optimization
Definition LinearExpression.h:92
const FullKeyMap & coefPerVar() const
Get the non-zero coefficients per variable ID.
Definition LinearExpression.cpp:58
LinearExpression operator/(const LinearExpression &other) const
Definition LinearExpression.cpp:92
LinearExpression operator-() const
Multiply linear expression by -1.
Definition LinearExpression.cpp:101
double offset() const
Get the offset.
Definition LinearExpression.cpp:106
LinearExpression()=default
Build a linear expression with zero offset and zero coefficients.
LinearExpression operator*(const LinearExpression &other) const
Definition LinearExpression.cpp:76
LinearExpression operator+(const LinearExpression &other) const
Sum two linear expressions.
Definition LinearExpression.cpp:53
Definition constraint-slack-analysis.cpp:45
FullKeyMap scale_map(const FullKeyMap &map, double scale)
Definition LinearExpression.cpp:37
std::unordered_map< Key, Value, HashType > add_maps(const std::unordered_map< Key, Value, HashType > &left, const std::unordered_map< Key, Value, HashType > &right, UnaryOp op=IdentityFunction< Value >{})
Definition LinearExpression.h:55
Definition LinearExpression.h:34