]> ruin.nu Git - icfp05.git/commitdiff
fixed priority queue and started to implement the robber algorithm, it needs tuning...
authorMichael Andreen <harv@ruin.nu>
Sat, 25 Jun 2005 01:25:56 +0000 (01:25 +0000)
committerMichael Andreen <harv@ruin.nu>
Sat, 25 Jun 2005 01:25:56 +0000 (01:25 +0000)
bot/bot.cpp
robber/robber.cpp
robber/robber.pro

index 2829030b3dfe4b26cc2652ffb667759892d7131e..1c2b4dccf9adaf079abcdb2038aecf96ebd24020 100644 (file)
@@ -8,9 +8,51 @@
 using namespace std;
 using namespace __gnu_cxx;
 
+struct VectComp{
+       bool operator()(const vector<string>& v1, const vector<string>& v2){
+               return v1.size() > v2.size();
+       }
+};
 Bot::Bot(string name, string type){
        _name = name;
        _type = type;
+
+       /*
+       priority_queue<vector<string>, vector<vector<string> >, VectComp> pq;
+
+       vector<string> v;
+       v.push_back("hej");
+       pq.push(v);
+       v.push_back("hå");
+       pq.push(v);
+       v.push_back("blaha");
+       pq.push(v);
+       v.clear();
+       v.push_back("hej hå");
+       pq.push(v);
+       v.clear();
+       v.push_back("hejsan");
+       pq.push(v);
+       v.push_back("what?");
+       pq.push(v);
+       v.push_back("blaha");
+       pq.push(v);
+
+       cerr << "Size: " << pq.top().size() << " " << pq.top()[0] << endl;
+       pq.pop();
+       cerr << "Size: " << pq.top().size() << " " << pq.top()[0] << endl;
+       pq.pop();
+       cerr << "Size: " << pq.top().size() << " " << pq.top()[0] << endl;
+       pq.pop();
+       cerr << "Size: " << pq.top().size() << " " << pq.top()[0] << endl;
+       pq.pop();
+       cerr << "Size: " << pq.top().size() << " " << pq.top()[0] << endl;
+       pq.pop();
+       cerr << "Size: " << pq.top().size() << " " << pq.top()[0] << endl;
+       pq.pop();
+
+*/
+
 }
 
 void Bot::play(){
@@ -123,11 +165,11 @@ void Bot::updateWorld(){
        string input;
        getline(cin,input);
        _world = value<int>(input);
-       cerr << "World: " << _world << endl;
+       //cerr << "World: " << _world << endl;
 
        getline(cin,input);
        _robbed = value<int>(input);
-       cerr << "Robbed: " << _robbed << endl;
+       //cerr << "Robbed: " << _robbed << endl;
 
        getline(cin,input);
        while (true){
@@ -139,7 +181,7 @@ void Bot::updateWorld(){
                bank >> input;
                bank >> _banks[input];
        }
-       cerr << "Number of banks: " << _banks.size() << endl;
+       //cerr << "Number of banks: " << _banks.size() << endl;
        
        getline(cin,input);
        while (true){
@@ -160,12 +202,12 @@ void Bot::updateWorld(){
                istringstream player(input);
                player >> input;
                player >> input;
-               cerr << "Player: " << input << endl;
+               //cerr << "Player: " << input << endl;
                Player& pl = _players[input];
                player >> pl.location;
                player >> pl.type;
        }
-       cerr << "Number of players: " << _players.size() << endl;
+       //cerr << "Number of players: " << _players.size() << endl;
 }
 
 void Bot::move(std::string location){
@@ -189,7 +231,7 @@ std::string Bot::turn(){
 
 std::vector<std::string> Bot::shortestPath(std::string from, std::string to, std::string type){
        
-       priority_queue<vector<string>, vector<vector<string> >, greater<vector<string> > > pq;
+       priority_queue<vector<string>, vector<vector<string> >, VectComp > pq;
 
        vector<string> v;
        v.push_back(from);
@@ -198,25 +240,31 @@ std::vector<std::string> Bot::shortestPath(std::string from, std::string to, std
        hash_map<string,bool> settled;
 
        while(!pq.empty()){
-               const vector<string>& w = pq.top();
+               const vector<string> w = pq.top();
+               pq.pop();
+               //cerr << "Vector with size: " << w.size() << endl;
+               //cerr << "Looking at: " << w.back() << endl;
+               //copy(w.begin(), w.end(), ostream_iterator<string>(cerr, " : "));
                if (w.back() == to)
                        return w;
                settled[w.back()] = true;
                Intersection& inter = _intersections[w.back()];
                for (hash_map<string,StreetType>::const_iterator street = inter.connections.begin();
                                street != inter.connections.end(); ++street){
-                       if (settled.find(street->first) == settled.end())
+                       if (settled.find(street->first) != settled.end())
                                continue;
-                       bool car = type == "cop-car";
-                       if ( (car &&  (street->second == car || street->second == both)) ||
-                                       (!car && (street->second == foot || street->second == both))){
+                       //cerr << "Adding: " << street->first << endl;
+                       bool hascar = type == "cop-car";
+                       if ( (hascar &&  (street->second == car || street->second == both)) ||
+                                       (!hascar && (street->second == foot || street->second == both))){
+                               
                                v = w;
                                v.push_back(street->first);
                                pq.push(v);
                        }
+
                }
                
-               pq.pop();
        }
        
        return vector<string>();
index c888362d8768c231638aa52208b70b6bea697e22..efe40ded9ece7b52f43049d1529699e1c9c999d6 100644 (file)
@@ -1,24 +1,80 @@
 #include "robber.h"
 #include <iostream>
+#include <iterator>
+#include <cmath>
 
 using namespace std;
 using namespace __gnu_cxx;
 
 string Robber::turn(){
-
+       hash_map<string,double> streets;
        Intersection& inter = _intersections[_location];
-       
-       cerr << "Number of connecting streets: " << inter.connections.size() << endl;
-
        for (hash_map<string,StreetType>::const_iterator street = inter.connections.begin();
-               street != inter.connections.end(); ++street){
-               cerr << "Checking: " << street->first << endl;
-               if (street->second != car){
-                       cerr << "Moving to: " << street->first << endl;
-                       return street->first;
+               street != inter.connections.end(); ++street){
+               double goodness = 100;
+               int curx = _intersections[_location].x;
+               int cury = _intersections[_location].y;
+
+               int newx = _intersections[street->first].x;
+               int newy = _intersections[street->first].y;
+               for (hash_map<string, Player>::const_iterator player = _players.begin();
+                               player != _players.end(); ++player){
+                       if (player->first == _name)
+                               continue;
+
+                       int px = _intersections[player->second.location].x;
+                       int py = _intersections[player->second.location].y;
+
+                       double dist1 = sqrt(pow((curx-px),2.0)+pow((cury-py),2.0));
+                       double dist2 = sqrt(pow((newx-px),2.0)+pow((newy-py),2.0));
+                       cerr << "Original distance: " << dist1 << endl;
+                       cerr << "New distance: " << dist2 << endl;
+
+
+                       if (dist2 > dist1)
+                               goodness *= 4;
+                       else
+                               goodness /= 4;
+
+                       if (player->second.type == "cop-foot" && dist2 <= 3)
+                               goodness /= 10;
+                       else if (player->second.type == "cop-car" && dist2 <= 2)
+                               goodness /= 10;
+
+
+               }
+               streets[street->first] = goodness;
+       }
+
+       for(hash_map<string,int>::const_iterator bank = _banks.begin();
+                       bank != _banks.end(); ++bank){
+               //cerr << "Handling bank at: " << bank->first << endl;
+               if (bank->second > 0){
+                       vector<string> v = shortestPath(_location, bank->first,_type);
+                       if (v.size() < 2)
+                               continue;
+                       streets[v[1]] *= bank->second/v.size();
+                       
+               }
+       }
+       /*cerr << "Using route: ";
+       copy(v.begin(), v.end(), ostream_iterator<string>(cerr, " : "));
+       cerr << endl;
+       */
+
+       string destination;
+       double goodness = 0;
+       for (hash_map<string,double>::const_iterator dest = streets.begin();
+                       dest != streets.end(); ++dest){
+               cerr << "Goodness: " << dest->second << endl;
+               if (dest->second > goodness){
+               cerr << "New Goodness: " << dest->second << endl;
+                       goodness = dest->second;
+                       destination = dest->first;
                }
        }
-       return _location;
+               
+       return destination;
        
 }
 
index dd31e913b907b4584ca8ff27b34c606fea200116..027b399991f9c16e57f05fa94e97268512efb18a 100644 (file)
@@ -5,6 +5,7 @@
 TEMPLATE = app
 INCLUDEPATH += ../bot
 CONFIG -= qt
+unix:LIBS += -lm
 
 
 # Input