瀏覽代碼

GUI Optimizer got all data

Willi Zschiebsch 4 年之前
父節點
當前提交
87bd2def98
共有 5 個文件被更改,包括 107 次插入112 次删除
  1. 18 0
      lib/include/OptimizerBase.h
  2. 7 26
      lib/include/OptimizerEvolutionary.h
  3. 47 1
      lib/src/OptimizerBase.cpp
  4. 34 84
      lib/src/OptimizerEvolutionary.cpp
  5. 1 1
      lib/test/test_Ansys.cpp

+ 18 - 0
lib/include/OptimizerBase.h

@@ -1,6 +1,7 @@
 #pragma once
 #include "IOptimizer.h"
 #include "exprtk.hpp"
+#include <thread>
 
 namespace mdd {
 	////////////////////////////////////////////////////////////
@@ -23,6 +24,10 @@ namespace mdd {
 
 		std::function<json(const json&)> _callback;
 
+		std::shared_ptr<std::thread> _thread_ptr;
+
+		int step = -1;
+
 		struct opt_state {
 			state module_state = state::STATE_ERROR;
 			double opt_value = 0;
@@ -33,6 +38,15 @@ namespace mdd {
 		bool configureChild(const json& config) override;
 
 	public:
+		struct Permutation {
+		public:
+			std::vector<std::vector<double>> dna;
+			double fitness = 0;
+			bool operator== (const Permutation& ind) {
+				return (dna == ind.dna);
+			};
+		};
+
 		bool connect(std::shared_ptr<IModule> module) override;
 		void updateLayout();
 		bool setEvaluation(std::string func) override;
@@ -46,5 +60,9 @@ namespace mdd {
 
 		void attachCallback(std::function<json(const json&)> callback) override;
 		//state update() override;
+		void callback_add_step(const std::vector<Permutation>& pers);
+		void callback_evaluate_opt(const opt_state& state, const Permutation& per);
+
+		~OptimizerBase();
 	};
 }

+ 7 - 26
lib/include/OptimizerEvolutionary.h

@@ -5,16 +5,6 @@
 
 namespace mdd {
 	class OptimizerEvolutionary : public OptimizerBase {
-	public:
-		struct Individual {
-		public:
-			std::vector<std::vector<double>> dna;
-			double fitness = 0;
-			bool operator== (const Individual& ind) {
-				return (dna == ind.dna);
-			};
-		};
-
 	protected:
 		static int random_num(double min, double max, double inc = 1.0)
 		{
@@ -22,35 +12,26 @@ namespace mdd {
 			return min + inc * (std::rand() % range);
 		}
 
-		Individual generateIndividual();
-		Individual combine(Individual par1, Individual par2);
+		Permutation generatePermutation();
+		Permutation combine(Permutation par1, Permutation par2);
 
-		std::vector<Individual> _children;
-		std::vector<Individual> _bests;
+		std::vector<Permutation> _children;
+		std::vector<Permutation> _bests;
 		int _min_generations;
 		double _max_fitness;
 		size_t _grow_generation;
 		size_t _converges;
 		double _precision;
 
-		int generation = -1;
-
-		void evolve(std::vector<Individual> parents);
+		void evolve(std::vector<Permutation> parents);
 		void evaluate(size_t ignore_len = 0);
 		std::vector<double> mutateGene(std::shared_ptr<IInput> input, std::vector<double> seed = std::vector<double>());
 		bool configureChild(const json& config) override;
 
 	public:
 		OptimizerEvolutionary();
-		/*
-		std::shared_ptr<IModule> module,
-			size_t converges = 0,
-			size_t grow_generation = 20,
-			double max_fitness = 0.0,
-			int min_generations = -1
-		*/
 		double update() override;
-		std::vector<Individual> getBests();
-		double evaluateFitness(const Individual& ind);
+		std::vector<Permutation> getBests();
+		double evaluateFitness(const Permutation& ind);
 	};
 }

+ 47 - 1
lib/src/OptimizerBase.cpp

@@ -100,7 +100,7 @@ namespace mdd{
 				if (key == "run")
 				{
 					updateLayout();
-					update();
+					_thread_ptr = std::make_shared<std::thread>(&OptimizerBase::update,this);
 				}
 				else if (key == "pause") {
 
@@ -158,4 +158,50 @@ namespace mdd{
 	void OptimizerBase::attachCallback(std::function<json(const json&)> callback) {
 		_callback = callback;
 	}
+
+	void OptimizerBase::callback_add_step(const std::vector<Permutation>& pers){
+		json ret;
+		ret["step"] = step;
+		for (auto& ind : pers)
+		{
+			json jind;
+			jind["fitness"] = ind.fitness;
+			jind["dna"] = ind.dna;
+			ret["permutations"].push_back(jind);
+		}
+		json jcall;
+		jcall["operation"] = "add";
+		jcall["args"]["subject"] = getIdentifier();
+		jcall["args"]["object"] = ret;
+		_callback(jcall);
+	}
+
+	void OptimizerBase::callback_evaluate_opt(const opt_state& state, const Permutation& per) {
+		json ret;
+		ret["step"] = step;
+		json jind;
+		jind["fitness"] = state.opt_value;
+		jind["dna"] = per.dna;
+		if (state.module_state == state::STATE_ERROR)
+		{
+			jind["status"] = "error";
+		}
+		else {
+			jind["status"] = "ok";
+		}
+		jind["processor"] = _module->dump();
+		ret["permutation"] = jind;
+		json jcall;
+		jcall["operation"] = "change";
+		jcall["args"]["subject"] = getIdentifier();
+		jcall["args"]["object"] = ret;
+		_callback(jcall);
+	}
+
+	OptimizerBase::~OptimizerBase() {
+		if (_thread_ptr != nullptr)
+		{
+			_thread_ptr->join();
+		}
+	}
 }

+ 34 - 84
lib/src/OptimizerEvolutionary.cpp

@@ -12,26 +12,7 @@ namespace mdd {
 				_inputs[j]->setValue() = it->dna[j];
 			}
 			auto opt = updateOutputs();
-			json ret;
-			ret["generation"] = generation;
-			json jind;
-			jind["fitness"] = it->fitness;
-			jind["dna"] = it->dna;
-			ret["individual"] = jind;
-			if (opt.module_state == state::STATE_ERROR)
-			{
-				ret["state"] = "error";
-			}
-			else {
-				ret["state"] = "ok";
-			}
-			ret["processor"] = _module->dump();
-			json jcall;
-			jcall["operation"] = "change";
-			jcall["args"]["subject"] = getIdentifier();
-			jcall["args"]["object"] = ret;
-			_callback(jcall);
-
+			callback_evaluate_opt(opt, *it);
 			if (opt.module_state == state::STATE_ERROR) {
 				_children.erase(it);
 			}
@@ -66,20 +47,20 @@ namespace mdd {
 			}
 		}
 		
-		json ret;
-		ret["generation"] = generation;
-		for (auto& ind : _bests)
-		{
-			json jind;
-			jind["fitness"] = ind.fitness;
-			jind["dna"] = ind.dna;
-			ret["bests"].push_back(jind);
-		}
-		json jcall;
-		jcall["operation"] = "add";
-		jcall["args"]["subject"] = getIdentifier();
-		jcall["args"]["object"] = ret;
-		_callback(jcall);
+		// json ret;
+		// ret["step"] = step;
+		// for (auto& ind : _bests)
+		// {
+		// 	json jind;
+		// 	jind["fitness"] = ind.fitness;
+		// 	jind["dna"] = ind.dna;
+		// 	ret["bests"].push_back(jind);
+		// }
+		// json jcall;
+		// jcall["operation"] = "add";
+		// jcall["args"]["subject"] = getIdentifier();
+		// jcall["args"]["object"] = ret;
+		// _callback(jcall);
 	}
 
 	OptimizerEvolutionary::OptimizerEvolutionary()
@@ -182,52 +163,34 @@ namespace mdd {
 
 	double OptimizerEvolutionary::update()
 	{
-		generation = -1;
+		step = -1;
 		bool check;
-		Individual old_best;
+		Permutation old_best;
 		size_t same_counter = 0;
 		if (_inputs.size() == 0)
 		{
 			std::cout << "ERROR: No optimizable inputs detected!" << std::endl;
 			return -1.0;
 		}
+		_children.clear();
+		_bests.clear();
 		do
 		{
-			std::cout << _children.size() << " | " << generation << std::endl;
+			++step;
+			std::cout << _children.size() << " | " << step << std::endl;
 			if (_children.empty())
 			{
 				for (size_t i = 0; i < _grow_generation*2; i++)
 				{
-					_children.push_back(generateIndividual());
+					_children.push_back(generatePermutation());
 				}
-				json ret;
-				ret["generation"] = generation;
-				for (auto& ind : _children)
-				{
-					json jind;
-					jind["fitness"] = ind.fitness;
-					jind["dna"] = ind.dna;
-					ret["individuals"] = jind;
-				}
-				json jcall;
-				jcall["operation"] = "add";
-				jcall["args"]["subject"] = getIdentifier();
-				jcall["args"]["object"] = ret;
-				_callback(jcall);
+				callback_add_step(_children);
 				evaluate();
 			}
 			else {
-				if (generation != -1)
-				{
-					evolve(_children);
-				}
-				else {
-					evaluate();
-				}
+				evolve(_children);
 			}
-			
- 			++generation;
-			check = generation < _min_generations || _bests[0].fitness > _max_fitness;
+			check = step < _min_generations || _bests[0].fitness > _max_fitness;
 			if (!check && _converges > 0)
 			{
 				bool found = false;
@@ -344,7 +307,7 @@ namespace mdd {
 		return input->getValue();//ERROR
 	}
 
-	void OptimizerEvolutionary::evolve(std::vector<Individual> parents)
+	void OptimizerEvolutionary::evolve(std::vector<Permutation> parents)
 	{
 		////fittest should breed more
 		//detect lower border
@@ -366,7 +329,7 @@ namespace mdd {
 			sum = 1.0;
 		}
 		//multiply fitter parents
-		std::vector<Individual> gen_pool = parents;
+		std::vector<Permutation> gen_pool = parents;
 		for (size_t i = 0; i < parents.size(); i++)
 		{
 			int additional = std::round((max - parents[i].fitness) / sum * 20);
@@ -387,27 +350,14 @@ namespace mdd {
 			int p2 = (int)random_num(0, gen_pool.size() - 1);
 			_children.push_back(combine(gen_pool[p1], gen_pool[p2]));
 		}
-		json ret;
-		ret["generation"] = generation;
-		for (auto& ind : _children)
-		{
-			json jind;
-			jind["fitness"] = ind.fitness;
-			jind["dna"] = ind.dna;
-			ret["individuals"] = jind;
-		}
-		json jcall;
-		jcall["operation"] = "add";
-		jcall["args"]["subject"] = getIdentifier();
-		jcall["args"]["object"] = ret;
-		_callback(jcall);
+		callback_add_step(_children);
 		evaluate(init_len);
 	}
 
-	OptimizerEvolutionary::Individual OptimizerEvolutionary::combine(Individual par1, Individual par2)
+	OptimizerEvolutionary::Permutation OptimizerEvolutionary::combine(Permutation par1, Permutation par2)
 	{
 		size_t len = par1.dna.size();
-		Individual child;
+		Permutation child;
 		for (size_t i = 0; i < len; i++)
 		{
 			double p = random_num(0.0, 100.0, 1.0);
@@ -426,9 +376,9 @@ namespace mdd {
 		return child;
 	}
 
-	OptimizerEvolutionary::Individual OptimizerEvolutionary::generateIndividual()
+	OptimizerEvolutionary::Permutation OptimizerEvolutionary::generatePermutation()
 	{
-		Individual ret;
+		Permutation ret;
 		for (size_t i = 0; i < _inputs.size(); i++)
 		{
 			ret.dna.push_back(mutateGene(_inputs[i]));
@@ -436,11 +386,11 @@ namespace mdd {
 		return ret;
 	}
 
-	std::vector<OptimizerEvolutionary::Individual> OptimizerEvolutionary::getBests() {
+	std::vector<OptimizerEvolutionary::Permutation> OptimizerEvolutionary::getBests() {
 		return _bests;
 	}
 
-	double OptimizerEvolutionary::evaluateFitness(const Individual& ind) {
+	double OptimizerEvolutionary::evaluateFitness(const Permutation& ind) {
 		for (size_t j = 0; j < _inputs.size(); j++)
 		{
 			_inputs[j]->setValue() = ind.dna[j];

+ 1 - 1
lib/test/test_Ansys.cpp

@@ -24,7 +24,7 @@ namespace TEST_ANSYS {
         return std::string(buffer).substr(0, pos);
     }
 
-    void evaluateIndividuum(OptimizerEvolutionary& optimizer, IModule::Ptr module_ptr, OptimizerEvolutionary::Individual& ind) {
+    void evaluateIndividuum(OptimizerEvolutionary& optimizer, IModule::Ptr module_ptr, OptimizerEvolutionary::Permutation& ind) {
         auto start = std::chrono::steady_clock::now();
         json j;
         j = ind.dna[0];