Przeglądaj źródła

first evolu test is working

Willi Zschiebsch 5 lat temu
rodzic
commit
f256d0b438

+ 10 - 2
lib/include/OptimizerEvolutionary.h

@@ -14,7 +14,10 @@ namespace mdd {
 		struct Individual {
 		public:
 			json dna;
-			double fitness;
+			double fitness = 0;
+			bool operator== (const Individual& ind) {
+				return (dna == ind.dna) && (fitness == ind.fitness);
+			};
 		};
 
 		Individual generateIndividual();
@@ -25,13 +28,18 @@ namespace mdd {
 		int _min_generations;
 		double _max_fitness;
 		size_t _grow_generation;
+		size_t _converges;
 
 		void evolve(std::vector<Individual> parents);
 		void evaluate(size_t ignore_len = 0);
 		json mutateGene(json limit, json seed = json());
 
 	public:
-		OptimizerEvolutionary(std::shared_ptr<IModule> module, size_t grow_generation = 20, double max_fitness = 0.0, int min_generations = -1);
+		OptimizerEvolutionary(std::shared_ptr<IModule> module,
+			size_t converges = 0,
+			size_t grow_generation = 20,
+			double max_fitness = 0.0,
+			int min_generations = -1);
 		json update() override;
 	};
 }

+ 4 - 5
lib/src/OptimizerBase.cpp

@@ -11,10 +11,10 @@ namespace mdd{
 			for (size_t i = 0; i < _output_vals.size(); ++i)
 			{
 				_output_vals[i] = _outputs[i]->getValue()["value"][0].get<double>();
-				std::cout << "get: " << i << ": " << _output_vals[i] << std::endl;
+				//std::cout << "get: " << i << ": " << _output_vals[i] << std::endl;
 			}
 			ret.opt_value = _func_expr.value();
-			std::cout << "get: " << "opt" << ": " << ret.opt_value << std::endl;
+			//std::cout << "get: " << "opt" << ": " << ret.opt_value << std::endl;
 		}
 		
 		return ret;
@@ -92,11 +92,10 @@ namespace mdd{
 		}
 		symbol_table.add_constants();
 	
-		exprtk::expression<double> expression;
-		expression.register_symbol_table(symbol_table);
+		_func_expr.register_symbol_table(symbol_table);
 	
 		exprtk::parser<double> parser;
-		return parser.compile(func, expression);
+		return parser.compile(func, _func_expr);
 	}
 	
 }

+ 32 - 8
lib/src/OptimizerEvolutionary.cpp

@@ -9,7 +9,7 @@ namespace mdd {
 		{
 			for (size_t j = 0; j < _inputs.size(); j++)
 			{
-				std::cout << "set: " << j << ": " << it->dna[j] << std::endl;
+				//std::cout << "set: " << j << ": " << it->dna[j] << std::endl;
 				_inputs[j]->setDefaultValue() = it->dna[j];
 			}
 			auto opt = updateReader();
@@ -21,25 +21,29 @@ namespace mdd {
 			}
 		}
 		//find fitest
-		double max = _children[0].fitness;
+		double min = _children[0].fitness;
 		for (size_t i = 1; i < _children.size(); i++)
 		{
-			if (max < _children[i].fitness) {
-				max = _children[i].fitness;
+			if (min > _children[i].fitness) {
+				min = _children[i].fitness;
 				_best = _children[i];
 			}
 		}
 	}
-	OptimizerEvolutionary::OptimizerEvolutionary(std::shared_ptr<IModule> module, size_t grow_generation, double max_fitness, int min_generations)
+	OptimizerEvolutionary::OptimizerEvolutionary(std::shared_ptr<IModule> module, size_t converges, size_t grow_generation, double max_fitness, int min_generations)
 	{
 		_module = module;
 		_grow_generation = grow_generation;
 		_min_generations = min_generations;
 		_max_fitness = max_fitness;
+		_converges = converges;
 	}
 	json OptimizerEvolutionary::update()
 	{
-		size_t gen = -1;
+		int gen = -1;
+		bool check;
+		Individual old_best;
+		size_t same_counter = 0;
 		do
 		{
 			if (_children.empty())
@@ -53,8 +57,26 @@ namespace mdd {
 			else {
 				evolve(_children);
 			}
+			//std::cout << _children.size() << " | " << gen << std::endl;
 			++gen;
-		} while (gen < _min_generations || _best.fitness > _max_fitness);
+			check = gen < _min_generations || _best.fitness > _max_fitness;
+			if (!check && _converges > 0)
+			{
+				if (_best == old_best)
+				{
+					++same_counter;
+				}
+				else
+				{
+					old_best = _best;
+					same_counter = 0;
+				}
+				if (same_counter < _converges)
+				{
+					check = true;
+				}
+			}
+		} while (check);
 		
 		return _best.dna;
 	}
@@ -164,7 +186,9 @@ namespace mdd {
 		std::vector<Individual> gen_pool = parents;
 		for (size_t i = 0; i < parents.size(); i++)
 		{
-			for (size_t j = 1; j < std::round((max - parents[i].fitness)/sum*20); i++)
+			int additional = std::round((max - parents[i].fitness) / sum * 20);
+			//std::cout << additional << std::endl;
+			for (size_t j = 1; j < additional; j++)
 			{
 				gen_pool.push_back(parents[i]);
 			}

+ 3 - 1
lib/test/test_ModuleHTTP.cpp

@@ -158,6 +158,7 @@ TEST(ModuleHTTP, update_intern){
     EXPECT_EQ(module.getOutput(outputs_ids[2])->getValue()["value"].get<int>(), 1);
 }
 
+/*
 TEST(ModuleHTTP, test_ansys_server) {
     ModuleHTTP module("../../../lib/test/server/server-ansys.py", "localhost", 8888);
 
@@ -175,4 +176,5 @@ TEST(ModuleHTTP, test_ansys_server) {
     }
     Client cli("localhost", 8888);
     cli.Get("/stop");
-}
+}
+*/

+ 3 - 3
lib/test/test_OptimizerEvolutionary.cpp

@@ -11,7 +11,7 @@ TEST(OptimizerEvolutionary, OptimizeSimpleFormula) {
     //optimize f(x)=x^2
     std::vector<std::string> inputs;
     std::shared_ptr<ModuleMath> f1 = std::make_shared<ModuleMath>(MathOperation::MULTIPLY);
-    OptimizerEvolutionary optimizer(f1);
+    OptimizerEvolutionary optimizer(f1, 3);
     inputs = f1->getInputIDs();
     json limit;
     limit["min"] = { -10 };
@@ -22,6 +22,6 @@ TEST(OptimizerEvolutionary, OptimizeSimpleFormula) {
     optimizer.addReader(f1->getOutputIDs()[0]);
     optimizer.setEvaluation("out0");
     json res = optimizer.update();
-    std::cout << res.dump() << std::endl;
-    EXPECT_EQ(1,2);
+    //std::cout << res.dump() << std::endl;
+    EXPECT_EQ(res[0]["value"][0].get<int>()* res[1]["value"][0].get<int>(),-100);
 }