Advertisement
Guest User

Untitled

a guest
May 8th, 2016
259
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 16.17 KB | None | 0 0
  1. void doPrecalc() {
  2.   int nSame;
  3.   int nDiversity;
  4.   int nDiversityWLR;
  5.   int nNone;
  6.   int nSkull;
  7.   int nBorder;
  8.   int score;
  9.   for(Tile topTile: all_Tiles) {
  10.     for(Tile leftTile: all_Tiles) {
  11.       for(Tile rightTile: all_Tiles) {
  12.         for(Tile bottomTile: all_Tiles) {
  13.           for(Tile centerTile: all_Tiles) {
  14.             nSame = 0;
  15.             if(topTile == centerTile) {
  16.               ++nSame;
  17.             }
  18.             if(leftTile == centerTile) {
  19.               ++nSame;
  20.             }
  21.             if(rightTile == centerTile) {
  22.               ++nSame;
  23.             }
  24.             if(bottomTile == centerTile) {
  25.               ++nSame;
  26.             }
  27.             nDiversity = 0;
  28.             if(topTile == leftTile && isColorTile(topTile)) {
  29.               ++nDiversity;
  30.             }
  31.             if(topTile == rightTile && isColorTile(topTile)) {
  32.               ++nDiversity;
  33.             }
  34.             if(topTile == bottomTile && isColorTile(topTile)) {
  35.               ++nDiversity;
  36.             }
  37.             if(leftTile == rightTile && isColorTile(leftTile)) {
  38.               ++nDiversity;
  39.             }
  40.             if(leftTile == bottomTile && isColorTile(leftTile)) {
  41.               ++nDiversity;
  42.             }
  43.             if(rightTile == bottomTile && isColorTile(rightTile)) {
  44.               ++nDiversity;
  45.             }
  46.             nDiversityWLR = 0;
  47.             if(topTile == leftTile && isColorTile(topTile)) {
  48.               ++nDiversityWLR;
  49.             }
  50.             if(topTile == rightTile && isColorTile(topTile)) {
  51.               ++nDiversityWLR;
  52.             }
  53.             if(topTile == bottomTile && isColorTile(topTile)) {
  54.               ++nDiversityWLR;
  55.             }
  56.             if(leftTile == bottomTile && isColorTile(leftTile)) {
  57.               ++nDiversityWLR;
  58.             }
  59.             if(rightTile == bottomTile && isColorTile(rightTile)) {
  60.               ++nDiversityWLR;
  61.             }
  62.             nNone = 0;
  63.             if(topTile == Tile::None) {
  64.               ++nNone;
  65.             }
  66.             if(leftTile == Tile::None) {
  67.               ++nNone;
  68.             }
  69.             if(rightTile == Tile::None) {
  70.               ++nNone;
  71.             }
  72.             if(bottomTile == Tile::None) {
  73.               ++nNone;
  74.             }
  75.             nSkull = 0;
  76.             if(topTile == Tile::Skull) {
  77.               ++nSkull;
  78.             }
  79.             if(leftTile == Tile::Skull) {
  80.               ++nSkull;
  81.             }
  82.             if(rightTile == Tile::Skull) {
  83.               ++nSkull;
  84.             }
  85.             if(bottomTile == Tile::Skull) {
  86.               ++nSkull;
  87.             }
  88.             nBorder = 0;
  89.             if(topTile == Tile::Border) {
  90.               ++nBorder;
  91.             }
  92.             if(leftTile == Tile::Border) {
  93.               ++nBorder;
  94.             }
  95.             if(rightTile == Tile::Border) {
  96.               ++nBorder;
  97.             }
  98.             if(bottomTile == Tile::Border) {
  99.               ++nBorder;
  100.             }
  101.  
  102.  
  103.             if(centerTile == Tile::Skull) {
  104.               score = -1;
  105.               score -= (nSkull >= 3) ? 4*(nSkull-2) : 0;
  106.             } else if (isColorTile(centerTile)) {
  107.               score = -5;
  108.               if(nSame ==0 ) {
  109.                 score -= 8*(nNone == 0);
  110.                 score += nNone;
  111.               }
  112.               if(nSame == 1) {
  113.                 //Une paire
  114.                 score += 10;
  115.                 score += 3*(nNone>0);
  116.                 score += 0*nDiversity;
  117.                 score += nDiversityWLR;
  118.               } else if(nSame == 2) {
  119.                 //Un triple
  120.                 score += 30 + 3*(nNone>0);
  121.                 if(!(leftTile == centerTile && leftTile == rightTile) && !(topTile == centerTile && bottomTile == topTile)) {
  122.                   score += 5;
  123.                   score += 2*nDiversityWLR;
  124.                 }
  125.                 score += nDiversityWLR;
  126.               }
  127.               score += 8*(nNone>0);
  128.               score += 12*((isColorTile(leftTile)&& rightTile == Tile::None) || (isColorTile(rightTile)&& leftTile == Tile::None));
  129.               score += 4*nNone;
  130.               score -= 6*nBorder;
  131.               score += 1*nDiversity;
  132.             } else if (centerTile == Tile::None) {
  133.               score = 8*nDiversity;
  134.             } else {
  135.               score = 0;
  136.             }
  137.             _precalculatedScore[static_cast<int>(centerTile)][static_cast<int>(topTile)][static_cast<int>(leftTile)][static_cast<int>(rightTile)][static_cast<int>(bottomTile)] = score;
  138.           }
  139.         }
  140.       }
  141.     }
  142.   }
  143. }
  144.  
  145.  
  146. //////////////////////////////////////////////////////////////////////////////
  147.  
  148.  
  149. void evaluateGenome(Genome & genome, FitnessType &fitness, bool verbose, int* fillScenario, int scenarioNumber) {
  150.     myNodeEval.overTop = false;
  151.     board->loadBoard();
  152.     fitness.gameLost = false;
  153.     fitness.turnGameLost = 0;
  154.     fitness.droppedManySkulls = false;
  155.     fitness.turnDroppedManySkulls = 0;
  156.     fitness.discountedScore = 0;
  157.     fitness.boardArrangementQuality = 0;
  158.     for(int turn = 0; turn < genome.genomeSize; ++turn) {
  159.  
  160.       myNodeEval.rotation = (genome.genes[turn]) & 3;
  161.       myNodeEval.centerX = (genome.genes[turn]) >> 2;
  162.       myNodeEval.turn = turn;
  163.  
  164.       board->doNode(myNodeEval);
  165.  
  166.       if(scenarios[scenarioNumber][turn] > 0) {
  167.         board->dropSkulls(myNodeEval.pId, scenarios[scenarioNumber][turn]);
  168.       }
  169.       if(fillScenario != nullptr) {
  170.         fillScenario[turn] = myNodeEval.nSkullsDropped;
  171.       }
  172.  
  173.       if(myNodeEval.overTop) {
  174.         fitness.gameLost = true;
  175.         fitness.turnGameLost = turn;
  176.         break;
  177.       } else {
  178.         if((turn < 3 || turn == 17) && ratioScoreBoardArrangement != 0) {
  179.           board->fullEvaluateBoard(myNodeEval);
  180.           fitness.boardArrangementQuality += myNodeEval.positionalEstimation*discountFactorBQ[turn];
  181.         }
  182.         fitness.discountedScore += (myNodeEval.scoreThisTurn)*discountFactor[turn];
  183.         if(4*myNodeEval.nSkullsDropped > 4*SKULLS_DROPPED_THRESHOLD+turn-1 && !fitness.droppedManySkulls) {
  184.           fitness.droppedManySkulls = true;
  185.           //fitness.turnDroppedManySkulls = turn;
  186.           //fitness.turnDroppedManySkulls = 23*turn+2*turn*turn-20*std::min(12, myNodeEval.nSkullsDropped)+3*turn*std::min(12, myNodeEval.nSkullsDropped);
  187.           fitness.turnDroppedManySkulls = 4*turn - std::min(12, myNodeEval.nSkullsDropped);
  188.         }
  189.       }
  190.       if(verbose) {
  191.         std::cerr << myNodeEval.centerX << " " <<myNodeEval.rotation << "     " << board->nextTiles[2*turn] << " " << board->nextTiles[2*turn+1]<< "     "<<myNodeEval.scoreThisTurn <<std::endl;
  192.         board->dump(std::cerr);
  193.       }
  194.     }
  195.     fitness.compoundedValue = fitness.discountedScore + ratioScoreBoardArrangement*(fitness.boardArrangementQuality);
  196.   }
  197.  
  198.  
  199.  
  200. /////////////////////////////////////////////////////////////////////////
  201.  
  202. template <Behavior behavior> class TetrisFitness {
  203. public:
  204.   bool gameLost;
  205.   int turnGameLost;
  206.   int discountedScore;
  207.   int boardArrangementQuality;
  208.   bool droppedManySkulls;
  209.   int turnDroppedManySkulls=0;
  210.   int compoundedValue;
  211.  
  212.   bool operator < (const TetrisFitness &other) const {
  213.     if(behavior == Behavior::A) {
  214. //      LOGD(discountedScore);
  215.       //      Maximize the score
  216.       LEX_GT(gameLost);
  217.       if(gameLost) {
  218.         LEX_LT(turnGameLost);                 //If you lose, lose as late as possible
  219.       }
  220.  
  221.       LEX_LT(droppedManySkulls);
  222.       if(droppedManySkulls) {
  223.         LEX_GT(turnDroppedManySkulls);        //If you drop many skulls, do so ASAP
  224.       }
  225.       LEX_LT(compoundedValue);
  226.       LEX_LT(discountedScore);                //Maximize the score
  227.       LEX_LT(boardArrangementQuality);        //Maximize the arrangement quality
  228.       return false;
  229.     } else if (behavior == Behavior::B) {
  230.       LOGD("Erreur");
  231.       LEX_GT(gameLost);
  232.       if(gameLost) {
  233.         LEX_LT(turnGameLost);                 //If you lose, lose as late as possible
  234.       }
  235.  
  236.       LEX_LT(droppedManySkulls);
  237.       if(droppedManySkulls) {
  238.         LEX_GT(turnDroppedManySkulls);        //If you drop many skulls, do so ASAP
  239.       }
  240.  
  241.       LEX_LT(discountedScore/1000+1000*boardArrangementQuality);                //Maximize the score
  242.       LEX_LT(boardArrangementQuality);        //Maximize the arrangement quality
  243.       return false;
  244.  
  245.     }
  246.     else {
  247.       LOGERR("Tetris fitness compare error");
  248.     }
  249.   }
  250. }
  251.  
  252.  
  253. ////////////////////////////////////////////////////////////
  254.  
  255.  
  256. void roundOutput(std::ostream& oStream) {
  257.  
  258.     std::string message = " M ";
  259.     resetTimer();
  260.  
  261.     int timeUsed = 0;
  262.  
  263.     int popSize = 109;
  264.     int genomeSize = 8;
  265.     double discount = 0.80;
  266.     int pId = 0;
  267.     const Behavior behavior = Behavior::A;
  268.     int BQImportance = 5+25.0*std::max(0.0,(40.0-board.nColor[0]-board.nSkull[0])/40.0);
  269.     SKULLS_DROPPED_THRESHOLD = std::min(SKTH_ME_START,1+(6*12-board.nColor[0]-board.nSkull[0])/6) ;
  270.  
  271.  
  272.     int oppPopSize = 71;
  273.     int oppGenomeSize = 6;
  274.     double oppDiscount = 0.85;
  275.     int oppPId = 1;
  276.     const Behavior oppBehavior = Behavior::A;
  277.     int oppBQImportance = 0;
  278.  
  279.  
  280.     if(environmentTurnNumber == 0) {
  281.       saveGenome.init(genomeSize);
  282.       saveOppGenome.init(oppGenomeSize);
  283.     }
  284.  
  285.  
  286.     //Initialize the genetic algorithm for me
  287.     STCEvaluator<TetrisFitness<behavior>> evaluator(&board, popSize, genomeSize, discount, pId, BQImportance);
  288.     GeneticAlgorithm <TetrisFitness<behavior>, STCEvaluator<TetrisFitness<behavior>>, GeneticEvolverBasic>  tetrisGeneticAlgorithm(evaluator, popSize, genomeSize, 2, std::cout, false, true);  //bool verbose, bool timeBased
  289.  
  290.     //Restore the best variation found last turn
  291.     if(environmentTurnNumber > 0) {
  292.       tetrisGeneticAlgorithm.population[0].genomes[1].init(genomeSize);
  293.       tetrisGeneticAlgorithm.population[0].genomes[2].init(genomeSize);
  294.       for(int i = 0; i < genomeSize-1; ++i) {
  295.         tetrisGeneticAlgorithm.population[0].genomes[1].genes[i] = saveGenome.genes[i+1];
  296.         tetrisGeneticAlgorithm.population[0].genomes[2].genes[i] = saveGenome.genes[i+1];
  297.       }
  298.     }
  299.  
  300.     if(environmentTurnNumber > 5 && board.nColor[0] > 7 && board.nColor[1] > 9) {
  301.  
  302.  
  303.       //Initialize the opponent search
  304.       STCEvaluator<TetrisFitness<oppBehavior>> opponentEvaluator(&board, oppPopSize, oppGenomeSize, oppDiscount, oppPId, oppBQImportance);
  305.       GeneticAlgorithm <TetrisFitness<oppBehavior>, STCEvaluator<TetrisFitness<oppBehavior>>, GeneticEvolverBasic>  opponentGA(opponentEvaluator, oppPopSize, oppGenomeSize, 2, std::cout, false, true);  //bool verbose, bool timeBased
  306.  
  307.  
  308.  
  309.       //Restore the best variation found last turn
  310.       if(environmentTurnNumber > 0) {
  311.         opponentGA.population[0].genomes[1].init(oppGenomeSize);
  312.         opponentGA.population[0].genomes[2].init(oppGenomeSize);
  313.         for(int i = 0; i < oppGenomeSize-1; ++i) {
  314.           opponentGA.population[0].genomes[1].genes[i] = saveOppGenome.genes[i+1];
  315.           opponentGA.population[0].genomes[2].genes[i] = saveOppGenome.genes[i+1];
  316.         }
  317.       }
  318.  
  319.  
  320.  
  321.       //Find the best variation for me
  322.       tetrisGeneticAlgorithm.setControls(MY_FIRST_TIME_LIMIT, 2*TIME_LIMIT);
  323.       tetrisGeneticAlgorithm.doOptimize();
  324.       timeUsed += MY_FIRST_TIME_LIMIT;
  325.  
  326.       Genome myBestGenome = tetrisGeneticAlgorithm.getBestGenome();
  327.       TetrisFitness<behavior> fitnessThingMe;
  328.       tetrisGeneticAlgorithm.entityEvaluator.evaluateGenome(myBestGenome, fitnessThingMe, false, opponentGA.entityEvaluator.scenarios[0], 0);
  329.       LOGA("My variation  : ");
  330.       fitnessThingMe.toOutput(std::cerr);
  331.       std::cerr << "                    ";
  332.       for(int i = 0; i < MAX_HORIZON; ++i) {
  333.         std::cerr<< opponentGA.entityEvaluator.scenarios[0][i]<<" ";
  334.       }
  335.       std::cerr<<std::endl;
  336.  
  337.       SKULLS_DROPPED_THRESHOLD = SKTH_OPP;
  338.  
  339.  
  340.       //Find the best variation
  341.       opponentGA.setControls(OPPGA_TIME_LIMIT, 2*OPPGA_TIME_LIMIT);
  342.       timeUsed += OPPGA_TIME_LIMIT;
  343.       opponentGA.doOptimize();
  344.       //Save the best variation
  345.       Genome bestGenome = opponentGA.getBestGenome();
  346.       saveOppGenome = bestGenome;
  347.       TetrisFitness<oppBehavior> fitnessThing;
  348.       opponentGA.entityEvaluator.evaluateGenome(bestGenome, fitnessThing, false, tetrisGeneticAlgorithm.entityEvaluator.scenarios[0], 0);
  349.       LOGA("His variation : ");
  350.       fitnessThing.toOutput(std::cerr);
  351.       std::cerr << "                    ";
  352.       for(int i = 0; i < MAX_HORIZON; ++i) {
  353.         std::cerr<< tetrisGeneticAlgorithm.entityEvaluator.scenarios[0][i]<<" ";
  354.       }
  355.       std::cerr<<std::endl;
  356.  
  357.  
  358.  
  359.  
  360.       //Restore the best variation found last turn
  361.       tetrisGeneticAlgorithm.popSwitch = 0;
  362.       if(environmentTurnNumber > 0) {
  363.         tetrisGeneticAlgorithm.population[0].genomes[1].init(genomeSize);
  364.         tetrisGeneticAlgorithm.population[0].genomes[2].init(genomeSize);
  365.         tetrisGeneticAlgorithm.population[0].genomes[3].init(genomeSize);
  366.         tetrisGeneticAlgorithm.population[0].genomes[4].init(genomeSize);
  367.         for(int i = 0; i < genomeSize-1; ++i) {
  368.           tetrisGeneticAlgorithm.population[0].genomes[1].genes[i] = saveGenome.genes[i+1];
  369.           tetrisGeneticAlgorithm.population[0].genomes[2].genes[i] = saveGenome.genes[i+1];
  370.         }
  371.         for(int i = 0; i < genomeSize; ++i) {
  372.           tetrisGeneticAlgorithm.population[0].genomes[3].genes[i] = myBestGenome.genes[i];
  373.           tetrisGeneticAlgorithm.population[0].genomes[4].genes[i] = myBestGenome.genes[i];
  374.         }
  375.       }
  376.       for(int i = 5; i < popSize / 2 ; ++i) {
  377.         tetrisGeneticAlgorithm.population[0].genomes[i].init(genomeSize);
  378.       }
  379.  
  380.  
  381.       int mySum = 0;
  382.       int hisSum = 0;
  383.       for(int i = 0; i < genomeSize; ++i) {
  384.         mySum += opponentGA.entityEvaluator.scenarios[0][i];
  385.         hisSum = tetrisGeneticAlgorithm.entityEvaluator.scenarios[0][i];
  386.         if(hisSum > SEUIL_WIN && mySum < SEUIL_WIN - 1 && i > 0) {
  387.           //He will drop a lot of stuff on me and I won't.
  388.           //I need to drop something ASAP.
  389.           tetrisGeneticAlgorithm.entityEvaluator.scenarios[0][i-1] = hisSum;
  390.  
  391.           std::cerr << "His new variation : ";
  392.           for(int i = 0; i < MAX_HORIZON; ++i) {
  393.             std::cerr<< tetrisGeneticAlgorithm.entityEvaluator.scenarios[0][i]<<" ";
  394.  
  395.           }
  396.           std::cerr<<std::endl;
  397.  
  398.           message += "DEFENSE";
  399.           break;
  400.         }
  401.       }
  402.     }
  403.  
  404.     SKULLS_DROPPED_THRESHOLD = SKTH_ME_END;
  405.  
  406.  
  407.  
  408.     tetrisGeneticAlgorithm.setControls(TIME_LIMIT - getTimerDuration() + ((environmentTurnNumber == 0) ? 50000 : 0), 10*TIME_LIMIT);
  409.     //Find the best variation
  410.     tetrisGeneticAlgorithm.doOptimize();
  411.  
  412.     //Save the best variation
  413.     Genome bestGenome2 = tetrisGeneticAlgorithm.getBestGenome();
  414.     saveGenome = bestGenome2;
  415.  
  416.     TetrisFitness<behavior> bestFitness = tetrisGeneticAlgorithm.getBestFitness();
  417.  
  418.  
  419.     //Print the principal variation
  420.     LOGA("My Variation  : ");
  421.     bestFitness.toOutput(std::cerr);
  422.     std::cerr << "                    ";
  423.     TetrisFitness<behavior> fitnessThing2;
  424.     tetrisGeneticAlgorithm.entityEvaluator.evaluateGenome(bestGenome2, fitnessThing2, false, tetrisGeneticAlgorithm.entityEvaluator.scenarios[1], 0);
  425.     for(int i = 0; i < MAX_HORIZON; ++i) {
  426.       std::cerr<< tetrisGeneticAlgorithm.entityEvaluator.scenarios[1][i]<<" ";
  427.     }
  428.     std::cerr<<std::endl;
  429.  
  430.     //Log the statistics
  431.     LOGA("Round duration  : "<< getTimerDuration());
  432.     LOGA("Node count      : "<< board.nodeCounter);
  433.     LOGA("Generation count: " << tetrisGeneticAlgorithm.getGeneration());
  434.  
  435.     tetrisGeneticAlgorithm.entityEvaluator.evaluateGenome(bestGenome2, fitnessThing2, true, nullptr, 0);
  436.  
  437.     board.dump(std::cerr);
  438.     board.loadBoard();
  439.     board.dump(std::cerr);
  440.  
  441.     //Predict my future board state.
  442.     Node myNode;
  443.     myNode.pId = 0;
  444.     myNode.nSkullsDropped = 0;
  445.     myNode.rotation = (bestGenome2.genes[0]) & 3;
  446.     myNode.centerX = (bestGenome2.genes[0]) >> 2;
  447.     myNode.turn = 0;
  448.     board.loadBoard();
  449.     board.doNode(myNode);
  450.     saveSkullsDropped = myNode.nSkullsDropped;
  451.  
  452.     //Output the move that should be played
  453.     oStream << myNode.centerX << " " << myNode.rotation <<message <<std::endl;
  454.  
  455.     environmentTurnNumber++;
  456.   }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement