Browse Source

before changing ID System

Willi Zschiebsch 4 years ago
parent
commit
1484442338

+ 1 - 0
lib/include/Connector.h

@@ -21,6 +21,7 @@ namespace mdd {
 	private:
 		Registration regi = Registration();
 		IProcessor::Ptr _root;
+		IOptimizer::Ptr _opt;
 		const std::map<const std::string, std::function<json(const json&)>> _ops = {//const std::map<const std::string, const json& (Connector::*)(const json&)> _ops = {
 			{"add", std::bind(&Connector::add,this,std::placeholders::_1) },
 			{"change", std::bind(&Connector::change,this,std::placeholders::_1)},

+ 2 - 0
lib/include/OptimizerBase.h

@@ -14,6 +14,8 @@ namespace mdd {
 	{
 	protected:
 		json _base_config;
+		std::string key;
+		const std::string type = "optimizer";
 		std::shared_ptr<IModule> _module;
 		std::vector<std::shared_ptr<IInput>> _inputs;
 		std::vector<std::shared_ptr<IOutput>> _outputs;

+ 1 - 1
lib/include/ProcessorBase.h

@@ -21,7 +21,7 @@ namespace mdd {
     protected:
         json _base_config;
         ProcessorBase(const std::string& base_config);
-        std::string type = "processor";
+        const std::string type = "processor";
         std::string key;
         std::vector<std::shared_ptr<Input>>processor_inputs;
         std::vector<std::shared_ptr<Output>> processor_outputs;

+ 64 - 23
lib/src/Connector.cpp

@@ -38,7 +38,7 @@ namespace mdd {
 		}
 		else if (*jobj_typ == "module")
 		{
-			_root->addModule(Registration().generateModule((*jobj_typ)["key"].get<std::string>()));
+			_root->addModule(Registration().generateModule((*jobj)["key"].get<std::string>()));
 			jargs["object"] = _root->getModules().back()->dump();
 		}
 		else if(*jobj_typ == "connection")
@@ -194,7 +194,19 @@ namespace mdd {
 		}
 		else if (*jsub_typ == "output")
 		{
-
+			IModule::Ptr module_ptr = _root->getModule(*jsub);
+			if (module_ptr == nullptr) {
+				std::cout << "[Connector]: change: Was not able to find module." << std::endl;
+				return ret;
+			}
+			IOutput::Ptr out_ptr = module_ptr->getOutput(*jsub);
+			if (out_ptr == nullptr) {
+				std::cout << "[Connector]: change: Was not able to find output." << std::endl;
+				return ret;
+			}
+			out_ptr->configure(*jobj);
+			jargs["subject"] = out_ptr->getIdentifier();
+			jargs["object"] = *jobj;
 		}
 		else {
 			json j;
@@ -250,25 +262,39 @@ namespace mdd {
 		if (jtype != args.end())
 		{
 			found = true;
-			auto module_ptr = _root->getModule(args);
-			if (*jtype == "processor")
+			if (*jtype == "optimizer")
 			{
-				if (IProcessor* processor_ptr = dynamic_cast<IProcessor*>(&(*module_ptr)))
-				{
-					ret["info"] = processor_ptr->dump();
-				}
-			}
-			else if (*jtype == "module")
-			{
-				ret["info"] = module_ptr->dump();
+				ret["info"] = _opt->getConfiguration();
+				//ret["info"]["ID"] = _opt->getIdentifier();
 			}
-			else if (*jtype == "input")
-			{
-				ret["info"] = module_ptr->getInput(args)->dump();
-			}
-			else if (*jtype == "output")
+			else
 			{
-				ret["info"] = module_ptr->getOutput(args)->dump();
+				auto module_ptr = _root->getModule(args);
+				if (*jtype == "processor")
+				{
+					if (IProcessor* processor_ptr = dynamic_cast<IProcessor*>(&(*module_ptr)))
+					{
+						ret["info"] = processor_ptr->getConfiguration();
+						//ret["info"]["ID"] = processor_ptr->getIdentifier();
+					}
+				}
+				else if (*jtype == "module")
+				{
+					ret["info"] = module_ptr->getConfiguration();
+					//ret["info"]["ID"] = module_ptr->getIdentifier();
+				}
+				else if (*jtype == "input")
+				{
+					auto ptr = module_ptr->getInput(args);
+					ret["info"] = ptr->getConfiguration();
+					//ret["info"]["ID"] = ptr->getIdentifier();
+				}
+				else if (*jtype == "output")
+				{
+					auto ptr = module_ptr->getOutput(args);
+					ret["info"] = ptr->getConfiguration();
+					//ret["info"]["ID"] = ptr->getIdentifier();
+				}
 			}
 		}
 
@@ -287,7 +313,8 @@ namespace mdd {
 	json Connector::save(const json& args) {
 		std::ofstream out("save.json");
 		json jfile;
-		jfile = _root->dump();
+		jfile["processor"] = _root->dump();
+		jfile["optimizer"] = _opt->dump();
 		out << std::setw(4) << jfile << std::endl;
 		json ret;
 		ret["operation"] = "save";
@@ -299,18 +326,32 @@ namespace mdd {
 			std::ifstream in("save.json");
 			json jfile;
 			in >> jfile;
-			if (jfile.contains("key"))
+			auto jproc = jfile.find("processor");
+			if (jproc != jfile.end())
 			{
-				_root = regi.generateProcessor(jfile["key"].get<std::string>());
-				_root->load(jfile);
+				if (jproc->contains("key"))
+				{
+					_root = regi.generateProcessor((*jproc)["key"].get<std::string>());
+					_root->load((*jproc));
+				}
+			}
+			auto jopt = jfile.find("optimizer");
+			if (jopt != jfile.end()) {
+				if (jopt->contains("key"))
+				{
+					_opt = regi.generateOptimizer((*jopt)["key"].get<std::string>());
+					_opt->load((*jopt));
+				}
 			}
 		}
 		else
 		{
 			_root = regi.generateProcessor("ProcessorStandard");
 			_root->setName("root");
+
+			_opt = regi.generateOptimizer("OptimizerEvolutionary");
 		}
-		
+		_opt->connect(_root);
 		
 		json ret;
 		ret["operation"] = "state";

+ 84 - 6
lib/src/Input.cpp

@@ -148,8 +148,14 @@ namespace mdd{
 
     bool Input::configure(const json& config)
     {
-        json jconfig = config;
+        json jparse = config;
         bool success = false;
+        auto jit_config = jparse.find("configure");
+        json jconfig = jparse;
+        if (jit_config != jparse.end())
+        {
+            jconfig = *jit_config;
+        }
         auto jit = jconfig.find("name");
         if (jit != jconfig.end())
         {
@@ -167,7 +173,19 @@ namespace mdd{
         jit = jconfig.find("optimizable");
         if (jit != jconfig.end())
         {
-            setOptimizability(jit.value().get<bool>());
+            auto jopt = jit->find("value");
+            if (jopt != jit->end())
+            {
+                json jval = jopt.value();
+                if (jval.is_string()) {
+                    jval = json::parse(jval.get<std::string>());
+                }
+                setOptimizability(jval.get<bool>());
+            }
+            else
+            {
+                setOptimizability(jit.value().get<bool>());
+            }
             success = true;
         }
 
@@ -181,15 +199,73 @@ namespace mdd{
             _value = jval.get<std::vector<double>>();
             success = true;
         }
+
+        auto jit_limit = jparse.find("limit");
+        if (jit_limit != jparse.end())
+        {
+            auto jit = jit_limit->find("min");
+            if (jit != jit_limit->end())
+            {
+                json jval = jit.value();
+                if (jval.is_string()) {
+                    jval = json::parse(jval.get<std::string>());
+                }
+                _limit->min = jval.get<std::vector<double>>();
+                success = true;
+            }
+
+            jit = jit_limit->find("max");
+            if (jit != jit_limit->end())
+            {
+                json jval = jit.value();
+                if (jval.is_string()) {
+                    jval = json::parse(jval.get<std::string>());
+                }
+                _limit->max = jval.get<std::vector<double>>();
+                success = true;
+            }
+
+            jit = jit_limit->find("step");
+            if (jit != jit_limit->end())
+            {
+                json jval = jit.value();
+                if (jval.is_string()) {
+                    jval = json::parse(jval.get<std::string>());
+                }
+                _limit->step = jval.get<std::vector<double>>();
+                success = true;
+            }
+
+            jit = jit_limit->find("rule");
+            if (jit != jit_limit->end())
+            {
+                _limit->rule = jit.value().get<std::string>();
+                success = true;
+            }
+
+            jit = jit_limit->find("elements");
+            if (jit != jit_limit->end())
+            {
+                json jval = jit.value();
+                if (jval.is_string()) {
+                    jval = json::parse(jval.get<std::string>());
+                }
+                _limit->elements = jval.get<std::vector<std::vector<double>>>();
+                success = true;
+            }
+        }
         return success;
     }
 
     const json& Input::getConfiguration()
     {
-        _base_config["value"] = _value;
-        _base_config["optimizable"] = _optimizable;
-        _base_config["type"] = "input";
-        _base_config["key"] = "Input";
+        json jconfig;
+        jconfig["name"] = _name;
+        jconfig["optimizable"]["value"] = _optimizable;
+        jconfig["optimizable"]["options"] = { true, false };
+        jconfig["value"] = _value;
+       
+        _base_config["configure"] = jconfig;
         if (_optimizable == true && _limit != nullptr)
         {
             json jlimit;
@@ -213,6 +289,8 @@ namespace mdd{
     {
         json jdump = getConfiguration();
         jdump["ID"] = getIdentifier();
+        jdump["type"] = "input";
+        jdump["key"] = "Input";
         return jdump;
     }
 

+ 24 - 10
lib/src/OptimizerBase.cpp

@@ -26,7 +26,14 @@ namespace mdd{
 	}
 
 	OptimizerBase::OptimizerBase(const std::string& base_config) {
-		_base_config = base_config;
+		json jparse = json::parse(base_config);
+		if (jparse.contains("configure"))
+		{
+			_base_config = base_config;
+		}
+		else {
+			_base_config["configure"] = jparse;
+		}
 	}
 
 	void OptimizerBase::updateLayout()
@@ -83,16 +90,23 @@ namespace mdd{
 	}
 	//virtual std::string getGeneratorID() = 0;
 	void OptimizerBase::load(const json& j) {
-		if (j.contains("configure"))
+		auto j_ptr = j.find("ID");
+		if (j_ptr != j.end())
 		{
-			configure(j["configure"].get<std::string>());
+
+		}
+
+		j_ptr = j.find("configure");
+		if (j_ptr != j.end())
+		{
+			configure(*j_ptr);
 		}
 	}
 	json OptimizerBase::dump() {
-		json ret;
-
-		ret["configure"] = _base_config;
-
+		json ret = _base_config;
+		ret["ID"] = getIdentifier();
+		ret["type"] = type;
+		ret["key"] = key;
 		return ret;
 	}
 
@@ -100,10 +114,10 @@ namespace mdd{
 		json jID;
 		/*
 		jID["name"] = _name;
-		jID["id"] = getAppendix();
+		jID["id"] = getAppendix();*/
 		jID["type"] = type;
-		jID["key"] = key;*/
-		jID["prefix"] = std::vector<std::string>();
+		jID["key"] = key;
+		//jID["prefix"] = std::vector<std::string>();
 		return jID;
 	}
 }

+ 22 - 16
lib/src/OptimizerEvolutionary.cpp

@@ -48,24 +48,30 @@ namespace mdd {
 	}
 	OptimizerEvolutionary::OptimizerEvolutionary()
 		:OptimizerBase(R"JSON(
-        [{
-            "name":"converges",
-            "value": 0
-        },{
-            "name":"grow generation",
+        {
+            "converges":
+			{
+				"value": 0
+			},
+            "grow generation":
+			{
             "value": 20
-        },{
-            "name":"max fitness",
-            "value": 0.0
-        },{
-            "name":"min generations",
-            "value": -1
-        },{
-            "name":"precission",
-            "value": 0.001
-        }])JSON")
+			},
+            "max fitness":
+			{
+				"value": 0.0
+			},
+            "min generations": 
+			{
+				"value": -1
+			},
+            "precission":
+			{
+				"value": 0.001
+			}
+        })JSON")
 	{
-		//_module = module;
+		key = "OptimizerEvolutionary";
 		_converges = 0;
 		_grow_generation = 20;
 		_max_fitness = 0.0;

+ 33 - 7
lib/src/Output.cpp

@@ -150,8 +150,14 @@ namespace mdd {
 
     bool Output::configure(const json& config)
     {
-        json jconfig = config;
+        json jparse = config;
         bool success = false;
+        auto jit_config = jparse.find("configure");
+        json jconfig = jparse;
+        if (jit_config != jparse.end())
+        {
+            jconfig = *jit_config;
+        }
         auto jit = jconfig.find("name");
         if (jit != jconfig.end())
         {
@@ -169,14 +175,30 @@ namespace mdd {
         jit = jconfig.find("optimizable");
         if (jit != jconfig.end())
         {
-            _optimizable = jit.value().get < bool >();
+            auto jopt = jit->find("value");
+            if (jopt != jit->end())
+            {
+                json jval = jopt.value();
+                if (jval.is_string()) {
+                    jval = json::parse(jval.get<std::string>());
+                }
+                setOptimizability(jval.get<bool>());
+            }
+            else
+            {
+                setOptimizability(jit.value().get<bool>());
+            }
             success = true;
         }
 
         jit = jconfig.find("value");
         if (jit != jconfig.end())
         {
-            _value = jit.value().get <std::vector<double> >();
+            json jval = jit.value();
+            if (jval.is_string()) {
+                jval = json::parse(jval.get<std::string>());
+            }
+            _value = jval.get<std::vector<double>>();
             success = true;
         }
         return success;
@@ -184,10 +206,12 @@ namespace mdd {
 
     const json& Output::getConfiguration()
     {
-        _base_config["optimizable"] = _optimizable;
-        _base_config["value"] = _value;
-        _base_config["type"] = "output";
-        _base_config["key"] = "Output";
+        json jconfig;
+        jconfig["name"] = _name;
+        jconfig["optimizable"]["value"]= _optimizable;
+        jconfig["optimizable"]["options"] = {true, false};
+        jconfig["value"] = _value;
+        _base_config["configure"] = jconfig;
         return _base_config;
     }
 
@@ -201,6 +225,8 @@ namespace mdd {
     {
         json jdump = getConfiguration();
         jdump["ID"] = getIdentifier();
+        jdump["type"] = "output";
+        jdump["key"] = "Output";
         return jdump;
     }
 

+ 5 - 2
lib/src/ProcessorBase.cpp

@@ -592,7 +592,10 @@ namespace mdd{
         json jid_copy = jid;
         auto jid_typ = jid.find("type");
         auto jid_copy_pref = jid_copy.find("prefix");
-        jid_copy_pref->erase(jid_copy_pref->begin());
+        if (!jid_copy_pref->empty())
+        {
+            jid_copy_pref->erase(jid_copy_pref->begin());
+        }
         int final_size = 0;
         if (jid_typ != jid.end())
         {
@@ -621,7 +624,7 @@ namespace mdd{
                     return proc_ptr->getModule(jid_copy);
                 }
             }
-            else
+            else if(module_ptr != nullptr)
             {
                 return module_ptr;
             }

+ 4 - 0
lib/src/Registration.cpp

@@ -12,6 +12,8 @@
 
 #include "ProcessorStandard.h"
 
+#include "OptimizerEvolutionary.h"
+
 namespace mdd
 {
 	Registration::Registration()
@@ -27,6 +29,8 @@ namespace mdd
 		REGISTER_MODULE(ProcessorStandard);
 
 		REGISTER_PROCESSOR(ProcessorStandard);
+
+		REGISTER_OPTIMIZER(OptimizerEvolutionary);
 	}
 	IModule::Ptr Registration::generateModule(const std::string& name)
 	{