Willi Zschiebsch 4 роки тому
батько
коміт
faed6976b7

+ 2 - 4
lib/include/ModuleBase.h

@@ -1,5 +1,4 @@
-#ifndef MDD_ModuleBase_H
-#define MDD_ModuleBase_H
+#pragma once
 
 #include "Input.h"
 #include "Output.h"
@@ -53,5 +52,4 @@ namespace mdd {
 
         void disconnect() override;
     };
-}
-#endif //MDD_BASEMODULE_H
+}

+ 4 - 5
lib/include/OptimizerBase.h

@@ -1,5 +1,4 @@
-#ifndef OPTIMIZERBASE_H
-#define OPTIMIZERBASE_H
+#pragma once
 #include "IOptimizer.h"
 #include "exprtk.hpp"
 
@@ -27,7 +26,8 @@ namespace mdd {
 			double opt_value = 0;
 		};
 		opt_state updateOutputs();
-		OptimizerBase(const std::string& base_config);
+		OptimizerBase(const std::string& base_config = "{}");
+
 		bool configureChild(const json& config) override;
 
 	public:
@@ -43,5 +43,4 @@ namespace mdd {
 		json getIdentifier() override;
 		//state update() override;
 	};
-}
-#endif
+}

+ 7 - 1
lib/src/Connector.cpp

@@ -247,6 +247,11 @@ namespace mdd {
 			jargs["subject"] = out_ptr->getIdentifier();
 			jargs["object"] = *jobj;
 		}
+		else if (*jsub_typ == "optimizer") {
+			_opt->configure(*jobj);
+			jargs["subject"] = _opt->getIdentifier();
+			jargs["object"] = *jobj;
+		}
 		else {
 			json j;
 			return j;
@@ -379,6 +384,7 @@ namespace mdd {
 				if (jopt->contains("key"))
 				{
 					_opt = regi.generateOptimizer((*jopt)["key"].get<std::string>());
+					_opt->connect(_root);
 					_opt->load((*jopt));
 				}
 			}
@@ -390,8 +396,8 @@ namespace mdd {
 			jconfig["GUI"]["name"]["value"]	= "root";
 			_root->configure(jconfig);
 			_opt = regi.generateOptimizer("OptimizerEvolutionary");
+			_opt->connect(_root);
 		}
-		_opt->connect(_root);
 		
 		json ret;
 		ret["operation"] = "state";

+ 54 - 6
lib/src/Input.cpp

@@ -214,9 +214,20 @@ namespace mdd{
             auto jit = jit_limit->find("min");
             if (jit != jit_limit->end())
             {
-                json jval = jit.value();
+                auto jit_val = jit->find("value");
+                json jval;
+                if (jit_val != jit->end())
+                {
+                    jval = jit_val.value();
+                }
+                else
+                {
+                    jval = jit.value();
+                }
+               
                 if (jval.is_string()) {
-                    jval = json::parse(jval.get<std::string>());
+                    std::string jtest = jval.get<std::string>();
+                    jval = json::parse(jtest);
                 }
                 _limit->min = jval.get<std::vector<double>>();
                 success = true;
@@ -225,7 +236,16 @@ namespace mdd{
             jit = jit_limit->find("max");
             if (jit != jit_limit->end())
             {
-                json jval = jit.value();
+                auto jit_val = jit->find("value");
+                json jval;
+                if (jit_val != jit->end())
+                {
+                    jval = jit_val.value();
+                }
+                else
+                {
+                    jval = jit.value();
+                }
                 if (jval.is_string()) {
                     jval = json::parse(jval.get<std::string>());
                 }
@@ -236,7 +256,16 @@ namespace mdd{
             jit = jit_limit->find("step");
             if (jit != jit_limit->end())
             {
-                json jval = jit.value();
+                auto jit_val = jit->find("value");
+                json jval;
+                if (jit_val != jit->end())
+                {
+                    jval = jit_val.value();
+                }
+                else
+                {
+                    jval = jit.value();
+                }
                 if (jval.is_string()) {
                     jval = json::parse(jval.get<std::string>());
                 }
@@ -247,14 +276,33 @@ namespace mdd{
             jit = jit_limit->find("rule");
             if (jit != jit_limit->end())
             {
-                _limit->rule = jit.value().get<std::string>();
+                auto jit_val = jit->find("value");
+                json jval;
+                if (jit_val != jit->end())
+                {
+                    jval = jit_val.value();
+                }
+                else
+                {
+                    jval = jit.value();
+                }
+                _limit->rule = jval.get<std::string>();
                 success = true;
             }
 
             jit = jit_limit->find("elements");
             if (jit != jit_limit->end())
             {
-                json jval = jit.value();
+                auto jit_val = jit->find("value");
+                json jval;
+                if (jit_val != jit->end())
+                {
+                    jval = jit_val.value();
+                }
+                else
+                {
+                    jval = jit.value();
+                }
                 if (jval.is_string()) {
                     jval = json::parse(jval.get<std::string>());
                 }

+ 1 - 1
lib/src/ModuleBase.cpp

@@ -198,7 +198,7 @@ namespace mdd {
             for (size_t i = 0; i < jout.size(); i++)
             {
                 outputs[i]->setParent(shared_from_this());
-                inputs[i]->configure(jout[i]);
+                outputs[i]->configure(jout[i]);
             }
         }
     }

+ 48 - 14
lib/src/OptimizerBase.cpp

@@ -34,12 +34,29 @@ namespace mdd{
 		else {
 			_base_config["configure"] = jparse;
 		}
+		
+		json jstate;
+		jstate["value"] = "stop";
+		jstate["options"].push_back("run");
+		jstate["options"].push_back("pause");
+		jstate["options"].push_back("stop");
+
+		json jevalue;
+		jevalue["value"] = "";
+
+		json jsub;
+		jsub["state"] = jstate;
+		jsub["evaluate"] = jevalue;
+		_base_config["optimizer"] = jsub;
 	}
 
 	void OptimizerBase::updateLayout()
 	{
-		_inputs = _module->getOptimizableInputs();
-		_outputs = _module->getOptimizableOutputs();
+		if (_module != nullptr)
+		{
+			_inputs = _module->getOptimizableInputs();
+			_outputs = _module->getOptimizableOutputs();
+		}
 		_output_vals.clear();
 		if (_inputs.empty())
 		{
@@ -73,7 +90,34 @@ namespace mdd{
 	
 	bool OptimizerBase::configure(const json& config) {
 		json jconfig = config;
-		auto jit = jconfig.find("configure");
+		auto jit = jconfig.find("optimizer");
+		if (jit != jconfig.end())
+		{
+			auto jstate_it = jit->find("state");
+			if (jstate_it != jit->end())
+			{
+				std::string key = (*jstate_it)["value"].get<std::string>();
+				if (key == "run")
+				{
+					updateLayout();
+					update();
+				}
+				else if (key == "pause") {
+
+				}
+				else {//stop
+
+				}
+			}
+			jstate_it = jit->find("evaluate");
+			if (jstate_it != jit->end()) {
+				updateLayout();
+				setEvaluation((*jstate_it)["value"].get<std::string>());
+				_base_config["optimizer"]["evaluate"]["value"] = (*jstate_it)["value"].get<std::string>();
+			}
+
+		}
+		jit = jconfig.find("configure");
 		if (jit != jconfig.end())
 		{
 			return configureChild(jit.value());
@@ -90,17 +134,7 @@ namespace mdd{
 	}
 	//virtual std::string getGeneratorID() = 0;
 	void OptimizerBase::load(const json& j) {
-		auto j_ptr = j.find("ID");
-		if (j_ptr != j.end())
-		{
-
-		}
-
-		j_ptr = j.find("configure");
-		if (j_ptr != j.end())
-		{
-			configure(*j_ptr);
-		}
+		configure(j);
 	}
 	json OptimizerBase::dump() {
 		json ret = _base_config;

+ 41 - 5
lib/src/OptimizerEvolutionary.cpp

@@ -46,6 +46,7 @@ namespace mdd {
 			}
 		}
 	}
+
 	OptimizerEvolutionary::OptimizerEvolutionary()
 		:OptimizerBase(R"JSON(
         {
@@ -84,31 +85,61 @@ namespace mdd {
 		auto jit = config.find("converges");
 		if (jit != config.end())
 		{
-			_converges = jit.value()["value"].get<int>();
+			json jval = jit.value()["value"];
+			if (jval.is_string())
+			{
+				jval = json::parse(jval.get<std::string>());
+			}
+			_converges = jval.get<int>();
+			_base_config["configure"]["converges"]["value"] = _converges;
 			found = true;
 		}
 		jit = config.find("grow generation");
 		if (jit != config.end())
 		{
-			_grow_generation = jit.value()["value"].get<int>();
+			json jval = jit.value()["value"];
+			if (jval.is_string())
+			{
+				jval = json::parse(jval.get<std::string>());
+			}
+			_grow_generation = jval.get<int>();
+			_base_config["configure"]["grow generation"]["value"] = _grow_generation;
 			found = true;
 		}
 		jit = config.find("max fitness");
 		if (jit != config.end())
 		{
-			_max_fitness = jit.value()["value"].get<int>();
+			json jval = jit.value()["value"];
+			if (jval.is_string())
+			{
+				jval = json::parse(jval.get<std::string>());
+			}
+			_max_fitness = jval.get<int>();
+			_base_config["configure"]["max fitness"]["value"] = _max_fitness;
 			found = true;
 		}
 		jit = config.find("min generations");
 		if (jit != config.end())
 		{
-			_min_generations = jit.value()["value"].get<int>();
+			json jval = jit.value()["value"];
+			if (jval.is_string())
+			{
+				jval = json::parse(jval.get<std::string>());
+			}
+			_min_generations = jval.get<int>();
+			_base_config["configure"]["min generations"]["value"] = _min_generations;
 			found = true;
 		}
 		jit = config.find("precision");
 		if (jit != config.end())
 		{
-			_precision = jit.value()["value"].get<double>();
+			json jval = jit.value()["value"];
+			if (jval.is_string())
+			{
+				jval = json::parse(jval.get<std::string>());
+			}
+			_precision = jval.get<double>();
+			_base_config["configure"]["precision"]["value"] = _precision;
 			found = true;
 		}
 		return found;
@@ -177,6 +208,7 @@ namespace mdd {
 		
 		return _bests[0].fitness;
 	}
+
 	std::vector<double> OptimizerEvolutionary::mutateGene(std::shared_ptr<IInput> input, std::vector<double> seed)
 	{
 	
@@ -308,6 +340,7 @@ namespace mdd {
 		}
 		evaluate(init_len);
 	}
+
 	OptimizerEvolutionary::Individual OptimizerEvolutionary::combine(Individual par1, Individual par2)
 	{
 		size_t len = par1.dna.size();
@@ -329,6 +362,7 @@ namespace mdd {
 		}
 		return child;
 	}
+
 	OptimizerEvolutionary::Individual OptimizerEvolutionary::generateIndividual()
 	{
 		Individual ret;
@@ -338,9 +372,11 @@ namespace mdd {
 		}
 		return ret;
 	}
+
 	std::vector<OptimizerEvolutionary::Individual> OptimizerEvolutionary::getBests() {
 		return _bests;
 	}
+
 	double OptimizerEvolutionary::evaluateFitness(const Individual& ind) {
 		for (size_t j = 0; j < _inputs.size(); j++)
 		{

+ 16 - 2
lib/src/ProcessorBase.cpp

@@ -85,7 +85,7 @@ namespace mdd{
             sucess = true;
         }
 
-        jit = jconfig.find("configuration");
+        jit = jconfig.find("configure");
         if (jit != jconfig.end()) {
             sucess = configureChild(jit.value());
         }
@@ -176,10 +176,12 @@ namespace mdd{
         ret["args"] = jargs;
         return ret;
     }
+
     json ProcessorBase::removeInput(std::shared_ptr<IInput> input) {
         json ret;
         return ret;
     }
+
     json ProcessorBase::addOutput(std::shared_ptr<IOutput> output) {
         auto param_ptr = std::make_shared<Parameter>();
         param_ptr->setName(output->getName());
@@ -196,6 +198,7 @@ namespace mdd{
         ret["args"] = jargs;
         return ret;
     }
+
     json ProcessorBase::removeOutput(std::shared_ptr<IOutput> output) {
         json ret;
         return ret;
@@ -216,6 +219,7 @@ namespace mdd{
     size_t ProcessorBase::getNumOutputs() {
         return processor_outputs.size() + outputs.size();
     }
+
     std::vector< std::shared_ptr<IModule >>& ProcessorBase::getModules(){
         return modules;
     }
@@ -305,6 +309,7 @@ namespace mdd{
     }
 
     void ProcessorBase::load(const json& j) {
+        disconnect();
         auto jit = j.find("ID");
         if (jit != j.end())
         {
@@ -325,7 +330,16 @@ namespace mdd{
         jit = j.find("configure");
         if (jit != j.end())
         {
-
+            json jcon;
+            jcon["configure"] = *jit;
+            configure(jcon);
+        }
+        jit = j.find("GUI");
+        if (jit != j.end())
+        {
+            json jgui;
+            jgui["GUI"] = *jit;
+            configure(jgui);
         }
         jit = j.find("modules");
         if (jit != j.end())

+ 1 - 1
lib/src/ProcessorStandard.cpp

@@ -57,7 +57,7 @@ namespace mdd {
             {
                 found = false;
             }
-            _base_config["configure"]["priority"]["value"] = _priorityEvaluation;
+            _base_config["configure"]["priority"]["value"] = op;
         }
 
         jit = config.find("iterations");