TVM  0.9.4
SolverEvents.h
Go to the documentation of this file.
1 /* Copyright 2017-2020 CNRS-AIST JRL and CNRS-UM LIRMM */
2 
3 #pragma once
4 
5 #include <tvm/api.h>
6 #include <tvm/defs.h>
7 
8 #include <vector>
9 
11 {
14 {
15 public:
16  struct WeightEvent
17  {
19  bool scalar;
20  bool vector;
21  };
22 
23  struct Objective
24  {
28  };
29 
32 
37  void addObjective(const Objective & o);
39 
40  void addVariable(VariablePtr v);
42 
43  const std::vector<WeightEvent> & weightEvents() const { return weightEvents_; }
44  const std::vector<LinearConstraintPtr> & addedConstraints() const { return addedConstraints_; }
45  const std::vector<LinearConstraintPtr> & addedBounds() const { return addedBounds_; }
46  const std::vector<Objective> & addedObjectives() const { return addedObjectives_; }
47  const std::vector<LinearConstraintPtr> & removedConstraints() const { return removedConstraints_; }
48  const std::vector<LinearConstraintPtr> & removedBounds() const { return removedBounds_; }
49  const std::vector<LinearConstraintPtr> & removedObjectives() const { return removedObjectives_; }
50 
51  const std::vector<VariablePtr> & addedVariables() const { return addedVariables_; }
52  const std::vector<VariablePtr> & removedVariables() const { return removedVariables_; }
53 
59  bool hasHiddenVariableChange() const { return hiddenVariableChange_; }
60 
61 private:
63  template<typename T>
64  bool addIfPair(T & c, std::vector<T> & addVec, std::vector<T> & removeVec);
65 
69  std::vector<WeightEvent> weightEvents_;
70 
71  std::vector<LinearConstraintPtr> addedConstraints_;
72  std::vector<LinearConstraintPtr> addedBounds_;
73  std::vector<Objective> addedObjectives_;
74  std::vector<LinearConstraintPtr> removedConstraints_;
75  std::vector<LinearConstraintPtr> removedBounds_;
76  std::vector<LinearConstraintPtr> removedObjectives_;
77 
78  std::vector<VariablePtr> addedVariables_;
79  std::vector<VariablePtr> removedVariables_;
80  bool hiddenVariableChange_ = false;
81 };
82 
84 {
85  for(auto & e : weightEvents_)
86  {
87  if(e.c == c)
88  {
89  e.scalar = true;
90  return;
91  }
92  }
93 
94  weightEvents_.push_back({c, true, false});
95 }
96 
98 {
99  for(auto & e : weightEvents_)
100  {
101  if(e.c == c)
102  {
103  e.vector = true;
104  return;
105  }
106  }
107 
108  weightEvents_.push_back({c, false, true});
109 }
110 
111 inline void SolverEvents::addConstraint(LinearConstraintPtr c) { addIfPair(c, addedConstraints_, removedConstraints_); }
112 
114 { addIfPair(c, removedConstraints_, addedConstraints_); }
115 
116 inline void SolverEvents::addBound(LinearConstraintPtr b) { addIfPair(b, addedBounds_, removedBounds_); }
117 
118 inline void SolverEvents::removeBound(LinearConstraintPtr b) { addIfPair(b, removedBounds_, addedBounds_); }
119 
120 inline void SolverEvents::addObjective(const Objective & o)
121 {
122  auto it = std::find(removedObjectives_.begin(), removedObjectives_.end(), o.c);
123  if(it == removedObjectives_.end())
124  addedObjectives_.push_back(o);
125  else
126  removedObjectives_.erase(it);
127 }
128 
130 {
131  auto it = std::find_if(addedObjectives_.begin(), addedObjectives_.end(), [&o](const auto & it) { return it.c == o; });
132  if(it == addedObjectives_.end())
133  removedObjectives_.push_back(o);
134  else
135  addedObjectives_.erase(it);
136 }
137 
139 {
140  if(!addIfPair(v, addedVariables_, removedVariables_))
141  hiddenVariableChange_ = true;
142 }
143 
145 {
146  if(!addIfPair(v, removedVariables_, addedVariables_))
147  hiddenVariableChange_ = true;
148 }
149 
150 template<typename T>
151 inline bool SolverEvents::addIfPair(T & c, std::vector<T> & addVec, std::vector<T> & removeVec)
152 {
153  auto it = std::find(removeVec.begin(), removeVec.end(), c);
154  bool notFound = it == removeVec.end();
155  if(notFound)
156  addVec.push_back(c);
157  else
158  removeVec.erase(it);
159  return notFound;
160 }
161 } // namespace tvm::solver::internal
Definition: LinearConstraint.h:56
Definition: SolverEvents.h:14
const std::vector< VariablePtr > & addedVariables() const
Definition: SolverEvents.h:51
void removeBound(LinearConstraintPtr c)
Definition: SolverEvents.h:118
void addConstraint(LinearConstraintPtr c)
Definition: SolverEvents.h:111
const std::vector< WeightEvent > & weightEvents() const
Definition: SolverEvents.h:43
const std::vector< LinearConstraintPtr > & addedConstraints() const
Definition: SolverEvents.h:44
const std::vector< LinearConstraintPtr > & addedBounds() const
Definition: SolverEvents.h:45
const std::vector< LinearConstraintPtr > & removedObjectives() const
Definition: SolverEvents.h:49
const std::vector< LinearConstraintPtr > & removedConstraints() const
Definition: SolverEvents.h:47
void addVectorWeightEvent(constraint::abstract::LinearConstraint *c)
Definition: SolverEvents.h:97
void addVariable(VariablePtr v)
Definition: SolverEvents.h:138
const std::vector< Objective > & addedObjectives() const
Definition: SolverEvents.h:46
void addScalarWeightEvent(constraint::abstract::LinearConstraint *c)
Definition: SolverEvents.h:83
void addBound(LinearConstraintPtr c)
Definition: SolverEvents.h:116
void removeVariable(VariablePtr v)
Definition: SolverEvents.h:144
void addObjective(const Objective &o)
Definition: SolverEvents.h:120
void removeObjective(LinearConstraintPtr o)
Definition: SolverEvents.h:129
void removeConstraint(LinearConstraintPtr c)
Definition: SolverEvents.h:113
bool hasHiddenVariableChange() const
Definition: SolverEvents.h:59
const std::vector< LinearConstraintPtr > & removedBounds() const
Definition: SolverEvents.h:48
const std::vector< VariablePtr > & removedVariables() const
Definition: SolverEvents.h:52
Definition: SolverEvents.h:11
std::shared_ptr< Variable > VariablePtr
Definition: defs.h:65
std::shared_ptr< constraint::abstract::LinearConstraint > LinearConstraintPtr
Definition: defs.h:59
std::shared_ptr< requirements::SolvingRequirementsWithCallbacks > SolvingRequirementsPtr
Definition: defs.h:63
LinearConstraintPtr c
Definition: SolverEvents.h:25
double scalarizationWeight
Definition: SolverEvents.h:27
SolvingRequirementsPtr req
Definition: SolverEvents.h:26
bool vector
Definition: SolverEvents.h:20
bool scalar
Definition: SolverEvents.h:19
constraint::abstract::LinearConstraint * c
Definition: SolverEvents.h:18