<commit_before>/*
 * Colony.cpp
 *
 *  Created on: Nov 17, 2014
 *      Author: sina
 */

#include "Colony.h"
#include <iostream>
#include <algorithm>
using namespace std;

Colony::Colony(int id){
	this->id = id;

	char buffer[100];
	sprintf(buffer, "Colony_%d.log", id);
	logger = new Logger(buffer);

	for (int i = 0; i < COLONY_MAX_SIZE; i++){
		planets[i] = -1;
	}
	size = 0;

	attackThreshold = 30;
}

Colony::~Colony() {

}

// The DoTurn function is where your code goes. The PlanetWars object contains
// the state of the game, including information about all planets and fleets
// that currently exist. Inside this function, you issue orders using the
// pw.IssueOrder() function. For example, to send 10 ships from planet 3 to
// planet 8, you would say pw.IssueOrder(3, 8, 10).
void Colony::DoTurn(const PlanetWars &pw, Colony *destination) {
	sprintf(logger->buffer, "TURN START: %d ", ID());
	logger->log();

	int num_my_fleets = pw.MyFleets().size();

	// (1) If we currently have a fleet in flight, just do nothing.
	if (num_my_fleets >= MAX_NUM_MY_FLEETS) {
		return;
	}

	// (2) Find my eligible planets to attack.
	vector<int> sources;
	for (size_t i = 0; i < size; i++){
		const Planet* planet = pw.GetPlanet(planets[i]);
		if (planet->Owner() == ME){
			if (planet->NumShips() > ELIGIBILITY_THRESHOLD_PERCENT)
				sources.push_back(planets[i]);
		}
	}

	// (3) Find the weakest enemy or neutral planet.
	int dest = -1;
	for (size_t i = 0; i < destination->Size(); i++){
		if (pw.GetPlanet(destination->Planets()[i])->Owner() == ENEMY ||
				pw.GetPlanet(destination->Planets()[i])->Owner() == NEUTRAL){
			dest = destination->Planets()[i];
			break;
		}
	}

	if (sources.size() > 0 && dest != -1){
		// (4) Send half the ships from my strongest planet to the weakest
		// planet that I do not own.
		for (size_t i = 0; i < sources.size(); i++){
			int num_ships = pw.GetPlanet(sources[i])->NumShips() / 2;
			pw.IssueOrder(sources[i], dest, num_ships);
			sprintf(logger->buffer, "Attack %d: Source: %d, Dest.: %d Ships: %d", (i + 1), sources[i], dest, num_ships);
			logger->log();
		}
	}

	//**************************************************//

	/*// (2) Find my eligible planets to attack.
	vector<int> sources;
	for (size_t i = 0; i < size; i++){
		const Planet* planet = pw.GetPlanet(planets[i]);
		if (planet->Owner() == ME){
			if (planet->NumShips() > ELIGIBILITY_THRESHOLD_PERCENT)
				sources.push_back(planets[i]);
		}
	}

	// (3) Discretize strongness of each planet in the colony
	vector<int> planet_strongness;

	for (size_t i = 0; i < COLONY_MAX_SIZE; i++){
		if (i > destination->Size()){
			planet_strongness.push_back(0);
		}else{
			int ships_num = pw.GetPlanet(destination->Planets()[i])->NumShips();
			if (ships_num < PLANET_STRONGNESS_MIN){
				ships_num = PLANET_STRONGNESS_MIN;
			}else if (ships_num >= PLANET_STRONGNESS_MAX){
				ships_num = PLANET_STRONGNESS_MAX;
			}

			ships_num = (ships_num - PLANET_STRONGNESS_MIN) / PLANET_STRONGNESS_STEP;
			planet_strongness.push_back(ships_num);
		}
	}

	bool random_action = (((rand() % 1000) / 1000.0) > EXPLOITATION_PLANET) ? true : false;
	double max_q = -99999999;
	Action* max_action = 0;
	int action_t = -1;

	if (random_action){
		while (true){
			action_t = rand() % q_value_obj->actions.size();
			max_action = q_value_obj->actions[action_t];

			if (max_action->destination == -1){
				return;
			}
		}
	}else{
		for (size_t i = 0; i < q_value_obj->actions.size(); i++){
			vector<int> indexes;
			for (size_t j = 0; j < planet_strongness.size(); j++){
				indexes.push_back(planet_strongness[j]);
			}
			if (num_my_fleets > MAX_NUM_MY_FLEETS){
				indexes.push_back(MAX_NUM_MY_FLEETS);
			}else{
				indexes.push_back(num_my_fleets);
			}

			if (planets[q_value_obj->actions[i]->destination] != -1 && q_value_obj->q_values[get_index_for(indexes)] > max_q){
				max_q = q_value_obj->q_values[get_index_for(indexes)];
				max_action = q_value_obj->actions[i];
				action_t = i;
			}
		}

		if (max_q == -99999999){
			return;
		}
	}

	if (sources.size() > 0){
		// (4) Send half the ships from my strongest planet to the weakest
		// planet that I do not own.
		for (size_t i = 0; i < sources.size(); i++){
			int num_ships = pw.GetPlanet(sources[i])->NumShips() / 2;
			pw.IssueOrder(sources[i], max_action->destination, num_ships);
			sprintf(logger->buffer, "Attack %d: Source: %d, Dest.: %d Ships: %d", (i + 1), sources[i], max_action->destination, num_ships);
			logger->log();
		}
	}*/

	//**************************************************//

	sprintf(logger->buffer, "TURN END: %d ", ID());
	logger->log();
}
<commit_msg>I am implementing the DoTurn function in Colony class, it is getting a little complicated<commit_after>/*
 * Colony.cpp
 *
 *  Created on: Nov 17, 2014
 *      Author: sina
 */

#include "Colony.h"
#include <iostream>
#include <algorithm>
using namespace std;

Colony::Colony(int id){
	this->id = id;

	char buffer[100];
	sprintf(buffer, "Colony_%d.log", id);
	logger = new Logger(buffer);

	for (int i = 0; i < COLONY_MAX_SIZE; i++){
		planets[i] = -1;
	}
	size = 0;

	attackThreshold = 30;
}

Colony::~Colony() {

}

// The DoTurn function is where your code goes. The PlanetWars object contains
// the state of the game, including information about all planets and fleets
// that currently exist. Inside this function, you issue orders using the
// pw.IssueOrder() function. For example, to send 10 ships from planet 3 to
// planet 8, you would say pw.IssueOrder(3, 8, 10).
void Colony::DoTurn(const PlanetWars &pw, Colony *destination) {
	sprintf(logger->buffer, "TURN START: %d ", ID());
	logger->log();

	int num_my_fleets = pw.MyFleets().size();

	//**************************************************//

	// (2) Find my eligible planets to attack.
	vector<int> sources;
	for (size_t i = 0; i < size; i++){
		const Planet* planet = pw.GetPlanet(planets[i]);
		if (planet->Owner() == ME){
			if (planet->NumShips() > ELIGIBILITY_THRESHOLD_PERCENT)
				sources.push_back(planets[i]);
		}
	}

	// (3) Discretize strongness of each planet in the colony
	vector<int> planet_strongness;

	for (size_t i = 0; i < COLONY_MAX_SIZE; i++){
		if (i > destination->Size()){
			planet_strongness.push_back(0);
		}else{
			int ships_num = pw.GetPlanet(destination->Planets()[i])->NumShips();
			if (ships_num < PLANET_STRONGNESS_MIN){
				ships_num = PLANET_STRONGNESS_MIN;
			}else if (ships_num >= PLANET_STRONGNESS_MAX){
				ships_num = PLANET_STRONGNESS_MAX;
			}

			ships_num = (ships_num - PLANET_STRONGNESS_MIN) / PLANET_STRONGNESS_STEP;
			planet_strongness.push_back(ships_num);
		}
	}

	bool random_action = (((rand() % 1000) / 1000.0) > EXPLOITATION_PLANET) ? true : false;
	double max_q = -99999999;
	Action* max_action = 0;
	int action_t = -1;

	if (random_action){
		while (true){
			action_t = rand() % q_value_obj->actions.size();
			max_action = q_value_obj->actions[action_t];

			if (max_action->destination == -1){
				return;
			}
		}
	}else{
		for (size_t i = 0; i < q_value_obj->actions.size(); i++){
			vector<int> indexes;
			for (size_t j = 0; j < planet_strongness.size(); j++){
				indexes.push_back(planet_strongness[j]);
			}
			if (num_my_fleets > MAX_NUM_MY_FLEETS){
				indexes.push_back(MAX_NUM_MY_FLEETS);
			}else{
				indexes.push_back(num_my_fleets);
			}

			if (planets[q_value_obj->actions[i]->destination] != -1 && q_value_obj->q_values[get_index_for(indexes)] > max_q){
				max_q = q_value_obj->q_values[get_index_for(indexes)];
				max_action = q_value_obj->actions[i];
				action_t = i;
			}
		}

		if (max_q == -99999999){
			return;
		}
	}

	if (sources.size() > 0){
		// (4) Send half the ships from my strongest planet to the weakest
		// planet that I do not own.
		for (size_t i = 0; i < sources.size(); i++){
			int num_ships = pw.GetPlanet(sources[i])->NumShips() / 2;
			pw.IssueOrder(sources[i], max_action->destination, num_ships);
			sprintf(logger->buffer, "Attack %d: Source: %d, Dest.: %d Ships: %d", (i + 1), sources[i], max_action->destination, num_ships);
			logger->log();
		}
	}

	//**************************************************//

	sprintf(logger->buffer, "TURN END: %d ", ID());
	logger->log();
}

{'fraction_non_alphanumeric': 0.18071995118974984, 'fraction_numerical': 0.01683953630262355, 'mean_word_length': 3.846836191602602, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 90, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 4, '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': '2293359', 'n_tokens_mistral': 3450, 'n_tokens_neox': 3050, 'n_words': 751}