Commit 0b6b0943 authored by ehebrard's avatar ehebrard
Browse files

prof

parent 68c7a036
...@@ -7,6 +7,8 @@ ...@@ -7,6 +7,8 @@
#include "Global.hpp" #include "Global.hpp"
#include "SparseSet.hpp" #include "SparseSet.hpp"
#define DEBUG_BELLMANFORD
using namespace std; using namespace std;
namespace schedcl { namespace schedcl {
...@@ -28,13 +30,27 @@ public: ...@@ -28,13 +30,27 @@ public:
// updates all shortest paths from x (and through y) and put them in // updates all shortest paths from x (and through y) and put them in
// 'shortest_path'. Sets the flag 'negative_cycle' if it finds a negative // 'shortest_path'. Sets the flag 'negative_cycle' if it finds a negative
// cycle including x-y // cycle including x-y
template <class G> template <class G
#ifdef DEBUG_BELLMANFORD
,
class P
#endif
>
void allShorterPaths(const int x, const int y, const T wxy, void allShorterPaths(const int x, const int y, const T wxy,
const G &neighbors, vector<T> &shortest_path, const G &neighbors, vector<T> &shortest_path,
SparseSet<> &reached); SparseSet<> &reached
#ifdef DEBUG_BELLMANFORD
,
P &printer
#endif
);
bool negative_cycle{false}; bool negative_cycle{false};
#ifdef DEBUG_BELLMANFORD
bool debug_flag{false};
#endif
// vector<T> shortest_path; // vector<T> shortest_path;
// SparseSet<> reached; // SparseSet<> reached;
...@@ -98,8 +114,29 @@ void BellmanFord<T>::allShortestPaths(const int s, const G& neighbors, vector<T> ...@@ -98,8 +114,29 @@ void BellmanFord<T>::allShortestPaths(const int s, const G& neighbors, vector<T>
} }
template <typename T> template <typename T>
template<class G> template <class G
void BellmanFord<T>::allShorterPaths(const int x, const int y, const T wxy, const G& neighbors, vector<T>& shortest_path, SparseSet<>& reached) { #ifdef DEBUG_BELLMANFORD
,
class P
#endif
>
void BellmanFord<T>::allShorterPaths(const int x, const int y, const T wxy,
const G &neighbors,
vector<T> &shortest_path,
SparseSet<> &reached
#ifdef DEBUG_BELLMANFORD
,
P &printer
#endif
) {
assert(changed.empty());
#ifdef DEBUG_BELLMANFORD
if (debug_flag)
cout << " ASP from " << printer.label(y) << " after edge "
<< printer.label(x) << "->" << printer.label(y) << " (" << wxy
<< ")\n";
#endif
shortest_path[y] = wxy; shortest_path[y] = wxy;
...@@ -108,16 +145,52 @@ void BellmanFord<T>::allShorterPaths(const int x, const int y, const T wxy, cons ...@@ -108,16 +145,52 @@ void BellmanFord<T>::allShorterPaths(const int x, const int y, const T wxy, cons
while (not changed.empty()) { while (not changed.empty()) {
auto u{changed.front()}; auto u{changed.front()};
#ifdef DEBUG_BELLMANFORD
if (debug_flag) {
cout << printer.label(u) << " (" << shortest_path[u] << ") stack:";
for (auto q : changed)
cout << " " << printer.label(q);
cout << " vis:";
for (auto q : reached)
cout << " " << printer.label(q);
cout << endl;
}
#endif
changed.pop_front(); changed.pop_front();
for (auto e : neighbors[u]) { for (auto e : neighbors[u]) {
auto v{e.neighbor()}; auto v{e.neighbor()};
auto w{e.length()}; auto w{e.length()};
#ifdef DEBUG_BELLMANFORD
if (debug_flag)
cout << " -" << printer.label(v) << " (" << shortest_path[u] << "+" << w
<< "/" << shortest_path[v] << ")\n";
#endif
if (w != INFTY and shortest_path[u] + w < shortest_path[v]) { if (w != INFTY and shortest_path[u] + w < shortest_path[v]) {
if (v == x and wxy + w < 0) { #ifdef DEBUG_BELLMANFORD
if (debug_flag)
cout << " explore\n";
#endif
if (v == x and wxy + shortest_path[u] + w < 0) {
#ifdef DEBUG_BELLMANFORD
if (debug_flag)
cout << "created a negative cycle between " << printer.label(x)
<< " and " << printer.label(y) << " (" << wxy << " + "
<< shortest_path[u] << " + " << w << ")" << endl;
#endif
negative_cycle = true; negative_cycle = true;
changed.clear();
// exit(1);
return; return;
} }
...@@ -127,6 +200,10 @@ void BellmanFord<T>::allShorterPaths(const int x, const int y, const T wxy, cons ...@@ -127,6 +200,10 @@ void BellmanFord<T>::allShorterPaths(const int x, const int y, const T wxy, cons
if (not reached.has(v)) if (not reached.has(v))
reached.add(v); reached.add(v);
} }
#ifdef DEBUG_BELLMANFORD
else if (debug_flag)
cout << " reject\n";
#endif
} }
} }
} }
......
...@@ -139,6 +139,7 @@ public: ...@@ -139,6 +139,7 @@ public:
// get the current distance // get the current distance
T get() const { return literals.back().value; } T get() const { return literals.back().value; }
operator T() const { return literals.back().value; }
const Bound<T> &back() const { return literals.back(); } const Bound<T> &back() const { return literals.back(); }
......
This diff is collapsed.
...@@ -139,6 +139,7 @@ private: ...@@ -139,6 +139,7 @@ private:
template <typename T> SparseGraph<T>::SparseGraph() : ReversibleObject() {} template <typename T> SparseGraph<T>::SparseGraph() : ReversibleObject() {}
template <typename T> void SparseGraph<T>::addArc(DistanceVariable<T> *v) { template <typename T> void SparseGraph<T>::addArc(DistanceVariable<T> *v) {
auto pto{static_cast<int>(neighbor[PREDECESSOR][v->to].size())}; auto pto{static_cast<int>(neighbor[PREDECESSOR][v->to].size())};
auto pfrom{static_cast<int>(neighbor[SUCCESSOR][v->from].size())}; auto pfrom{static_cast<int>(neighbor[SUCCESSOR][v->from].size())};
......
...@@ -125,28 +125,28 @@ CumulativeTimeTabling<T, C>::CumulativeTimeTabling(Schedule<T> &schedule, ...@@ -125,28 +125,28 @@ CumulativeTimeTabling<T, C>::CumulativeTimeTabling(Schedule<T> &schedule,
// ORIGIN <= START(i) // ORIGIN <= START(i)
distance[START(i)][ORIGIN] = distance[START(i)][ORIGIN] =
m_schedule.addPrecedence(ORIGIN, START(m_tasks[i])); m_schedule.addPrecedence(ORIGIN, START(m_tasks[i]), -INFTY);
// START(i) <= ORIGIN + oo // START(i) <= ORIGIN + oo
distance[ORIGIN][START(i)] = distance[ORIGIN][START(i)] =
m_schedule.addMaximumLag(ORIGIN, START(m_tasks[i]), INFTY); m_schedule.addMaximumLag(ORIGIN, START(m_tasks[i]), INFTY);
// ORIGIN <= END(i) // ORIGIN <= END(i)
distance[END(i)][ORIGIN] = distance[END(i)][ORIGIN] =
m_schedule.addPrecedence(ORIGIN, END(m_tasks[i])); m_schedule.addPrecedence(ORIGIN, END(m_tasks[i]), -INFTY);
// END(i) <= ORIGIN + oo // END(i) <= ORIGIN + oo
distance[ORIGIN][END(i)] = distance[ORIGIN][END(i)] =
m_schedule.addMaximumLag(ORIGIN, END(m_tasks[i]), INFTY); m_schedule.addMaximumLag(ORIGIN, END(m_tasks[i]), INFTY);
// END(i) <= HORIZON // END(i) <= HORIZON
distance[HORIZON][END(i)] = distance[HORIZON][END(i)] =
m_schedule.addPrecedence(END(m_tasks[i]), HORIZON); m_schedule.addPrecedence(END(m_tasks[i]), HORIZON, -INFTY);
// HORIZON <= END(i) + oo // HORIZON <= END(i) + oo
distance[END(i)][HORIZON] = distance[END(i)][HORIZON] =
m_schedule.addMaximumLag(END(m_tasks[i]), HORIZON, INFTY); m_schedule.addMaximumLag(END(m_tasks[i]), HORIZON, INFTY);
// START(i) <= HORIZON // START(i) <= HORIZON
distance[HORIZON][START(i)] = distance[HORIZON][START(i)] =
m_schedule.addPrecedence(START(m_tasks[i]), HORIZON); m_schedule.addPrecedence(START(m_tasks[i]), HORIZON, -INFTY);
// HORIZON <= START(i) + oo // HORIZON <= START(i) + oo
distance[START(i)][HORIZON] = distance[START(i)][HORIZON] =
m_schedule.addMaximumLag(START(m_tasks[i]), HORIZON, INFTY); m_schedule.addMaximumLag(START(m_tasks[i]), HORIZON, INFTY);
...@@ -227,19 +227,16 @@ void CumulativeTimeTabling<T, C>::post(const int idx) { ...@@ -227,19 +227,16 @@ void CumulativeTimeTabling<T, C>::post(const int idx) {
#ifdef DEBUG_CONSTRAINT #ifdef DEBUG_CONSTRAINT
if (this->ResourceConstraint<T>::debug_flag) { if (this->ResourceConstraint<T>::debug_flag) {
cout << endl << "post timetabling("; cout << "post " << *this << endl;
for (auto t : m_tasks) {
cout << " " << m_schedule.taskLabel(t) << " " << m_schedule.domain(t);
}
cout << ")\n";
} }
#endif #endif
int k{0}; // int k{0};
for(int i{0}; i<distance.size(); ++i) { for(int i{0}; i<distance.size(); ++i) {
for(int j{i+1}; j<distance.size(); ++j) { for(int j{i+1}; j<distance.size(); ++j) {
m_schedule.wake_me_on(distance[i][j], idx, k++); m_schedule.wake_me_on(distance[i][j], idx, i*distance.size()+j);
m_schedule.wake_me_on(distance[j][i], idx, j*distance.size()+i);
} }
} }
...@@ -520,7 +517,13 @@ void CumulativeTimeTabling<T, C>::propagate() { ...@@ -520,7 +517,13 @@ void CumulativeTimeTabling<T, C>::propagate() {
template <class T, class C> template <class T, class C>
ostream &CumulativeTimeTabling<T, C>::display(ostream &os) const { ostream &CumulativeTimeTabling<T, C>::display(ostream &os) const {
os << "TimeTabling("; os << "TimeTabling";
#ifdef DEBUG_CONSTRAINT
os << "[" << ResourceConstraint<T>::id << "]";
#endif
os << "(";
for (auto t : m_tasks) { for (auto t : m_tasks) {
cout << " " << m_schedule.taskLabel(t) ; cout << " " << m_schedule.taskLabel(t) ;
} }
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment