Browse Source

Fix bug (bool ok) + cleaning

Olivier Marty 8 years ago
parent
commit
c38a8ea6f5
3 changed files with 29 additions and 78 deletions
  1. 0 30
      Olivier/instance.cpp
  2. 0 1
      Olivier/instance.h
  3. 29 47
      Olivier/main.cpp

+ 0 - 30
Olivier/instance.cpp

@@ -49,33 +49,3 @@ instance::instance() {
     orders.push_back(order(i, pos(x, y), ord));
   }
 }
-
-int instance::find(pos p, int id, int quantity) { // cherche une warehouse proche de x/y
-  int best = -1, best_quantity = -1;
-  int d = 1000000000.;
-  for(int i = 0; i < W; i++) {
-    if(wh[i].disp[id] >= best_quantity) {
-      int d2 = p.dist2(wh[i].p);
-      if(best_quantity == quantity) {
-        if(d2 < d) {
-          d = d2;
-          best = i;
-        }
-      }
-      else if(wh[i].disp[id] > best_quantity) {
-        d = d2;
-        best = i;
-      }
-      else if(wh[i].disp[id] == best_quantity && d2 < d) {
-        d = d2;
-        best = i;
-      }
-      best_quantity = min(quantity, wh[i].disp[id]);
-    }
-  }
-  if(best < 0) {
-    cerr << "error in find\n";
-    exit(1);
-  }
-  return best;
-}

+ 0 - 1
Olivier/instance.h

@@ -31,7 +31,6 @@ class order {
 class instance {
   public:
     instance(); // read from stdin
-    int find(pos p, int id, int quantity); // cherche une warehouse proche de x/y
     int row, col, drones, max_load;
     int T; // deadline
     int P; // # products

+ 29 - 47
Olivier/main.cpp

@@ -6,23 +6,17 @@ instance ins;
 
 pos drone_pos;
 
-int dist2_wh(const order &a) {
-  int r = 1000000000;
-  for(int i = 0; i < ins.W; i++) {
-    r = min(r, a.p.dist2(ins.wh[i].p));
-  }
-  return r;
-}
-
 int order_d(const order &o) {
-  int d = 1000000000;
-  bool ok = false;
+  int d = -1;
   int poids = 0;
-  int best = -1;
+  int best_wh = -1;
+  if(o.ord.empty())
+    return 2000000000; // INF
   for(auto item : o.ord) {
     poids += item.second*ins.weights[item.first];
   }
   for(int i = 0; i < ins.W; i++) {
+    bool ok = false;
     for(pair<int, int> item : o.ord) {
       if(ins.wh[i].disp[item.first] > 0) {
         ok = true;
@@ -31,37 +25,26 @@ int order_d(const order &o) {
     }
     if(ok) {
       int d2 = drone_pos.dist2(ins.wh[i].p) + ins.wh[i].p.dist2(o.p);
-      if(d2 < d) {
+      if(d < 0 || d2 < d) {
         d = d2;
-        best = i;
+        best_wh = i;
       }
     }
   }
+
+  if(d < 0) {
+    cerr << "! d < 0" << endl;
+    return 2000000000; // INF
+  }
   if(poids > ins.max_load)
     d += ins.col*ins.col + ins.row*ins.row;
-  for(int i = 0; i < ins.orders.size(); i++)
-    if(ins.orders[i].id == o.id)
-      ins.orders[i].best_wh = best;
-  cerr << "okB" << endl;
 
+  ins.orders[o.id].best_wh = best_wh; // (o is const)
   return d;
 }
 
-bool sort_ord(const order &a, const order &b) {
-  // if(a.ord.size() == b.ord.size())
-    return order_d(a) < order_d(b);
-  // return a.ord.size() < b.ord.size();
-}
-
-int next(instance &ins) {
-  int best = -1;
-  for(int i = 0; i < ins.orders.size(); i++) {
-    if(!ins.orders[i].ord.empty()) {
-      best = i;
-      break;
-    }
-  }
-  return best;
+bool cmp_ord(const order &a, const order &b) {
+  return order_d(a) < order_d(b);
 }
 
 class drone {
@@ -87,31 +70,27 @@ void glouton(instance &ins) {
     Q.pop();
 
     drone_pos = dr.p;
-    sort(ins.orders.begin(), ins.orders.end(), sort_ord);
-    int next_i = next(ins);
-    if(next_i < 0)
+    order &ord = *min_element(ins.orders.begin(), ins.orders.end(), cmp_ord);
+
+    if(ord.ord.empty())
       break;
-    order &ord = ins.orders[next_i];
-    assert(!ord.ord.empty());
-    pair<const int, int> &type = *ord.ord.begin();
-    int quantity = min(type.second, ins.max_load/ins.weights[type.first]);
-    // int wa = ins.find(dr.p, type.first, quantity); // TODO
+
     assert(ord.best_wh >= 0);
     int wa = ord.best_wh;
-    quantity = min(quantity, ins.wh[wa].disp[type.first]);
 
-    // augment drone_temps
+    // augment dr.temps
     dr.temps += 1+ceil(sqrt(dr.p.dist2(ins.wh[wa].p)));
     dr.temps += 1+ceil(sqrt(ins.wh[wa].p.dist2(ord.p)));
     dr.p = ord.p;
-    if(dr.temps > ins.T)
+    if(dr.temps > ins.T) {
       continue;
+    }
 
-    // optimiser l'ordre
-    int load = quantity;
+    // planifie l'ordre
+    int load = 0;
     vector<pair<int, int>> aprendre;
-    aprendre.push_back(make_pair(type.first, quantity));
     for(pair<int, int> item : ord.ord) {
+      assert(load <= ins.max_load);
       if(load == ins.max_load)
         break;
       int q = min(item.second, ins.wh[wa].disp[item.first]);
@@ -122,9 +101,12 @@ void glouton(instance &ins) {
       }
     }
 
+    if(aprendre.empty())
+      cerr << "! aprendre is empty" << endl;
+
     // pass order
     for(pair<int, int> item : aprendre) {
-    // decrease order / stock
+      // decrease order / stock
       ins.wh[wa].disp[item.first] -= item.second;
       ord.ord[item.first] -= item.second;
       if(!ord.ord[item.first])