Skip to content

Commit

Permalink
mock all methods using exprs in Model
Browse files Browse the repository at this point in the history
  • Loading branch information
hlefebvr committed Oct 24, 2024
1 parent d3f55da commit d3ce764
Show file tree
Hide file tree
Showing 31 changed files with 161 additions and 1,116 deletions.
8 changes: 4 additions & 4 deletions dev/main.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#include <iostream>
#include "idol/containers/SparseVector.h"
#include "idol/modeling/variables/Var.h"
#include "idol/modeling/models/Model.h"
#include "idol/modeling/objects/Env.h"
#include "idol/general/utils/SparseVector.h"
#include "idol/mixed-integer/modeling/variables/Var.h"
#include "idol/mixed-integer/modeling/models/Model.h"
#include "idol/mixed-integer/modeling/objects/Env.h"

using namespace idol;

Expand Down
8 changes: 0 additions & 8 deletions lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,6 @@ add_library(idol STATIC
include/idol/mixed-integer/modeling/Types.h
include/idol/mixed-integer/modeling/expressions/Constant.h
src/mixed-integer/modeling/expressions/Constant.cpp
include/idol/mixed-integer/modeling/matrix/AbstractMatrixCoefficient.h
include/idol/mixed-integer/modeling/matrix/MatrixCoefficientReference.h
include/idol/mixed-integer/modeling/matrix/MatrixCoefficient.h
src/mixed-integer/modeling/matrix/MatrixCoefficient.cpp
src/mixed-integer/modeling/matrix/MatrixCoefficientReference.cpp
include/idol/mixed-integer/modeling/constraints/TempCtr.h
src/mixed-integer/modeling/constraints/TempCtr.cpp
include/idol/general/utils/IteratorForward.h
Expand All @@ -23,8 +18,6 @@ add_library(idol STATIC
src/mixed-integer/modeling/matrix/Row.cpp
src/mixed-integer/modeling/matrix/Column.cpp
include/idol/mixed-integer/modeling/matrix/Column.h
include/idol/mixed-integer/modeling/matrix/Matrix.h
src/mixed-integer/modeling/matrix/Matrix.cpp
src/mixed-integer/problems/generalized-assignment-problem/GAP_Instance.cpp
include/idol/mixed-integer/problems/knapsack-problem/KP_Instance.h
src/mixed-integer/problems/knapsack-problem/KP_Instance.cpp
Expand All @@ -46,7 +39,6 @@ add_library(idol STATIC
include/idol/mixed-integer/problems/helpers/parse_delimited.h
include/idol/mixed-integer/modeling/expressions/operations/operators_utils.h
include/idol/mixed-integer/modeling/expressions/QuadExpr.h
include/idol/mixed-integer/modeling/expressions/AbstractExpr.h
include/idol/mixed-integer/modeling/models/Model.h
include/idol/mixed-integer/modeling/objects/Env.h
include/idol/mixed-integer/modeling/variables/Var.h
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,19 @@ namespace idol {
// Create index mapping
MatrixIndices indices;

for (const auto &[var1, var2, constant]: t_expr) {
indices.add(var1);
indices.add(var2);
for (const auto &[vars, constant]: t_expr) {
indices.add(vars.first);
indices.add(vars.second);
}

// Create quadratic form matrix s.t. quadratic_part = x^TQx
SquareMatrix Q(indices);

for (const auto &[var1, var2, constant]: t_expr) {
if (var1.id() == var2.id()) {
Q.set(var1, var2, constant);
for (const auto &[vars, constant]: t_expr) {
if (vars.first.id() == vars.second.id()) {
Q.set(vars.first, vars.second, constant);
} else {
Q.set(var1, var2, constant / 2.);
Q.set(vars.first, vars.second, constant / 2.);
}
}

Expand Down
9 changes: 9 additions & 0 deletions lib/include/idol/general/utils/Pair.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#ifndef IDOL_PAIR_H
#define IDOL_PAIR_H

#include <iostream>

namespace idol {
template<class T1, class T2> struct Pair;
}
Expand All @@ -26,4 +28,11 @@ struct idol::Pair {
}
};

namespace idol {
template<class T1, class T2>
static std::ostream &operator<<(std::ostream &t_os, const Pair<T1, T2> &t_pair) {
return t_os << t_pair.first << " " << t_pair.second;
}
}

#endif //IDOL_PAIR_H
13 changes: 8 additions & 5 deletions lib/include/idol/general/utils/SparseVector.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,15 @@ namespace idol {

template<class IndexT, class ValueT, class IndexExtractorT = std::conditional_t<std::is_arithmetic_v<IndexT>, idol::identity<IndexT>, idol::get_id<IndexT>>>
class idol::SparseVector {
static_assert(std::is_default_constructible_v<ValueT>);
public:
//static_assert(std::is_default_constructible_v<ValueT>);

enum class SortingCriteria {
Index,
Value,
None
};

private:
std::vector<IndexT> m_indices;
std::vector<ValueT> m_values;
SortingCriteria m_sorting_criteria;
Expand Down Expand Up @@ -53,6 +54,8 @@ class idol::SparseVector {

[[nodiscard]] unsigned int size() const { return m_indices.size(); }

[[nodiscard]] bool empty() const { return m_indices.empty(); }

[[nodiscard]] const IndexT& index_at(unsigned int t_index) const { return m_indices[t_index]; }

[[nodiscard]] const ValueT& value_at(unsigned int t_index) const { return m_values[t_index]; }
Expand Down Expand Up @@ -381,9 +384,9 @@ void idol::SparseVector<IndexT, ValueT, IndexExtractorT>::remove_at(unsigned int

template<class IndexT, class ValueT, class IndexExtractorT>
idol::SparseVector<IndexT, ValueT, IndexExtractorT>::SparseVector(std::vector<IndexT> t_indices,
std::vector<ValueT> t_values,
SortingCriteria t_sorting_criteria,
bool t_is_reduced)
std::vector<ValueT> t_values,
SortingCriteria t_sorting_criteria,
bool t_is_reduced)
: m_indices(std::move(t_indices)),
m_values(std::move(t_values)),
m_sorting_criteria(t_sorting_criteria),
Expand Down
8 changes: 7 additions & 1 deletion lib/include/idol/general/utils/sort.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,26 @@
#include <numeric>
#include <functional>
#include <iostream>
#include "idol/general/utils/Pair.h"

namespace idol {
template<class T> struct get_id;

template<class Key1, class Key2>
struct get_id<std::pair<Key1, Key2>> {
auto operator()(const std::pair<Key1, Key2>& t) const { return std::make_pair(get_id<Key1>(t.first), get_id<Key2>(t.second)); }
auto operator()(const std::pair<Key1, Key2>& t) const { return std::make_pair(get_id<Key1>()(t.first), get_id<Key2>()(t.second)); }
};

template<class T>
struct get_id {
unsigned int operator()(const T& t) const { return t.id(); }
};

template<class Key1, class Key2>
struct get_id<idol::Pair<Key1, Key2>> {
auto operator()(const idol::Pair<Key1, Key2>& t) const { return std::make_pair(get_id<Key1>()(t.first), get_id<Key2>()(t.second)); }
};

template<class T>
struct identity {
const T& operator()(const T& t) const { return t; }
Expand Down
Loading

0 comments on commit d3ce764

Please sign in to comment.