Skip to content

Commit

Permalink
refactor operators and new version of idol_Sum
Browse files Browse the repository at this point in the history
  • Loading branch information
hlefebvr committed Nov 23, 2024
1 parent ed72b02 commit bbf3234
Show file tree
Hide file tree
Showing 12 changed files with 222 additions and 881 deletions.
12 changes: 5 additions & 7 deletions dev/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "idol/mixed-integer/optimizers/branch-and-bound/BranchAndBound.h"
#include "idol/mixed-integer/modeling/expressions/QuadExpr.h"
#include "idol/general/utils/GenerationPattern.h"
#include "idol/mixed-integer/modeling/models/Dualizer.h"

using namespace idol;

Expand All @@ -29,16 +30,13 @@ int main(int t_argc, const char** t_argv) {

const auto x = model.add_vars(Dim<1>(10), 0, 1, Binary, 1, "x");

Point<Var> point;
point.set(x[0], 5);
Dualizer dualizer(model);

GenerationPattern<Ctr> pattern;
pattern.constant() += 2 + 2 * x[0];
auto s = idol_Sum(i, Range(2), i);

pattern.linear().set(x[1], x[0]);
pattern.linear().set(x[2], 10 * x[0] + 4);
auto sum = idol_Sum(i, Range(0), x[i]);

std::cout << pattern(point) << std::endl;
std::cout << sum << std::endl;

return 0;
}
2 changes: 2 additions & 0 deletions lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,8 @@ add_library(idol STATIC
src/mixed-integer/modeling/constraints/QCtrVersion.cpp
src/mixed-integer/modeling/constraints/QCtr.cpp
include/idol/general/utils/GenerationPattern.h
include/idol/mixed-integer/modeling/models/Dualizer.h
src/mixed-integer/modeling/models/Dualizer.cpp
)

find_package(OpenMP REQUIRED)
Expand Down
10 changes: 5 additions & 5 deletions lib/include/idol/general/utils/Map.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,13 @@ namespace idol {
namespace idol {

template<
class Key,
class KeyT,
class T,
class Hash = impl::hash<Key>,
class KeyEqual = std::equal_to<Key>,
class Allocator = std::allocator<std::pair<const Key, T> >
class Hash = impl::hash<KeyT>,
class KeyEqual = std::equal_to<KeyT>,
class Allocator = std::allocator<std::pair<const KeyT, T> >
>
using Map = std::unordered_map<Key, T, Hash, KeyEqual, Allocator>;
using Map = std::unordered_map<KeyT, T, Hash, KeyEqual, Allocator>;

}

Expand Down
43 changes: 16 additions & 27 deletions lib/include/idol/mixed-integer/modeling/expressions/AffExpr.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,19 @@
namespace idol {
class Var;

template<class Key1, class ValueT>
template<class KeyT, class ValueT>
class AffExpr;
}

template<class Key1 = idol::Var, class ValueT = double>
template<class KeyT = idol::Var, class ValueT = double>
class idol::AffExpr {
LinExpr<Key1, ValueT> m_linear;
LinExpr<KeyT, ValueT> m_linear;
double m_constant = 0.;
public:
AffExpr();
AffExpr(const ValueT& t_constant); // NOLINT(google-explicit-constructor)
AffExpr(ValueT&& t_constant); // NOLINT(google-explicit-constructor)
AffExpr(const Key1& t_var); // NOLINT(google-explicit-constructor)
AffExpr(LinExpr<Key1>&& t_expr); // NOLINT(google-explicit-constructor)
AffExpr(const LinExpr<Key1>& t_expr); // NOLINT(google-explicit-constructor)
AffExpr(ValueT t_constant); // NOLINT(google-explicit-constructor)
AffExpr(const KeyT& t_key); // NOLINT(google-explicit-constructor)
AffExpr(LinExpr<KeyT> t_expr); // NOLINT(google-explicit-constructor)

virtual ~AffExpr() = default;

Expand All @@ -36,13 +34,14 @@ class idol::AffExpr {
AffExpr& operator=(AffExpr&&) noexcept = default;

AffExpr& operator+=(const AffExpr& t_rhs);

AffExpr& operator-=(const AffExpr& t_rhs);
AffExpr& operator*=(double t_rhs);
AffExpr& operator/=(double t_rhs);
AffExpr operator-() const;

LinExpr<Key1, ValueT>& linear() { return m_linear; }
[[nodiscard]] const LinExpr<Key1, ValueT>& linear() const { return m_linear; }
LinExpr<KeyT, ValueT>& linear() { return m_linear; }
[[nodiscard]] const LinExpr<KeyT, ValueT>& linear() const { return m_linear; }

double& constant() { return m_constant; }

Expand All @@ -56,6 +55,11 @@ class idol::AffExpr {
}
};

template<class KeyT, class ValueT>
idol::AffExpr<KeyT, ValueT>::AffExpr(const KeyT &t_key) : m_linear(t_key) {

}

template<class Key1, class ValueT>
idol::AffExpr<Key1, ValueT> idol::AffExpr<Key1, ValueT>::operator-() const {
auto result = *this;
Expand All @@ -70,27 +74,12 @@ idol::AffExpr<Key1, ValueT>::AffExpr() {
}

template<class Key1, class ValueT>
idol::AffExpr<Key1, ValueT>::AffExpr(const ValueT& t_constant) : m_constant(t_constant) {

}

template<class Key1, class ValueT>
idol::AffExpr<Key1, ValueT>::AffExpr(ValueT&& t_constant) : m_constant(t_constant) {

}

template<class Key1, class ValueT>
idol::AffExpr<Key1, ValueT>::AffExpr(const Key1 &t_var) : m_linear(t_var) {

}

template<class Key1, class ValueT>
idol::AffExpr<Key1, ValueT>::AffExpr(LinExpr<Key1> &&t_expr) : m_linear(std::move(t_expr)) {
idol::AffExpr<Key1, ValueT>::AffExpr(ValueT t_constant) : m_constant(std::move(t_constant)) {

}

template<class Key1, class ValueT>
idol::AffExpr<Key1, ValueT>::AffExpr(const LinExpr<Key1> &t_expr) : m_linear(t_expr) {
idol::AffExpr<Key1, ValueT>::AffExpr(LinExpr<Key1> t_expr) : m_linear(std::move(t_expr)) {

}

Expand Down
52 changes: 44 additions & 8 deletions lib/include/idol/mixed-integer/modeling/expressions/LinExpr.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,25 +22,61 @@ namespace idol {
}

/**
* @tparam Key the class representing keys
* @tparam KeyT the class representing keys
*/
template<class Key = idol::Var, class ValueT = double>
class idol::LinExpr : public SparseVector<Key, ValueT> {
template<class KeyT = idol::Var, class ValueT = double>
class idol::LinExpr : public SparseVector<KeyT, ValueT> {
public:
LinExpr() = default;
LinExpr(SparseVector<Key, ValueT>&& t_vector) : SparseVector<Key, ValueT>(std::move(t_vector)) {}
LinExpr(const Key& t_key); // NOLINT(google-explicit-constructor)
LinExpr(const ValueT& t_factor, const Key& t_key);
LinExpr(ValueT&& t_factor, const Key& t_key);
LinExpr(KeyT t_key); // NOLINT(google-explicit-constructor)
LinExpr(SparseVector<KeyT, ValueT> t_vector) : SparseVector<KeyT, ValueT>(std::move(t_vector)) {} // NOLINT(*-explicit-constructor)
LinExpr(const ValueT& t_factor, const KeyT& t_key);
LinExpr(ValueT&& t_factor, const KeyT& t_key);

LinExpr(const LinExpr<KeyT, ValueT>&) = default;
LinExpr(LinExpr<KeyT, ValueT>&&) = default;

LinExpr& operator=(const LinExpr<KeyT, ValueT>&) noexcept = default;
LinExpr& operator=(LinExpr<KeyT, ValueT>&&) noexcept = default;

LinExpr& operator+=(const LinExpr<KeyT, ValueT>& t_rhs);
LinExpr& operator+=(const KeyT& t_rhs);

LinExpr& operator-=(const LinExpr<KeyT, ValueT>& t_rhs);
LinExpr& operator-=(const KeyT& t_rhs);
};

template<class KeyT, class ValueT>
idol::LinExpr<KeyT, ValueT> &idol::LinExpr<KeyT, ValueT>::operator-=(const KeyT &t_rhs) {
SparseVector<KeyT, ValueT>::operator-=(SparseVector<KeyT, ValueT>(t_rhs, 1));
return *this;
}

template<class KeyT, class ValueT>
idol::LinExpr<KeyT, ValueT> &idol::LinExpr<KeyT, ValueT>::operator-=(const idol::LinExpr<KeyT, ValueT> &t_rhs) {
SparseVector<KeyT, ValueT>::operator-=((SparseVector<KeyT, ValueT>&) t_rhs);
return *this;
}

template<class KeyT, class ValueT>
idol::LinExpr<KeyT, ValueT> &idol::LinExpr<KeyT, ValueT>::operator+=(const KeyT &t_rhs) {
SparseVector<KeyT, ValueT>::operator+=(SparseVector<KeyT, ValueT>(t_rhs, 1));
return *this;
}

template<class Key, class ValueT>
idol::LinExpr<Key, ValueT>& idol::LinExpr<Key, ValueT>::operator+=(const idol::LinExpr<Key, ValueT> &t_rhs) {
SparseVector<Key, ValueT>::operator+=((SparseVector<Key, ValueT>&) t_rhs);
return *this;
}

template<class Key, class ValueT>
idol::LinExpr<Key, ValueT>::LinExpr(ValueT &&t_factor, const Key &t_key) : SparseVector<Key, ValueT>(t_key, std::move(t_factor)) {

}

template<class Key, class ValueT>
idol::LinExpr<Key, ValueT>::LinExpr(const Key &t_key) : SparseVector<Key, double>(t_key, 1.) {
idol::LinExpr<Key, ValueT>::LinExpr(Key t_key) : SparseVector<Key, double>(t_key, 1.) {

}

Expand Down
19 changes: 8 additions & 11 deletions lib/include/idol/mixed-integer/modeling/expressions/QuadExpr.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,11 @@ class idol::QuadExpr : public LinExpr<CommutativePair<KeyT>, ValueT> {
AffExpr<KeyT, ValueT> m_affine;
public:
QuadExpr() = default;
QuadExpr(const KeyT& t_key) : m_affine(t_key) {}
QuadExpr(KeyT&& t_key) : m_affine(std::move(t_key)) {}
QuadExpr(const ValueT& t_value) : m_affine(t_value) {}
QuadExpr(ValueT&& t_value) : m_affine(std::move(t_value)) {}
QuadExpr(const LinExpr<KeyT, ValueT>& t_expr) : m_affine(t_expr) {} // NOLINT(*-explicit-constructor)
QuadExpr(LinExpr<KeyT, ValueT>&& t_expr) : m_affine(std::move(t_expr)) {} // NOLINT(*-explicit-constructor)
QuadExpr(const AffExpr<KeyT, ValueT>& t_expr) : m_affine(t_expr) {} // NOLINT(*-explicit-constructor)
QuadExpr(AffExpr<KeyT, ValueT>&& t_expr) : m_affine(std::move(t_expr)) {} // NOLINT(*-explicit-constructor)
QuadExpr(ValueT t_constant) : m_affine(std::move(t_constant)) {} // NOLINT(*-explicit-constructor)
QuadExpr(const KeyT& t_key) : m_affine(std::move(t_key)) {} // NOLINT(*-explicit-constructor)
QuadExpr(LinExpr<KeyT, ValueT> t_expr) : m_affine(std::move(t_expr)) {} // NOLINT(*-explicit-constructor)
QuadExpr(AffExpr<KeyT, ValueT> t_expr) : m_affine(std::move(t_expr)) {} // NOLINT(*-explicit-constructor)

QuadExpr(const KeyT& t_key1, const KeyT& t_key2) : LinExpr<CommutativePair<KeyT>, ValueT>(CommutativePair<KeyT>(t_key1, t_key2)) {}
QuadExpr(const ValueT& t_factor, const KeyT& t_key1, const KeyT& t_key2) : LinExpr<CommutativePair<KeyT>, ValueT>(CommutativePair<KeyT>(t_key1, t_key2), t_factor) {}
QuadExpr(ValueT&& t_factor, const KeyT& t_key1, const KeyT& t_key2) : LinExpr<CommutativePair<KeyT>, ValueT>(CommutativePair<KeyT>(t_key1, t_key2), std::move(t_factor)) {}
Expand All @@ -46,11 +43,11 @@ class idol::QuadExpr : public LinExpr<CommutativePair<KeyT>, ValueT> {
AffExpr<KeyT, ValueT>& affine() { return m_affine; }
const AffExpr<KeyT, ValueT>& affine() const { return m_affine; }

bool has_quadratic() const { return !LinExpr<CommutativePair<KeyT>, ValueT>::empty(); }
[[nodiscard]] bool has_quadratic() const { return !LinExpr<CommutativePair<KeyT>, ValueT>::empty(); }

bool empty_all() const { return LinExpr<CommutativePair<KeyT>, ValueT>::empty() && m_affine.linear().empty(); }
[[nodiscard]] bool empty_all() const { return LinExpr<CommutativePair<KeyT>, ValueT>::empty() && m_affine.linear().empty(); }

bool is_zero(double t_tolerance) const override;
[[nodiscard]] bool is_zero(double t_tolerance) const override;

void clear_all();
};
Expand Down
Loading

0 comments on commit bbf3234

Please sign in to comment.