Browse Source

First greedy

Olivier Marty 8 years ago
parent
commit
ce2d231b2b
5 changed files with 181 additions and 4 deletions
  1. 4 0
      Olivier/.gitignore
  2. 22 4
      Olivier/Makefile
  3. 68 0
      Olivier/instance.cpp
  4. 42 0
      Olivier/instance.h
  5. 45 0
      Olivier/main.cpp

+ 4 - 0
Olivier/.gitignore

@@ -0,0 +1,4 @@
+bd.out
+moaw.out
+red.out
+source.zip

+ 22 - 4
Olivier/Makefile

@@ -2,15 +2,33 @@ GUROBI_DIR=/opt/gurobi650/linux64
 CPP=g++ -D_GLIBCXX_USE_CXX11_ABI=0 -m64 -g
 INC=-I$(GUROBI_DIR)/include
 LIBS=-L $(GUROBI_DIR)/lib -lgurobi_c++ -lgurobi65 -lpthread -lm
-SOURCES=gurobi.cpp
+SOURCES=instance.cpp main.cpp
+SOURCES_GUROBI=gurobi.cpp
+
+all: source.zip bd.out moaw.out red.out
+
+bd.out: main
+	./main < ../busy_day.in > bd.out
+
+moaw.out: main
+	./main < ../mother_of_all_warehouses.in > moaw.out
+
+red.out: main
+	./main < ../redundancy.in > red.out
+
+source.zip: $(SOURCES)
+	zip source.zip $(wildcard *.cpp)  $(wildcard *.h)
 
 main: $(SOURCES:.cpp=.o)
 	$(CPP) -o $@ $^ $(INC) $(LIBS)
 
+gurobi: $(SOURCES_GUROBI:.cpp=.o)
+	$(CPP) -o $@ $^ $(INC) $(LIBS)
+
 %.o: %.cpp
 	$(CPP) -o $@ -c $< $(INC)
 
-.PHONY: clean mrproper init
+.PHONY: all clean mrproper init
 
 init:
 	@echo '# please run the following command'
@@ -18,7 +36,7 @@ init:
 	@echo 'export LD_LIBRARY_PATH=$$LD_LIBRARY_PATH:$(GUROBI_DIR)/lib'
 
 clean:
-	rm -f $(SOURCES:.cpp=.o)
+	rm -f $(SOURCES:.cpp=.o) $(SOURCES_GUROBI:.cpp=.o)
 
 mrproper: clean
-	rm -f main
+	rm -f main gurobi *.out

+ 68 - 0
Olivier/instance.cpp

@@ -0,0 +1,68 @@
+#include "instance.h"
+
+using namespace std;
+
+pos::pos(int x, int y) : x(x), y(y) {
+}
+
+int pos::dist2(pos &b) {
+  return x*b.x + y*b.y;
+}
+
+warehouse::warehouse(pos p, vector<int> disp) : p(p), disp(disp) {
+}
+
+
+order::order(pos p, map<int, int> ord) : p(p), ord(ord) {
+}
+
+
+instance::instance() {
+  int tmp;
+  cin >> row >> col >> drones >> T >> max_load >> P;
+  for(int i = 0; i < P; i++) {
+    cin >> tmp;
+    weights.push_back(tmp);
+  }
+  cin >> W;
+  for(int i = 0; i < W; i++) {
+    int x, y;
+    vector<int> disp;
+    cin >> y >> x;
+    for(int j = 0; j < P; j++) {
+      cin >> tmp;
+      disp.push_back(tmp);
+    }
+    wh.push_back(warehouse(pos(x, y), disp));
+  }
+  cin >> C;
+  for(int i = 0; i < C; i++) {
+    int x, y, L;
+    map<int, int> ord;
+    cin >> y >> x >> L;
+    for(int j = 0; j < L; j++) {
+      cin >> tmp;
+      ord[tmp]++;
+    }
+    orders.push_back(order(pos(x, y), ord));
+  }
+}
+
+int instance::find(pos p, int id) { // cherche une warehouse proche de x/y
+  int best = -1;
+  int d = 1000000000.;
+  for(int i = 0; i < W; i++) {
+    if(wh[i].disp[id] > 0) {
+      int d2 = p.dist2(wh[i].p);
+      if(d2 < d) {
+        d = d2;
+        best = i;
+      }
+    }
+  }
+  if(best < 0) {
+    cerr << "error in find\n";
+    exit(1);
+  }
+  return best;
+}

+ 42 - 0
Olivier/instance.h

@@ -0,0 +1,42 @@
+#ifndef INSTANCE_H
+#define INSTANCE_H
+
+#include <bits/stdc++.h>
+
+
+class pos {
+  public:
+    pos(int x, int y);
+    int dist2(pos &b);
+    int x, y;
+};
+
+class warehouse {
+  public:
+    warehouse(pos p, std::vector<int> disp);
+    pos p;
+    std::vector<int> disp;
+};
+
+class order {
+  public:
+    order(pos p, std::map<int, int> ord);
+    pos p;
+    std::map<int, int> ord;
+};
+
+class instance {
+  public:
+    instance(); // read from stdin
+    int find(pos p, int id); // cherche une warehouse proche de x/y
+    int row, col, drones, max_load;
+    int T; // deadline
+    int P; // # products
+    int W; // # warehouses
+    int C; // # orders
+    std::vector<int> weights;
+    std::vector<warehouse> wh;
+    std::vector<order> orders;
+};
+
+#endif

+ 45 - 0
Olivier/main.cpp

@@ -0,0 +1,45 @@
+#include "instance.h"
+
+using namespace std;
+
+int next(instance &ins) {
+  int best;
+  for(int i = 0; i < ins.orders.size(); i++) {
+    if(!ins.orders[i].ord.empty()) {
+      best = i;
+      break;
+    }
+  }
+  return best;
+}
+
+void glouton(instance &ins) {
+  int tour = 1000;
+  cout << 2*tour << endl;
+  for(int i = 0; i < tour; i++) {
+    int next_i = next(ins);
+    int drone = rand()%ins.drones;
+    order &ord = ins.orders[next_i];
+    assert(!ord.ord.empty());
+    pair<const int, int> &type = *ord.ord.begin();
+    int wa = ins.find(pos(0, 0), type.first);
+    int quantity = min(type.second, ins.max_load/ins.weights[type.first]);
+    quantity = min(quantity, ins.wh[wa].disp[type.first]);
+    ins.wh[wa].disp[type.first] -= quantity;
+    // decrease order / stock
+    type.second -= quantity;
+    if(!type.second)
+      ord.ord.erase(type.first);
+
+    // pass order
+    cout << drone << " L " << wa << " " << type.first << " " << quantity << endl;
+    cout << drone << " D " << next_i << " " << type.first << " " << quantity << endl;
+  }
+}
+
+int main() {
+  srand(time(NULL));
+  instance ins;
+  glouton(ins);
+  return 0;
+}