BWAPI
UAlbertaBot_src/Projects/UAlbertaBot/Source/base/StarcraftSearchData.h
Go to the documentation of this file.
00001 #pragma once
00002 
00003 #include "Common.h"
00004 #include "BuildOrderQueue.h"
00005 #include "WorkerManager.h"
00006 #include "starcraftsearch\ActionSet.hpp"
00007 #include "starcraftsearch\ProtossState.hpp"
00008 #include "starcraftsearch\TerranState.hpp"
00009 #include "starcraftsearch\DFBBStarcraftSearch.hpp"
00010 #include "starcraftsearch\StarcraftState.hpp"
00011 #include "starcraftsearch\StarcraftSearchGoal.hpp"
00012 #include "starcraftsearch\SmartStarcraftSearch.hpp"
00013 #include "starcraftsearch\StarcraftData.hpp"
00014 #include "starcraftsearch\SearchSaveState.hpp"
00015 
00016 template <class StarcraftStateType>
00017 class StarcraftSearchData
00018 {
00019         // parameters being used for this search
00020         SearchParameters<StarcraftStateType>                    previousParameters;
00021 
00022         // whether a search is currently in progress
00023         bool searchInProgress;
00024 
00025         // the search results from the previous search attempt
00026         // if this is solved, we can return the build order
00027         SearchResults previousSearchResults;
00028         SearchResults lastSolvedResults;
00029 
00030         // data for statistics
00031         int lastSearchFrame;
00032         int searchFrames;
00033         int totalNodes;
00034         double branch;
00035         double totalSearchTime;
00036 
00037         std::vector<std::pair<double,bool>>             searchHistory;
00038 
00039 public:
00040 
00041         StarcraftSearchData<StarcraftStateType>() :             searchInProgress(false), 
00042                                                                                                         lastSearchFrame(0), 
00043                                                                                                         searchFrames(0), 
00044                                                                                                         totalNodes(0),
00045                                                                                                         branch(0),
00046                                                                                                         totalSearchTime(0) {}
00047         ~StarcraftSearchData<StarcraftStateType>() {}
00048 
00049         void update(double timeLimit)
00050         {
00051                 // if a search is in progress 
00052                 if (searchInProgress)
00053                 {
00054                         //BWAPI::Broodwar->printf("Searching with %lf ms left", timeLimit);
00055                         lastSearchFrame = BWAPI::Broodwar->getFrameCount();
00056 
00057                         // set the time limit based on how much time we have this frame
00058                         previousParameters.searchTimeLimit = (int)(timeLimit > 3 ? timeLimit : 3);//(int)timeLimit;
00059 
00060                         // construct the new search object
00061                         DFBBStarcraftSearch<StarcraftStateType> SCSearch(previousParameters);
00062 
00063                         // set the results based on the
00064                         previousSearchResults = SCSearch.search();
00065 
00066                         // if there is a solution in these results, store it
00067                         if (previousSearchResults.solved)
00068                         {
00069                                 lastSolvedResults = previousSearchResults;
00070                         }
00071 
00072                         // if we didn't time out, then we're finished the search
00073                         if (!previousSearchResults.timedOut || searchFrames > 500)
00074                         {
00075                                 searchInProgress = false;
00076                         }
00077                         // if we did time out, set the save state
00078                         else
00079                         {
00080                                 previousParameters.useSaveState = true;
00081                                 previousParameters.saveState = previousSearchResults.saveState;
00082                         }
00083 
00084                         // keep statistics
00085                         lastSearchFrame = BWAPI::Broodwar->getFrameCount();
00086                         searchFrames++;
00087                         totalNodes += (int)previousSearchResults.nodesExpanded;
00088                         branch += previousSearchResults.avgBranch;
00089                         totalSearchTime += previousSearchResults.timeElapsed;
00090 
00091                         //BWAPI::Broodwar->printf("Search took %lf ms, and %s", previousSearchResults.timeElapsed, previousSearchResults.solved ? "solved" : "not solved");
00092                 }
00093 
00094                 drawSearchResults(10, 240);
00095         }
00096 
00097         void drawSearchResults(int x, int y)
00098         {
00099                 if (DRAW_UALBERTABOT_DEBUG)
00100                 {
00101                         BWAPI::Broodwar->drawBoxScreen(x-5, y-15, x+125, y+55, BWAPI::Colors::Black, true);
00102 
00103                         BWAPI::Broodwar->drawTextScreen(x, y-13,        "\x07Search Information");
00104                                 
00105                         BWAPI::Broodwar->drawTextScreen(x, y,           "\x04 Total Time");
00106                         BWAPI::Broodwar->drawTextScreen(x+75, y,        "%.3lf ms", totalSearchTime);
00107 
00108                         BWAPI::Broodwar->drawTextScreen(x, y+10,        "\x04 Nodes");
00109                         BWAPI::Broodwar->drawTextScreen(x+75, y+10,     "%d", totalNodes);
00110 
00111                         BWAPI::Broodwar->drawTextScreen(x, y+20,        "\x04 Branch");
00112                         BWAPI::Broodwar->drawTextScreen(x+75, y+20,     "%.3lf", searchFrames > 0 ? (branch/searchFrames) : 0);
00113                         
00114                         BWAPI::Broodwar->drawTextScreen(x, y+30,        "\x04 Frames");
00115                         BWAPI::Broodwar->drawTextScreen(x+75, y+30,     "%d", searchFrames);
00116 
00117                         BWAPI::Broodwar->drawTextScreen(x, y+40,        "\x04 End Frame");
00118                         BWAPI::Broodwar->drawTextScreen(x+75, y+40,     "%d", lastSearchFrame);
00119                 }
00120         }
00121 
00122         // get the build order which has been calculated
00123         // this build order is stored in previousSearchResults
00124         SearchResults getResults()
00125         {
00126                 // if the search is still ongoing, return blank
00127                 if (searchInProgress)
00128                 {
00129                         return SearchResults();
00130                 }
00131 
00132                 // when it's done return the last solution, which will be the best
00133                 return lastSolvedResults;
00134         }
00135 
00136         void startNewSearch(SearchParameters<StarcraftStateType> params, bool forceNewSearch = false)
00137         {
00138                 // if this is a new goal, start a new search
00139                 if (forceNewSearch || !(previousParameters.goal == params.goal))
00140                 {
00141                         //BWAPI::Broodwar->printf("New goal! Searching...");
00142 
00143                         searchFrames = 0;
00144                         totalSearchTime = 0;
00145                         totalNodes = 0;
00146                         branch = 0;
00147 
00148                         // set the class parameters
00149                         previousParameters = params;
00150 
00151                         // re-set the search results
00152                         lastSolvedResults = SearchResults();
00153                         previousSearchResults = SearchResults();
00154                         previousSearchResults.timedOut = true;
00155                         
00156                         // start search on next frame
00157                         searchInProgress = true;
00158 
00159                         searchHistory.clear();
00160                 }
00161                 // otherwise don't do anything
00162                 else
00163                 {
00164                         
00165                 }
00166         }
00167 };
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines