<commit_before>/**
* @file StrategyTools.cpp
*
* @author <NAME></a>
* 
* Implementation of the class StrategyTools
*/

#include "StrategyTools.h"
#include <Tools/Debug/NaoTHAssert.h>
#include <Tools/Math/Common.h>

void StrategyTools::arrangeRobots(
  const std::vector<Vector2d>& robots, 
  const std::vector<Vector2d>& places, 
  std::vector<int>& placesToRobots,
  int numberOfIterations)
{
  size_t numberOfRobots = robots.size();
  size_t numberOfPlaces = places.size();

  double fakeDistance = 0;

  // inverse assignement
  std::vector<int> robotsToPlaces(numberOfRobots,-1); // init with invalid places -1

  ASSERT(numberOfPlaces == placesToRobots.size());
  ASSERT(numberOfPlaces <= numberOfRobots);
  
  // TODO: treat the case numberOfPlaces > numberOfRobots
  //initial assignment
  for(size_t i = 0; i < numberOfPlaces; i++)
  {
    //int robotIndex = (int)(Math::random()*(numberOfRobots-1)+0.5);
    robotsToPlaces[placesToRobots[i]] = static_cast<int> (i);

    // place i <= robotIndex
    //placesToRobots[i] = i;
  }//end for


  for(int i = 0; i < numberOfIterations; i++)
  {
    // select place
    int placeIndexOne = (int)(Math::random()*(static_cast<double> (numberOfPlaces)-1.0)+0.5);
    // take the robot assighned to this place
    int robotIndexOne = placesToRobots[placeIndexOne];

    // select another robot
    int robotIndexTwo = (int)(Math::random()*(static_cast<double> (numberOfRobots)-1.0)+0.5);
    // take the place of this robot (!!!could be -1)
    int placeIndexTwo = robotsToPlaces[robotIndexTwo];

    if(robotIndexOne == robotIndexTwo) {
      continue;
    }

    // current distance
    double distOne = (robots[robotIndexOne] - places[placeIndexOne]).abs();
    double distTwo = fakeDistance;
    if(placeIndexTwo != -1) {
      distTwo = (robots[robotIndexTwo] - places[placeIndexTwo]).abs();
    }
    double currentDistance = distOne + distTwo;
  
    // alternative distance
    double alternativeDistOne = fakeDistance;
    if(placeIndexTwo != -1) {
      alternativeDistOne = (robots[robotIndexOne] - places[placeIndexTwo]).abs();
    }

    double alternativeDistTwo = (robots[robotIndexTwo] - places[placeIndexOne]).abs();
    double alternativeDistance = alternativeDistOne + alternativeDistTwo;

    // switch
    if(alternativeDistance < currentDistance)
    {
      placesToRobots[placeIndexOne] = robotIndexTwo;
      if(placeIndexTwo != -1) {
        placesToRobots[placeIndexTwo] = robotIndexOne;
      }

      robotsToPlaces[robotIndexOne] = placeIndexTwo;
      robotsToPlaces[robotIndexTwo] = placeIndexOne;
    }//end if
  }//end for

}//end arrangeRobots
<commit_msg>Removed fakeDistance to minmize confusion<commit_after>/**
* @file StrategyTools.cpp
*
* @author <NAME></a>
* 
* Implementation of the class StrategyTools
*/

#include "StrategyTools.h"
#include <Tools/Debug/NaoTHAssert.h>
#include <Tools/Math/Common.h>

void StrategyTools::arrangeRobots(
  const std::vector<Vector2d>& robots, 
  const std::vector<Vector2d>& places, 
  std::vector<int>& placesToRobots,
  int numberOfIterations)
{
  size_t numberOfRobots = robots.size();
  size_t numberOfPlaces = places.size();


  // inverse assignement
  std::vector<int> robotsToPlaces(numberOfRobots,-1); // init with invalid places -1

  ASSERT(numberOfPlaces == placesToRobots.size());
  ASSERT(numberOfPlaces <= numberOfRobots);
  
  // TODO: treat the case numberOfPlaces > numberOfRobots
  //initial assignment
  for(size_t i = 0; i < numberOfPlaces; i++)
  {
    //int robotIndex = (int)(Math::random()*(numberOfRobots-1)+0.5);
    robotsToPlaces[placesToRobots[i]] = static_cast<int> (i);

    // place i <= robotIndex
    //placesToRobots[i] = i;
  }//end for


  for(int i = 0; i < numberOfIterations; i++)
  {
    // select place
    int placeIndexOne = (int)(Math::random()*(static_cast<double> (numberOfPlaces)-1.0)+0.5);
    // take the robot assighned to this place
    int robotIndexOne = placesToRobots[placeIndexOne];

    // select another robot
    int robotIndexTwo = (int)(Math::random()*(static_cast<double> (numberOfRobots)-1.0)+0.5);
    // take the place of this robot (!!!could be -1)
    int placeIndexTwo = robotsToPlaces[robotIndexTwo];

    if(robotIndexOne == robotIndexTwo) {
      continue;
    }

    // current distance
    double distOne = (robots[robotIndexOne] - places[placeIndexOne]).abs();
    double distTwo = 0;
    if(placeIndexTwo != -1) {
      distTwo = (robots[robotIndexTwo] - places[placeIndexTwo]).abs();
    }
    double currentDistance = distOne + distTwo;
  
    // alternative distance
    double alternativeDistOne = 0;
    if(placeIndexTwo != -1) {
      alternativeDistOne = (robots[robotIndexOne] - places[placeIndexTwo]).abs();
    }

    double alternativeDistTwo = (robots[robotIndexTwo] - places[placeIndexOne]).abs();
    double alternativeDistance = alternativeDistOne + alternativeDistTwo;

	//current use the minimum sum, maybe better to use the minimum distance of longest way

    // switch
    if(alternativeDistance < currentDistance)
    {
      placesToRobots[placeIndexOne] = robotIndexTwo;
      if(placeIndexTwo != -1) {
        placesToRobots[placeIndexTwo] = robotIndexOne;
      }

      robotsToPlaces[robotIndexOne] = placeIndexTwo;
      robotsToPlaces[robotIndexTwo] = placeIndexOne;
    }//end if
  }//end for

}//end arrangeRobots

{'fraction_non_alphanumeric': 0.13070306656694092, 'fraction_numerical': 0.008414360508601346, 'mean_word_length': 3.7336283185840706, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5317959', 'n_tokens_mistral': 1776, 'n_tokens_neox': 1721, 'n_words': 368}