public GPAbstractNode InternalCreateRandomNode(GPConfig config, bool flagTerminal, GPAbstractNode parent = null, int countLevel = 0) { GPSemantica semantica = GetRandomSemantica(flagTerminal); GPAbstractNode node = semantica.InstantiateEmpty(); if (parent == null) { parent = node; } int rndVal = rnd.Next(semantica.minParams, semantica.maxParams); for (int i = 0; i < rndVal; i++) { GPAbstractNode nodeParam = InternalCreateRandomNode(config, countLevel >= config.minLevels, parent, countLevel + 1); if (!node.CanAddNode(nodeParam)) { throw new Exception("Node não permite adicionar nodeParam!"); } node.AddNode(nodeParam); } return(node); }
private string SpliceWith(GPSolution mateWith, string key) { if (!valores.ContainsKey(key)) { return(null); } if (!mateWith.valores.ContainsKey(key)) { return(null); } GPAbstractNode rootNode1 = valores[key]; GPAbstractNode rootNode2 = mateWith.valores[key]; if (rootNode1.Size() > 1 && rootNode2.Size() > 1) { int count = 0; GPAbstractNode rndNode1 = rootNode1.GetNthChild(Utils.RandomInt(2, rootNode1.Size()), ref count); count = 0; GPAbstractNode rndNode2 = rootNode2.GetNthChild(Utils.RandomInt(2, rootNode2.Size()), ref count); if (!rndNode1.nodePai.TransferNode(rndNode1, rndNode2)) { Utils.Error("Erro fazendo transferNode!"); return(null); } } else { valores.Remove(key); mateWith.valores.Remove(key); valores.Add(key, rootNode2); mateWith.valores.Add(key, rootNode1); } return(key); }
public void TestGPTemplate() { GPSolutionDefinition holder = new GPSolutionDefinition(config); /* * vou simular um template com 4 formulas(nodes) e 2 variaveis numéricas * f1(NODE,NUMBER) * F2(NODE,NODE) * F3 * F4(NUMBER) */ SemanticaList listaFormulas = CriaListaDefault(holder); //Lista Numerica SemanticaList listaNumerica = CriaListaNumerica(holder); //Templates: possuem propriedades evolutivas, cada uma apontando para uma lista de semantica GPTemplate template = CreateTestTemplate(config, listaFormulas, listaNumerica); GPSolution solution = template.CreateRandomSolution(); Assert.IsNotNull(solution); GPAbstractNode p1 = solution.GetValue("prop1"); Assert.IsNotNull(p1); GPAbstractNode p2 = solution.GetValue("prop2"); Assert.IsNotNull(p2); GPAbstractNode p3 = solution.GetValue("prop3"); Assert.IsNotNull(p3); GPAbstractNode p4 = solution.GetValue("prop4"); Assert.IsNotNull(p4); GPAbstractNode n1 = solution.GetValue("number1"); Assert.IsNotNull(n1); GPAbstractNode n2 = solution.GetValue("number2"); Assert.IsNotNull(n2); GPAbstractNode clonep1 = p1.Clone(); Assert.IsNotNull(clonep1); Assert.IsFalse(clonep1 == p1); Assert.IsTrue(clonep1.ToString() == p1.ToString()); GPSolution cloneSolution = solution.Clone(); GPSolution solution2 = template.CreateRandomSolution(); GPSolution childSolution2 = null; GPSolution childSolution = solution.CreateChildWith(solution2, out childSolution2); Assert.IsNotNull(childSolution); Assert.IsNotNull(childSolution2); }
public string GetValueAsString(string var) { GPAbstractNode node = GetValue(var); if (node == null) { return(""); } return(node.ToString()); }
public GPSolution Clone() { GPSolution result = new GPSolution(template); foreach (string key in valores.Keys) { GPAbstractNode node = valores[key].Clone(); result.valores.Add(key, node); } return(result); }
internal void FinishLoading(GPSolutionDefinition definition) { if (idSolution > countSolutions) { countSolutions = idSolution + 1; } propriedadesDeNegocio = new Dictionary <string, object>(); foreach (string key in valores.Keys) { GPAbstractNode node = valores[key]; node.FinishLoading(definition); } }
public GPAbstractNode CreateRandomNode(GPConfig config, bool flagTerminal, GPAbstractNode parent = null, int countLevel = 0) { GPAbstractNode ret = null; do { ret = InternalCreateRandomNode(config, flagTerminal, parent, countLevel); if (ret.SizeLevel() > maxLevels || ret.SizeLevel() < minLevels) { ret = null; } } while (ret == null); return(ret); }
public void TestaGPCriacaoPelaLista() { GPPool pool = new GPPool(config); string listDefault = "default"; GPSolutionDefinition definition = CreateSampleDefinition(listDefault); SemanticaList listaFormulas = CriaListaDefault(definition); SemanticaList listSemantic = definition.GetListByName(listDefault); listSemantic.maxLevels = 3; listSemantic.minLevels = 2; GPAbstractNode nodeRandom = listSemantic.CreateRandomNode(config, false); Assert.IsNotNull(nodeRandom); string toString = nodeRandom.ToString(); Assert.IsTrue(nodeRandom.SizeLevel() >= listSemantic.minLevels && nodeRandom.SizeLevel() <= listSemantic.maxLevels, "SizeLevel:" + nodeRandom.SizeLevel()); }
public void Mutate(GPAbstractNode node, string property) { node.Mutate(template.GetProperty(property)); }
internal void SetValue(string key, GPAbstractNode node) { valores.Add(key, node); }