test_Ansys.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. #include <gtest/gtest.h>
  2. #include <json.hpp>
  3. #include <chrono>
  4. #include <httplib.h>
  5. #include "Registration.h"
  6. #include "OptimizerEvolutionary.h"
  7. #include "ProcessorStandard.h"
  8. #include <math.h>
  9. #include <thread>
  10. #include <windows.h>
  11. #include <string>
  12. #include <iostream>
  13. using namespace mdd;
  14. namespace TEST_ANSYS {
  15. auto regi = Registration();
  16. std::string ExePath() {
  17. TCHAR buffer[MAX_PATH] = { 0 };
  18. GetModuleFileName(NULL, buffer, MAX_PATH);
  19. std::string::size_type pos = std::string(buffer).find_last_of("\\/");
  20. return std::string(buffer).substr(0, pos);
  21. }
  22. void evaluateIndividuum(OptimizerEvolutionary& optimizer, IModule::Ptr module_ptr, OptimizerEvolutionary::Permutation& ind) {
  23. auto start = std::chrono::steady_clock::now();
  24. json j;
  25. j = ind.dna[0];
  26. std::cout << j.dump() << ": " << optimizer.evaluateFitness(ind);
  27. std::cout << "\t| " << module_ptr->getOutput(0)->getValue()[0] << "\t| " << module_ptr->getOutput(1)->getValue()[0] << "\t| " << module_ptr->getOutput(2)->getValue()[0] << "\t| " << module_ptr->getOutput(3)->getValue()[0] << std::endl;
  28. std::cout << "\t| " << module_ptr->getOutput(4)->getValue()[0] << "\t| " << module_ptr->getOutput(5)->getValue()[0] << std::endl;
  29. auto end = std::chrono::steady_clock::now();
  30. std::cout << "\t| " << std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count() << "ms" << std::endl;
  31. std::cout << std::endl;
  32. }
  33. /*
  34. TEST(ModuleHTTP, test_ansys_sql_server) {
  35. std::cout << ExePath() << std::endl;
  36. auto sql = regi.generateModule("ModuleSQL");
  37. json config = sql->getConfiguration();
  38. config["configure"]["database"]["value"] = "../../../../../lib/test/db/materials.db";
  39. sql->configure(config);
  40. auto http = regi.generateModule("ModuleHTTP");// ("", "localhost", 8888);
  41. config = http->getConfiguration())["configure"];
  42. config["file"]["value"] = "";
  43. config["url"]["value"] = "localhost";
  44. config["port"]["value"] = 8888;
  45. http->configure(config.dump());
  46. //../../../lib/test/server/server-ansys.py
  47. sql->getInput(0)->setValue() = { 1 };
  48. for (size_t i = 0; i < http->getNumInputs(); i++)
  49. {
  50. std::cout << http->getInput(i)->getValue()[0] << std::endl;
  51. }
  52. for (size_t i = 2; i < sql->getNumOutputs(); i++)//0: ID 1: Name
  53. {
  54. http->getInput(i - 2)->connect(sql->getOutput(i));
  55. }
  56. http->getInput(http->getNumInputs() - 1)->setOptimizability(true);
  57. limits limit;
  58. limit.min = { 0, 0, 0, 0, 0, 0 };
  59. limit.max = { 90, 90, 90, 90, 90, 90 };
  60. limit.rule = "val[0] != val[5] || val[1] != val[4] || val[2] != val[3]";
  61. http->getInput(http->getNumInputs() - 1)->setLimits() = limit;
  62. http->getInput(http->getNumInputs() - 1)->setValue() = { 0,0,0,0,0,90 };
  63. http->getOutput(0)->setOptimizability(true);
  64. sql->update();
  65. //test SQL
  66. std::cout << "SQL-TEST" << std::endl;
  67. for (size_t i = 2; i < sql->getNumOutputs(); i++)
  68. {
  69. std::cout << sql->getOutput(i)->getValue()[0] << std::endl;
  70. }
  71. http->update();
  72. EXPECT_FLOAT_EQ(http->getOutput(3)->getValue()[0], 37.154861);
  73. }
  74. TEST(ModuleHTTP, test_configurations) {
  75. auto sql = regi.generateModule("ModuleSQL");
  76. json config = sql->getConfiguration();
  77. config["configure"]["database"]["value"] = "../../../../../lib/test/db/materials.db";
  78. sql->configure(config);
  79. auto http = regi.generateModule("ModuleHTTP");// ("", "localhost", 8888);
  80. config = http->getConfiguration();
  81. config["configure"]["file"]["value"] = "";
  82. config["configure"]["url"]["value"] = "localhost";
  83. config["configure"]["port"]["value"] = 8888;
  84. http->configure(config);
  85. //../../../lib/test/server/server-ansys.py
  86. sql->getInput(0)->setValue() = { 1 };
  87. for (size_t i = 0; i < http->getNumInputs(); i++)
  88. {
  89. std::cout << http->getInput(i)->getValue()[0] << std::endl;
  90. }
  91. for (size_t i = 2; i < sql->getNumOutputs(); i++)//0: ID 1: Name
  92. {
  93. http->getInput(i - 2)->connect(sql->getOutput(i));
  94. }
  95. http->getInput(http->getNumInputs() - 1)->setOptimizability(true);
  96. limits limit;
  97. limit.min = { 0, 0, 0, 0, 0, 0 };
  98. limit.max = { 90, 90, 90, 90, 90, 90 };
  99. limit.rule = "val[0] != val[5] || val[1] != val[4] || val[2] != val[3]";
  100. http->getInput(http->getNumInputs() - 1)->setLimits() = limit;
  101. http->getOutput(0)->setOptimizability(true);
  102. std::shared_ptr<ProcessorStandard> processor = std::make_shared<ProcessorStandard>();;
  103. processor->addModule(sql);
  104. processor->addModule(http);
  105. OptimizerEvolutionary optimizer;
  106. config = optimizer.getConfiguration());
  107. config["configure"]["converges"]["value"] = 3;
  108. config["configure"]["grow generation"]["value"] = 20;
  109. config["configure"]["max fitness"]["value"] = 0.13;
  110. config["configure"]["min generations"]["value"] = 5;
  111. optimizer.configure(config.dump());
  112. optimizer.connect(processor);
  113. optimizer.setEvaluation("out0");
  114. //auto res = optimizer.update();
  115. OptimizerEvolutionary::Individual ind;
  116. auto start = std::chrono::steady_clock::now();
  117. auto end = std::chrono::steady_clock::now();
  118. ind.dna = { { 0,0,0,0,0,0 } };
  119. evaluateIndividuum(optimizer, http, ind);
  120. ind.dna = { { 90,90,90,90,90,90 } };
  121. evaluateIndividuum(optimizer, http, ind);
  122. ind.dna = { { 90,0,0,0,0,0 } };
  123. evaluateIndividuum(optimizer, http, ind);
  124. ind.dna = { { 0,0,0,0,0,90 } };
  125. evaluateIndividuum(optimizer, http, ind);
  126. ind.dna = { { 0,90,90,90,90,90 } };
  127. evaluateIndividuum(optimizer, http, ind);
  128. ind.dna = { { 90,90,90,90,90,0 } };
  129. evaluateIndividuum(optimizer, http, ind);
  130. ind.dna = { { 90,0,0,0,90,0 } };
  131. evaluateIndividuum(optimizer, http, ind);
  132. ind.dna = { { 90,0,0,0,90,0 } };
  133. evaluateIndividuum(optimizer, http, ind);
  134. ind.dna = { { 90,0,0,0,90,0 } };
  135. evaluateIndividuum(optimizer, http, ind);
  136. ind.dna = { { 0,0,90,90,90,90 } };
  137. evaluateIndividuum(optimizer, http, ind);
  138. ind.dna = { { 0,0,90,90,90,90 } };
  139. evaluateIndividuum(optimizer, http, ind);
  140. EXPECT_TRUE(ind.fitness <= 100.0);
  141. }
  142. TEST(ModuleHTTP, test_ansys_server) {
  143. auto sql = regi.generateModule("ModuleSQL");
  144. json config = sql->getConfiguration();
  145. config["configure"]["database"]["value"] = "../../../../../lib/test/db/materials.db";
  146. sql->configure(config);
  147. auto http = regi.generateModule("ModuleHTTP");// ("", "localhost", 8888);
  148. config = http->getConfiguration();
  149. config["configure"]["file"]["value"] = "";
  150. config["configure"]["url"]["value"] = "localhost";
  151. config["configure"]["port"]["value"] = 8888;
  152. http->configure(config);
  153. //../../../lib/test/server/server-ansys.py
  154. sql->getInput(0)->setValue() = { 1 };
  155. for (size_t i = 0; i < http->getNumInputs(); i++)
  156. {
  157. std::cout << http->getInput(i)->getValue()[0] << std::endl;
  158. }
  159. for (size_t i = 2; i < sql->getNumOutputs(); i++)//0: ID 1: Name
  160. {
  161. http->getInput(i - 2)->connect(sql->getOutput(i));
  162. }
  163. http->getInput(http->getNumInputs() - 1)->setOptimizability(true);
  164. limits limit;
  165. limit.min = { 0, 0, 0, 0, 0, 0 };
  166. limit.max = { 90, 90, 90, 90, 90, 90 };
  167. limit.rule = "val[0] != val[5] or val[1] != val[4] or val[2] != val[3]";
  168. http->getInput(http->getNumInputs() - 1)->setLimits() = limit;
  169. http->getOutput(0)->setOptimizability(true);
  170. std::shared_ptr<ProcessorStandard> processor = std::make_shared<ProcessorStandard>();;
  171. processor->addModule(sql);
  172. processor->addModule(http);
  173. OptimizerEvolutionary optimizer;
  174. config = optimizer.getConfiguration();
  175. config["configure"]["converges"]["value"] = 3;
  176. config["configure"]["grow generation"]["value"] = 20;
  177. config["configure"]["max fitness"]["value"] = 0.13;
  178. config["configure"]["min generations"]["value"] = 5;
  179. optimizer.configure(config);
  180. optimizer.connect(processor);
  181. optimizer.setEvaluation("out0");
  182. auto start = std::chrono::steady_clock::now();
  183. auto res = optimizer.update();
  184. auto end = std::chrono::steady_clock::now();
  185. std::cout << optimizer.getBests()[0].fitness;
  186. std::cout << " | " << std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count() << "ms" << std::endl;
  187. std::cout << optimizer.evaluateFitness(optimizer.getBests()[0]) << std::endl;
  188. EXPECT_TRUE(optimizer.getBests()[0].fitness <= 100.0);
  189. }
  190. TEST(ModuleHTTP, test_configurations) {
  191. auto sql = regi.generateModule("ModuleSQL");
  192. json config = sql->getConfiguration();
  193. config["configure"]["database"]["value"] = "../../../../../lib/test/db/materials.db";
  194. sql->configure(config);
  195. auto http = regi.generateModule("ModuleHTTP");// ("", "localhost", 8888);
  196. config = http->getConfiguration();
  197. config["configure"]["file"]["value"] = "";
  198. config["configure"]["url"]["value"] = "localhost";
  199. config["configure"]["port"]["value"] = 8888;
  200. http->configure(config);
  201. //../../../lib/test/server/server-ansys.py
  202. sql->getInput(0)->setValue() = { 1 };
  203. for (size_t i = 0; i < http->getNumInputs(); i++)
  204. {
  205. std::cout << http->getInput(i)->getValue()[0] << std::endl;
  206. }
  207. for (size_t i = 2; i < sql->getNumOutputs(); i++)//0: ID 1: Name
  208. {
  209. http->getInput(i - 2)->connect(sql->getOutput(i));
  210. }
  211. http->getInput(http->getNumInputs() - 1)->setOptimizability(true);
  212. limits limit;
  213. limit.min = { 0, 0, 0, 0, 0, 0 };
  214. limit.max = { 90, 90, 90, 90, 90, 90 };
  215. limit.rule = "val[0] != val[5] || val[1] != val[4] || val[2] != val[3]";
  216. http->getInput(http->getNumInputs() - 1)->setLimits() = limit;
  217. http->getOutput(0)->setOptimizability(true);
  218. http->getOutput(4)->setOptimizability(true);
  219. std::shared_ptr<ProcessorStandard> processor = std::make_shared<ProcessorStandard>();;
  220. processor->addModule(sql);
  221. processor->addModule(http);
  222. OptimizerEvolutionary optimizer;
  223. config = optimizer.getConfiguration();
  224. config["configure"]["converges"]["value"] = 3;
  225. config["configure"]["grow generation"]["value"] = 20;
  226. config["configure"]["max fitness"]["value"] = 0.13;
  227. config["configure"]["min generations"]["value"] = 5;
  228. optimizer.configure(config);
  229. optimizer.connect(processor);
  230. optimizer.setEvaluation("2^(-35+out0)+4.2233*10^(-11)*exp(-out1^5/643634300000)*out1^5");
  231. //auto res = optimizer.update();
  232. OptimizerEvolutionary::Individual ind;
  233. ind.dna = { { 0,0,0,0,0,0 } };
  234. evaluateIndividuum(optimizer, http, ind);
  235. ind.dna = { { 90,90,90,90,90,90 } };
  236. evaluateIndividuum(optimizer, http, ind);
  237. ind.dna = { { 90,0,0,0,0,0 } };
  238. evaluateIndividuum(optimizer, http, ind);
  239. ind.dna = { { 0,0,0,0,0,90 } };
  240. evaluateIndividuum(optimizer, http, ind);
  241. ind.dna = { { 0,90,90,90,90,90 } };
  242. evaluateIndividuum(optimizer, http, ind);
  243. ind.dna = { { 0,0,90,90,90,90 } };
  244. evaluateIndividuum(optimizer, http, ind);
  245. ind.dna = { { 90,90,90,0,90,90 } };
  246. evaluateIndividuum(optimizer, http, ind);
  247. ind.dna = { { 0,0,0,90,0,0 } };
  248. evaluateIndividuum(optimizer, http, ind);
  249. ind.dna = { { 0,0,90,0,90,0 } };
  250. evaluateIndividuum(optimizer, http, ind);
  251. EXPECT_TRUE(ind.fitness <= 100.0);
  252. }
  253. TEST(ModuleHTTP, test_ansys_server) {
  254. auto sql = regi.generateModule("ModuleSQL");
  255. json config = sql->getConfiguration();
  256. config["configure"]["database"]["value"] = "../../../../../lib/test/db/materials.db";
  257. sql->configure(config);
  258. auto http = regi.generateModule("ModuleHTTP");// ("", "localhost", 8888);
  259. config = http->getConfiguration();
  260. config["configure"]["file"]["value"] = "";
  261. config["configure"]["url"]["value"] = "localhost";
  262. config["configure"]["port"]["value"] = 8888;
  263. http->configure(config);
  264. //../../../lib/test/server/server-ansys.py
  265. sql->getInput(0)->setValue() = { 1 };
  266. for (size_t i = 0; i < http->getNumInputs(); i++)
  267. {
  268. std::cout << http->getInput(i)->getValue()[0] << std::endl;
  269. }
  270. for (size_t i = 2; i < sql->getNumOutputs(); i++)//0: ID 1: Name
  271. {
  272. http->getInput(i - 2)->connect(sql->getOutput(i));
  273. }
  274. http->getInput(http->getNumInputs() - 1)->setOptimizability(true);
  275. limits limit;
  276. limit.min = { 0, 0, 0, 0, 0, 0 };
  277. limit.max = { 90, 90, 90, 90, 90, 90 };
  278. limit.rule = "val[0] != val[5] or val[1] != val[4] or val[2] != val[3]";
  279. http->getInput(http->getNumInputs() - 1)->setLimits() = limit;
  280. http->getOutput(0)->setOptimizability(true);
  281. http->getOutput(4)->setOptimizability(true);
  282. std::shared_ptr<ProcessorStandard> processor = std::make_shared<ProcessorStandard>();;
  283. processor->addModule(sql);
  284. processor->addModule(http);
  285. OptimizerEvolutionary optimizer;
  286. config = optimizer.getConfiguration();
  287. config["configure"]["converges"]["value"] = 3;
  288. config["configure"]["grow generation"]["value"] = 20;
  289. config["configure"]["max fitness"]["value"] = 0.13;
  290. config["configure"]["min generations"]["value"] = 5;
  291. optimizer.configure(config);
  292. optimizer.connect(processor);
  293. optimizer.setEvaluation("2^(-35+out0)+4.2233*10^(-11)*exp(-out1^5/643634300000)*out1^5");
  294. auto start = std::chrono::steady_clock::now();
  295. auto res = optimizer.update();
  296. auto end = std::chrono::steady_clock::now();
  297. std::cout << optimizer.getBests()[0].fitness;
  298. std::cout << " | " << std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count() << "ms" << std::endl;
  299. std::cout << optimizer.evaluateFitness(optimizer.getBests()[0]) << std::endl;
  300. EXPECT_TRUE(optimizer.getBests()[0].fitness <= 10.0);
  301. }
  302. //*/
  303. }