Willi Zschiebsch 4 år sedan
förälder
incheckning
d067c0ff65
3 ändrade filer med 73 tillägg och 2 borttagningar
  1. 38 0
      lib/include/OptimizerBase.h
  2. 1 1
      lib/src/OptimizerBase.cpp
  3. 34 1
      lib/src/OptimizerEvolutionary.cpp

+ 38 - 0
lib/include/OptimizerBase.h

@@ -45,6 +45,44 @@ namespace mdd {
 			bool operator== (const Permutation& ind) {
 				return (dna == ind.dna);
 			};
+
+			static bool same(Permutation const& a, Permutation const& b) {
+				bool check1 = (a.dna == b.dna);
+				bool check2 = true;
+				if (a.dna.size() != b.dna.size())
+				{
+					check2 = false;
+				}
+				else {
+					for (size_t i = 0; i < a.dna.size(); i++)
+					{
+						if (a.dna[i].size() != b.dna[i].size())
+						{
+							check2 = false;
+						}
+						else
+						{
+							for (size_t j = 0; j < a.dna[i].size(); j++)
+							{
+								if (a.dna[i][j] != b.dna[i][j])
+								{
+									check2 = false;
+								}
+							}
+						}
+						
+					}
+				}
+				if (check1 != check2)
+				{
+					int test = 0;
+				}
+				return check1;
+			}
+
+			static bool smaller(Permutation const& a, Permutation const& b) {
+				return (a.dna < b.dna);
+			}
 		};
 
 		bool connect(std::shared_ptr<IModule> module) override;

+ 1 - 1
lib/src/OptimizerBase.cpp

@@ -176,7 +176,7 @@ namespace mdd{
 		_callback(jcall);
 	}
 
-	void OptimizerBase::callback_evaluate_opt(const opt_state& state, const Permutation& per) {
+	void OptimizerBase::callback_evaluate_opt(const OptimizerBase::opt_state& state, const Permutation& per) {
 		json ret;
 		ret["step"] = step;
 		json jind;

+ 34 - 1
lib/src/OptimizerEvolutionary.cpp

@@ -3,6 +3,37 @@
 namespace mdd {
 	void OptimizerEvolutionary::evaluate(size_t ignore_len)
 	{
+		//clear duplicates
+		//std::sort(_children.begin(), _children.end(), Permutation::smaller);
+		//_children.erase(std::unique(_children.begin(), _children.end(), Permutation::same), _children.end());
+		std::vector<Permutation> save = _children;
+		_children.clear();
+		for (size_t i = 0; i < save.size(); i++)
+		{
+			bool found = false;
+			for (size_t j = 0; j < _children.size(); j++)
+			{
+				if (save[i] == _children[j])
+				{
+					found = true;
+					break;
+				}
+			}
+			if (!found)
+			{
+				_children.push_back(save[i]);
+			}
+		}
+
+		//
+		opt_state def_state;
+		def_state.module_state = state::UNCHANGED;
+		for (int i = 0; i < ignore_len; ++i)
+		{
+			def_state.opt_value = _children[i].fitness;
+			callback_evaluate_opt(def_state, _children[i]);
+		}
+
 		//calculate fitness
 		for (auto it = _children.begin() + ignore_len; it != _children.end(); ++it)
 		{
@@ -11,7 +42,7 @@ namespace mdd {
 				//std::cout << "set: " << j << ": " << it->dna[j] << std::endl;
 				_inputs[j]->setValue() = it->dna[j];
 			}
-			auto opt = updateOutputs();
+			opt_state opt = updateOutputs();
 			callback_evaluate_opt(opt, *it);
 			if (opt.module_state == state::STATE_ERROR) {
 				_children.erase(it);
@@ -22,6 +53,8 @@ namespace mdd {
 		}
 		//find fitest
 		double min = _children[0].fitness;
+		_bests.clear();
+		_bests.push_back(_children[0]);
 		for (size_t i = 1; i < _children.size(); i++)
 		{
 			if (round(min/_precision)*_precision > round(_children[i].fitness / _precision) * _precision) {