Advertisement
Trainlover08

include/ai_folder/ai_versions/ai.cpp

Oct 30th, 2024
21
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 19.86 KB | None | 0 0
  1. // (include/ai_folder/ai_versions/ai.cpp)
  2. // A very early ineffective verion of me trying rl. Included to show pregression
  3.  
  4.  
  5. #include <iostream>
  6. #include <fstream>
  7. #include <sstream>
  8. #include <vector>
  9. #include <string>
  10. #include <cstdlib>
  11. #include <cmath>
  12. #include <algorithm>
  13. #include <ctime>
  14. #include <utility>
  15.  
  16. // Function to apply ReLU activation
  17. double ReLU(double x)
  18. {
  19. return std::max(0.0, x);
  20. }
  21.  
  22. class LayerDense
  23. {
  24. public:
  25. void initialize(size_t n_inputs, size_t n_neurons, const std::vector<std::vector<double>> &weights, const std::vector<double> &biases);
  26. std::vector<std::vector<double>> forward(const std::vector<std::vector<double>> &inputs);
  27. bool apply_ReLU = true;
  28.  
  29. private:
  30. std::vector<std::vector<double>> weights;
  31. std::vector<double> biases;
  32. };
  33.  
  34. void LayerDense::initialize(size_t n_inputs, size_t n_neurons, const std::vector<std::vector<double>> &init_weights, const std::vector<double> &init_biases)
  35. {
  36. weights = init_weights;
  37. biases = init_biases;
  38. }
  39.  
  40. std::vector<std::vector<double>> LayerDense::forward(const std::vector<std::vector<double>> &inputs)
  41. {
  42. std::vector<std::vector<double>> output(inputs.size(), std::vector<double>(biases.size(), 0.0));
  43. for (size_t i = 0; i < inputs.size(); ++i)
  44. {
  45. for (size_t k = 0; k < biases.size(); ++k)
  46. {
  47. double sum = 0.0;
  48. for (size_t j = 0; j < inputs[i].size(); ++j)
  49. {
  50. sum += inputs[i][j] * weights[j][k];
  51. }
  52. output[i][k] = sum + biases[k];
  53. // Apply ReLU activation
  54. if (apply_ReLU == true)
  55. {
  56. output[i][k] = ReLU(output[i][k]);
  57. }
  58. }
  59. }
  60. return output;
  61. }
  62.  
  63. namespace ai{
  64.  
  65. class NeuralNetwork
  66. {
  67. public:
  68. std::string FILENAME = "NNQtable";
  69. double greedy_start = 1.0f;
  70. unsigned char selected_models = 1;
  71. unsigned short episodes = 1;
  72. unsigned short current_epoch = 0;
  73. double LEARNINGRATE = 1.0;
  74.  
  75. private:
  76. double greedy_value()
  77. {
  78. double greedy_min = greedy_start * 0.001f;
  79. double decay_rate = LEARNINGRATE;
  80. return (greedy_min + (greedy_start - greedy_min)) * std::exp(-decay_rate * current_epoch);
  81. }
  82.  
  83. double range_rand(const int &range)
  84. {
  85. return ((std::rand() % (range * 2)) - range) / static_cast<double>(range / 2);
  86. }
  87.  
  88. double greedy_adjusted_value(double &inputs, const double &greedy_val)
  89. {
  90. return inputs + (range_rand(1000) * greedy_val);
  91. }
  92.  
  93. std::vector<std::string> file_name(const std::string &base_file_name)
  94. {
  95. std::vector<std::string> output;
  96. for (unsigned char j = 0; j < selected_models; ++j)
  97. {
  98. std::string index_str = std::to_string(j + 1);
  99. std::string file_name = base_file_name + index_str + ".txt";
  100. output.push_back(file_name);
  101. }
  102. return output;
  103. }
  104.  
  105. std::vector<std::vector<std::vector<std::vector<double>>>> readTxt(const std::string &file_name, unsigned char iteration_number)
  106. {
  107. std::vector<std::vector<std::vector<std::vector<double>>>> output;
  108.  
  109. std::string line;
  110. std::ifstream file(file_name);
  111.  
  112. while (std::getline(file, line))
  113. {
  114. std::istringstream iss(line);
  115. unsigned char i, j, k, l;
  116. double value;
  117. if (iss >> i >> j >> k >> l >> value)
  118. {
  119. while (output.size() <= i)
  120. output.push_back({});
  121. while (output[i].size() <= j)
  122. output[i].push_back({});
  123. while (output[i][j].size() <= k)
  124. output[i][j].push_back({});
  125. output[i][j][k].push_back(value);
  126. }
  127. }
  128.  
  129. file.close();
  130. return output;
  131. }
  132.  
  133. std::vector<std::vector<std::vector<std::vector<double>>>> mutated_values(std::vector<std::vector<std::vector<std::vector<double>>>> input_data)
  134. {
  135. for (unsigned short index = 0; index < input_data.size(); ++index)
  136. {
  137. for (unsigned short k = 0; k < input_data[index].size(); ++k)
  138. {
  139. for (unsigned short j = 0; j < input_data[index][k].size(); ++j)
  140. {
  141. for (unsigned short a = 0; a < input_data[index][k][j].size(); ++a)
  142. {
  143. input_data[index][k][j][a] = greedy_adjusted_value(input_data[index][k][j][a], greedy_value());
  144. }
  145. }
  146. }
  147. }
  148. return input_data;
  149. }
  150.  
  151. std::vector<std::vector<std::vector<std::vector<std::vector<double>>>>> generate_batch(const unsigned char n_batch_size)
  152. {
  153. std::vector<std::vector<std::vector<std::vector<std::vector<double>>>>> output(n_batch_size);
  154. std::vector<std::string> names = file_name(FILENAME);
  155.  
  156. unsigned char actual_batch_size = std::min(n_batch_size, static_cast<unsigned char>(names.size()));
  157.  
  158. for (char j = 0; j < actual_batch_size; ++j)
  159. {
  160. std::string name_gen = names.at(j);
  161. std::vector<std::vector<std::vector<std::vector<double>>>> input = mutated_values(readTxt(name_gen, j));
  162. output[j] = input;
  163. }
  164. return output;
  165. }
  166.  
  167. void creation_cycle(std::ofstream &newFile)
  168. {
  169. static bool d = false;
  170. newFile << 0.001f;
  171.  
  172. if (!d)
  173. {
  174. newFile << ",";
  175. }
  176. else
  177. {
  178. newFile << " ";
  179. }
  180. d = !d;
  181. }
  182.  
  183.  
  184. void create_model(const std::vector<std::string> &file_names, char n_input, char &n_hidden_neurons, char &n_hidden_layers, char &n_output)
  185. {
  186. for (unsigned char i = 0; i < file_names.size(); ++i)
  187. {
  188. std::string filename = file_names[i];
  189. std::ofstream newFile(filename);
  190. if (!newFile.is_open())
  191. {
  192. std::cerr << "Error: Could not open file " << filename << std::endl;
  193. continue;
  194. }
  195.  
  196. for (unsigned char j = 0; j < (n_input * 2); ++j)
  197. {
  198. creation_cycle(newFile);
  199. }
  200. newFile << std::endl;
  201.  
  202. for (char k = 0; k < n_hidden_layers; ++k)
  203. {
  204. for (unsigned char m = 0; m < (n_hidden_neurons * 2); ++m)
  205. {
  206. creation_cycle(newFile);
  207. }
  208. newFile << std::endl;
  209. }
  210.  
  211. for (unsigned char g = 0; g < (n_output * 2); ++g)
  212. {
  213. creation_cycle(newFile);
  214. }
  215. newFile << std::endl;
  216.  
  217. newFile.close();
  218. }
  219. }
  220.  
  221.  
  222. void create_multiple_models(char n_input, char n_hidden_neurons, char n_hidden_layers, char n_output)
  223. {
  224. for (unsigned char j = 0; j < selected_models; ++j)
  225. {
  226. create_model(file_name(FILENAME), n_input, n_hidden_neurons, n_hidden_layers, n_output);
  227. }
  228. }
  229.  
  230. unsigned short total_lines(const std::string &input)
  231. {
  232. std::ifstream file(input);
  233. unsigned int count = 0;
  234. std::string line;
  235.  
  236. while (std::getline(file, line))
  237. {
  238. ++count;
  239. }
  240.  
  241. file.close();
  242. return count;
  243. }
  244.  
  245. unsigned short n_neurons_of_line(const std::string &input, unsigned short target_line)
  246. {
  247. std::ifstream file(input);
  248. unsigned short count = 0;
  249. std::string line;
  250. char character;
  251. unsigned short index = 0;
  252.  
  253. for (unsigned short i = 0; i < target_line; ++i)
  254. {
  255. std::getline(file, line);
  256. ++count;
  257. }
  258.  
  259. while (file.get(character))
  260. {
  261. if (character == '\n')
  262. {
  263. return index / 3;
  264. }
  265. if (character == ' ' || character == '\t')
  266. {
  267. if (character == ' ')
  268. {
  269. ++index;
  270. }
  271. }
  272. }
  273. file.close();
  274. return index;
  275. }
  276.  
  277. double find_value(const std::string &file_name, unsigned char nn_layer, unsigned short dataset_in_layer, unsigned char weight_or_bias)
  278. {
  279. double output;
  280. std::string outputstr;
  281. std::ifstream file(file_name);
  282. std::string line;
  283. char character;
  284.  
  285. for (unsigned char i = 0; i < nn_layer;)
  286. {
  287. std::getline(file, line);
  288. }
  289.  
  290. for (unsigned short k = 0; k < dataset_in_layer; ++k)
  291. {
  292. file.get(character);
  293. if (character == ' ')
  294. {
  295. file.get(character);
  296. if (character == ' ')
  297. {
  298. ++k;
  299. }
  300. }
  301.  
  302. if (k + 1 == dataset_in_layer)
  303. {
  304. if (weight_or_bias == 0)
  305. {
  306. file.get(character);
  307. while (character != ' ')
  308. {
  309. file.get(character);
  310. }
  311. file >> outputstr;
  312. }
  313. else
  314. {
  315. file.get(character);
  316. while (character != ' ')
  317. {
  318. file.get(character);
  319. }
  320. file >> outputstr;
  321. }
  322. output = std::stof(outputstr);
  323. }
  324. }
  325.  
  326. return output;
  327. }
  328.  
  329. std::vector<std::vector<std::vector<std::vector<std::vector<double>>>>> full_read_of_files(const std::string &base_file_name)
  330. {
  331. std::vector<std::vector<std::vector<std::vector<std::vector<double>>>>> output;
  332. std::vector<std::string> names = file_name(base_file_name);
  333. output.resize(names.size());
  334.  
  335. for (unsigned char my_file_name = 0; my_file_name < names.size(); my_file_name++)
  336. {
  337. std::ifstream nfile(names[my_file_name]);
  338.  
  339. std::stringstream buffer;
  340. buffer << nfile.rdbuf(); // Read the entire file into the stringstream
  341. nfile.close();
  342.  
  343. std::string file_content = buffer.str();
  344. std::stringstream file_stream(file_content);
  345.  
  346. unsigned short total_lines_of_file = total_lines(names[my_file_name]);
  347. std::string temp_string = "";
  348.  
  349. for (unsigned short file_lines = 0; file_lines < total_lines_of_file;)
  350. {
  351. char character;
  352. bool weight = false;
  353. unsigned short set_index = 0;
  354.  
  355. if (output[my_file_name].size() <= file_lines)
  356. {
  357. output[my_file_name].resize(file_lines + 1);
  358. }
  359. if (output[my_file_name][file_lines].size() <= set_index)
  360. {
  361. output[my_file_name][file_lines].resize(set_index + 1);
  362. output[my_file_name][file_lines][set_index].resize(2);
  363. }
  364.  
  365. std::vector<char> temp_char_string(50);
  366. unsigned short temp_char_string_index = 0;
  367. double doubleValue;
  368. file_stream.get(character);
  369.  
  370. switch (character)
  371. {
  372. case ' ':
  373. weight = false;
  374. output[my_file_name][file_lines][set_index][weight].push_back(doubleValue);
  375. set_index++;
  376. break;
  377. case ',':
  378. weight = false;
  379. doubleValue = std::stof(temp_string);
  380. output[my_file_name][file_lines][set_index][weight].push_back(doubleValue);
  381. temp_string.clear();
  382. break;
  383. case '\n':
  384. file_lines++;
  385. break;
  386. default:
  387. temp_string.push_back(character);
  388. break;
  389. }
  390. }
  391. }
  392.  
  393. return output;
  394. }
  395.  
  396. std::vector<std::vector<std::vector<std::vector<std::vector<double>>>>> create_episodes(std::vector<std::vector<std::vector<std::vector<std::vector<double>>>>> input_vector)
  397. {
  398. std::vector<std::vector<std::vector<std::vector<std::vector<double>>>>> output;
  399.  
  400. int TOTALNETWORKS = input_vector.size() * episodes;
  401.  
  402. output.resize(TOTALNETWORKS);
  403.  
  404. for (int currentSet = 0; currentSet < input_vector.size(); ++currentSet)
  405. {
  406. int intigralNet = 0;
  407. for (int currentNetwork = 0; currentNetwork < episodes; ++currentNetwork)
  408. {
  409. output.push_back(input_vector[currentSet]);
  410. ++intigralNet;
  411. }
  412. }
  413. return output;
  414. }
  415.  
  416. std::vector<std::vector<std::vector<std::vector<std::vector<double>>>>> mutate_batch(std::vector<std::vector<std::vector<std::vector<std::vector<double>>>>> input)
  417. {
  418. for (unsigned short a = 0; a < input.size(); ++a)
  419. {
  420. for (unsigned short b = 0; b < input[a].size(); ++b)
  421. {
  422. for (unsigned short c = 0; c < input[a][b].size(); ++c)
  423. {
  424. for (unsigned short d = 0; d < input[a][b][c].size(); ++d)
  425. {
  426. for (unsigned short e = 0; e < input[a][b][c][d].size(); ++e)
  427. {
  428. if (input[a][b][c][d].size() > e)
  429. {
  430. input[a][b][c][d][e] = greedy_adjusted_value(input[a][b][c][d][e], greedy_value());
  431. }
  432. }
  433. }
  434. }
  435. }
  436. }
  437.  
  438. return input;
  439. }
  440.  
  441. std::vector<std::vector<std::vector<double>>> run_batch(std::vector<std::vector<std::vector<std::vector<std::vector<double>>>>> &input, std::vector<std::vector<std::vector<double>>> &input_data)
  442. {
  443. LayerDense layer;
  444. std::vector<std::vector<std::vector<double>>> output(input.size());
  445.  
  446. for (size_t input_first_index = 0; input_first_index < input.size(); ++input_first_index)
  447. {
  448. size_t num_layers = input[input_first_index].size();
  449. std::vector<std::vector<double>> temp_output;
  450.  
  451. for (size_t input_second_index = 0; input_second_index < num_layers; ++input_second_index)
  452. {
  453. size_t n_inputs;
  454. std::vector<std::vector<double>> weights;
  455. std::vector<double> biases;
  456. std::vector<std::vector<double>> forward_inputs;
  457.  
  458. for (size_t neuron_set_index = 0; neuron_set_index < input[input_first_index][input_second_index].size(); ++neuron_set_index)
  459. {
  460. weights.push_back(std::vector<double>());
  461. }
  462.  
  463. for (size_t neuron_set_index = 0; neuron_set_index < input[input_first_index][input_second_index].size(); ++neuron_set_index)
  464. {
  465. if (input_second_index == 0)
  466. {
  467. n_inputs = input_data.size();
  468. forward_inputs = input_data[neuron_set_index];
  469. }
  470. else
  471. {
  472. n_inputs = input[input_first_index][input_second_index][neuron_set_index].size();
  473. }
  474.  
  475. double temp_weight = input[input_first_index][input_second_index][neuron_set_index][0][0];
  476. weights[neuron_set_index].push_back(temp_weight);
  477. double temp_biases = input[input_first_index][input_second_index][neuron_set_index][0][1];
  478. biases.push_back(temp_biases);
  479. }
  480.  
  481. layer.initialize(n_inputs, input[input_first_index][input_second_index].size(), weights, biases);
  482.  
  483. if (input_second_index != 0)
  484. {
  485. forward_inputs = temp_output;
  486. }
  487.  
  488. if (num_layers == input_second_index + 1)
  489. {
  490. layer.apply_ReLU = false;
  491. }
  492.  
  493. temp_output = layer.forward(forward_inputs);
  494. layer.apply_ReLU = true;
  495. }
  496.  
  497. output[input_first_index] = temp_output;
  498. }
  499. return output;
  500. }
  501.  
  502. std::vector<std::vector<std::vector<std::vector<std::vector<double>>>>> find_best_networks(std::vector<std::vector<std::vector<std::vector<std::vector<double>>>>> &vector_of_networks, std::vector<double> &vector_of_scores)
  503. {
  504. std::vector<std::vector<std::vector<std::vector<std::vector<double>>>>> output;
  505.  
  506. std::vector<std::pair<double, int>> score_and_index;
  507.  
  508. for (int index = 0; index < vector_of_scores.size(); ++index)
  509. {
  510. score_and_index.push_back(std::make_pair(vector_of_scores[index], index));
  511. }
  512.  
  513. std::sort(score_and_index.begin(), score_and_index.end(), std::greater<std::pair<double, int>>());
  514.  
  515. std::vector<int> top_episodes;
  516.  
  517. for (int second_index = 0; second_index < selected_models; ++second_index)
  518. {
  519. top_episodes.push_back(score_and_index[second_index].second);
  520. }
  521.  
  522. for (int a = 0; a < top_episodes.size(); ++a)
  523. {
  524. output.push_back(vector_of_networks[top_episodes[a]]);
  525. }
  526.  
  527. return output;
  528. }
  529.  
  530. void write_data(const std::string &base_file_name, std::vector<std::vector<std::vector<std::vector<std::vector<double>>>>> &input)
  531. {
  532. for (int a = 0; a < input.size(); ++a)
  533. {
  534. std::ofstream newFile((base_file_name + std::to_string(a + 1) + ".txt"));
  535. for (int b = 0; b < input[a].size(); ++b)
  536. {
  537. for (int c = 0; c < input[a][b].size(); ++c)
  538. {
  539. for (int d = 0; d < input[a][b][c].size(); ++d)
  540. {
  541. bool commaSwitch = false;
  542. for (int e = 0; e < input[a][b][c][d].size(); ++e)
  543. {
  544. newFile << input[a][b][c][d][e];
  545. if (!commaSwitch)
  546. {
  547. newFile << ',';
  548. commaSwitch = true;
  549. }
  550. else
  551. {
  552. newFile << ' ';
  553. commaSwitch = false;
  554. }
  555. }
  556. }
  557. }
  558. newFile << '\n';
  559. }
  560. newFile.close();
  561. }
  562. }
  563.  
  564. public:
  565. void createnewmodel(int input, int neurons, int layers, int output)
  566. {
  567. create_multiple_models(input, neurons, layers, output);
  568. }
  569.  
  570. std::vector<std::vector<std::vector<std::vector<std::vector<double>>>>> mutate()
  571. {
  572. return mutate_batch(create_episodes(full_read_of_files(FILENAME)));
  573. }
  574.  
  575. std::vector<std::vector<std::vector<double>>> run(std::vector<std::vector<std::vector<std::vector<std::vector<double>>>>> inputNetworks, std::vector<std::vector<std::vector<double>>> &SCORES)
  576. {
  577. return run_batch(inputNetworks, SCORES);
  578. }
  579.  
  580. std::vector<std::vector<std::vector<std::vector<std::vector<double>>>>> selection(std::vector<std::vector<std::vector<std::vector<std::vector<double>>>>> &mutatedData, std::vector<double> &testResults)
  581. {
  582. return find_best_networks(mutatedData, testResults);
  583. }
  584.  
  585. void write(std::vector<std::vector<std::vector<std::vector<std::vector<double>>>>> data)
  586. {
  587. write_data(FILENAME, data);
  588. }
  589.  
  590. std::vector<std::vector<std::vector<std::vector<std::vector<double>>>>> read() {
  591. return full_read_of_files(FILENAME);
  592. }
  593.  
  594. double ReLU(double input)
  595. {
  596. return ReLU(input);
  597. }
  598. };
  599. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement