SCIP++ refs/heads/main
 
Loading...
Searching...
No Matches
lin_expr.hpp
1#pragma once
2
3#include <array>
4#include <vector>
5
6#include "scippp/var.hpp"
7
8// just for tests, see test_linexpr.cpp
9namespace LinExpression { // NOLINT(readability-identifier-naming)
10struct CheckInternalsUsingFriendStruct;
11}
12namespace scippp {
13
18class LinExpr {
19 friend class Model;
20 friend struct LinExpression::CheckInternalsUsingFriendStruct; // just for tests, see test_linexpr.cpp
22 double m_constant { 0.0 };
24 std::vector<Var> m_vars {};
26 std::vector<double> m_coeffs {};
27
28public:
33 LinExpr() = default;
42 template <typename Arithmetic, std::enable_if_t<std::is_arithmetic_v<Arithmetic>, bool> = true>
43 LinExpr(Arithmetic constant)
44 : m_constant { static_cast<double>(constant) }
45 {
46 }
53 LinExpr(const Var& var);
60 LinExpr(std::initializer_list<Var> vars);
68 LinExpr(std::initializer_list<Var> vars, std::initializer_list<double> coeffs);
76 template <std::size_t N>
77 LinExpr(const std::array<Var, N>& vars)
78 : m_vars { vars.begin(), vars.end() }
79 , m_coeffs(vars.size(), 1)
80 {
81 }
89 template <std::size_t N>
90 LinExpr(const std::array<Var, N>& vars, std::array<double, N>& coeffs)
91 : m_vars { vars.begin(), vars.end() }
92 , m_coeffs { coeffs.begin(), coeffs.end() }
93 {
94 }
101 LinExpr(const std::vector<Var>& vars);
108 LinExpr(const std::vector<Var>& vars, const std::vector<double>& coeffs);
109
115 [[nodiscard]] double getConstant() const;
116
137 LinExpr& operator*=(double factor);
138};
139
147LinExpr operator*(double factor, LinExpr rhs);
148
157
166
167}
Represents a linear combination of variables plus a constant term.
Definition lin_expr.hpp:18
LinExpr & operator*=(double factor)
Multiply all coefficients.
LinExpr(const Var &var)
Creates a linear expression with zero as constant and the given variable with coefficient one.
LinExpr(const std::array< Var, N > &vars, std::array< double, N > &coeffs)
Creates a linear expression with zero as constant and the given variables with the provided coefficie...
Definition lin_expr.hpp:90
LinExpr & operator+=(const LinExpr &expr)
Add another linear expression to this.
LinExpr(const std::vector< Var > &vars)
Creates a linear expression with zero as constant and the given variables with coefficient one.
LinExpr(std::initializer_list< Var > vars, std::initializer_list< double > coeffs)
Creates a linear expression with zero as constant and the given variables with the provided coefficie...
LinExpr(const std::array< Var, N > &vars)
Creates a linear expression with zero as constant and the given variables with coefficient one.
Definition lin_expr.hpp:77
LinExpr(Arithmetic constant)
Creates a linear expression with no variables.
Definition lin_expr.hpp:43
LinExpr & operator-=(const LinExpr &expr)
Subtract another expression from this.
LinExpr()=default
Sets constant term and linear combination to zero.
double getConstant() const
Returns the constant term of the expression.
LinExpr(std::initializer_list< Var > vars)
Creates a linear expression with zero as constant and the given variables with coefficient one.
LinExpr(const std::vector< Var > &vars, const std::vector< double > &coeffs)
Creates a linear expression with zero as constant and the given variables with the provided coefficie...
A SCIP optimization model.
Definition model.hpp:40
C++ wrapper for SCIP.
LinExpr operator*(double factor, LinExpr rhs)
Scales a linear expression by a factor.
LinExpr operator+(LinExpr lhs, const LinExpr &rhs)
Creates a new linear expression as the sum of two.
LinExpr operator-(LinExpr lhs, const LinExpr &rhs)
Creates the new linear expression lhs - rhs.
Wrapper for a SCIP variable.
Definition var.hpp:17