public override void PrepareSolution(GPSolution solution) { TradeSystem ts = new TradeSystem(config); ts.name = solution.name; foreach (string var in listVariaveis) { if (config.IsGPVarDefined(var)) { ts.vm.SetVariavel(var, config.GetGPVarValue(var)); } else { ts.vm.SetVariavel(var, solution.GetValueAsNumber(var)); } } ts.targetSaida = solution.GetValueAsString(PROP_COND_TARGET_SAIDA); if (config.flagCompra) { ts.condicaoEntradaC = solution.GetValueAsString(PROP_COND_ENTRADA_C); ts.condicaoSaidaC = solution.GetValueAsString(PROP_COND_SAIDA_C); ts.stopMovelC = solution.GetValueAsString(PROP_COND_STOP_MOVEL_C); ts.stopInicialC = solution.GetValueAsString(PROP_COND_STOP_INICIAL_C); ts.sizingCompra = solution.GetValueAsString(PROP_SIZING_C); } if (config.flagVenda) { ts.condicaoEntradaV = solution.GetValueAsString(PROP_COND_ENTRADA_V); ts.condicaoSaidaV = solution.GetValueAsString(PROP_COND_SAIDA_V); ts.stopMovelV = solution.GetValueAsString(PROP_COND_STOP_MOVEL_V); ts.stopInicialV = solution.GetValueAsString(PROP_COND_STOP_INICIAL_V); ts.sizingVenda = solution.GetValueAsString(PROP_SIZING_V); } solution.SetPropriedade(ConstsComuns.OBJ_TRADESYSTEM, ts); }
public override void RunSolution(GPSolution solution) { TradeSystem ts = solution.GetPropriedade(ConstsComuns.OBJ_TRADESYSTEM) as TradeSystem; ts.name = solution.name; Carteira carteira = gpController.RunBackTester(ts, solution.name); carteira.monteCarlo.properties = solution; float dif = carteira.GetCapital() - carteira.capitalInicial; if (dif > 0) { float?vExist = solution.GetPropriedade(ConstsComuns.OBJ_TOTAL_PROFIT) as float?; float v = vExist == null ? 0 : (float)vExist; v += dif; solution.SetPropriedade(ConstsComuns.OBJ_TOTAL_PROFIT, v); } else { float?vExist = solution.GetPropriedade(ConstsComuns.OBJ_TOTAL_LOSS) as float?; float v = vExist == null ? 0 : (float)vExist; v += dif; solution.SetPropriedade(ConstsComuns.OBJ_TOTAL_LOSS, v); } solution.iterations++; solution.SetPropriedade(ConstsComuns.OBJ_ITERATIONS, solution.iterations); solution.fitnessResult = carteira.monteCarlo.CalcFitness(); }
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 void TestGPInicial() { GPSolutionDefinition sh = new GPSolutionDefinition(config); SemanticaList listaFormulas = CriaListaDefault(sh); SemanticaList listaNumerica = CriaListaNumerica(sh); GPTemplate template = CreateTestTemplate(config, listaFormulas, listaNumerica); GPPool pool = new GPPool(config); Assert.IsTrue(config.poolSize > 0); Assert.IsTrue(config.elitismRange > 0); Assert.IsTrue(config.mutationRatePerc > 0); Assert.IsTrue(config.mutationRange > 0); Assert.IsTrue(config.poolSize > 0); pool.InitPool(template); Assert.IsTrue(pool.solutions.Count > 0); //randomizo valores iniciais para fazer ordenação Random rnd = new Random(); for (int i = 0; i < pool.solutions.Count; i++) { GPSolution solution = pool.solutions[i]; solution.fitnessResult = rnd.Next(1, 10000); } pool.SortFitness(); for (int i = 1; i < pool.solutions.Count; i++) { Assert.IsTrue(pool.solutions[i - 1].fitnessResult >= pool.solutions[i].fitnessResult); } GPSolution sol1 = pool.solutions[1]; GPSolution sol2 = pool.solutions[2]; string splicedKey = sol1.SpliceWith(sol2); Assert.IsNotNull(splicedKey); string strOriginal = sol1.GetValue(splicedKey).ToString(); sol1.Mutate(sol1.GetValue(splicedKey), splicedKey); string strMutated = sol1.GetValue(splicedKey).ToString(); //Assert.IsFalse(strOriginal == strMutated,"Não mutou: "+strOriginal); GPSolution badSolution = pool.solutions[pool.solutions.Count - 1]; pool.Mutate(); Assert.IsFalse(pool.solutions.Contains(badSolution)); Assert.IsTrue(pool.solutions.Count == config.poolSize, pool.solutions.Count + "<>" + config.poolSize); }
public solution.GPSolution CreateRandomSolution() { GPSolution solution = new GPSolution(this); foreach (string key in properties.Keys) { SemanticaList lista = properties[key]; solution.SetValue(key, lista.CreateRandomNode(config, false)); } return(solution); }
public void TestRandomSolution() { MockReferView rv = new MockReferView(); ConfigController configController = new ConfigController(rv); GeneticProgrammingController gpc = new GeneticProgrammingController(rv, configController); gpc.gpRunner.gpConfig.poolSize = 2; gpc.InitPool(); GPSolution solution = gpc.gpRunner.pool.template.CreateRandomSolution(); Assert.IsNotNull(solution); }
public void EndTurn() { //penalizando solucoes com mesmo fitness (parece que a mesma solução se alastra, ficando todos iguais) for (int i = 0; i < solutions.Count - 1; i++) { GPSolution solI = solutions[i]; for (int j = i + 1; j < solutions.Count; j++) { GPSolution solJ = solutions[j]; if (solI.fitnessResult == solJ.fitnessResult) { solJ.fitnessResult -= Utils.Random(PENALTY, PENALTY * 2); } } } SortFitness(); }
public override void EndSolution(GPSolution solution) { solution.RemovePropriedade(UsoComum.ConstsComuns.OBJ_MONTECARLO); if (solution.iterations >= config.saveMinIterations) { float totalProfit = solution.GetPropriedadeAsFloat(UsoComum.ConstsComuns.OBJ_TOTAL_PROFIT); float totalLoss = Math.Abs(solution.GetPropriedadeAsFloat(UsoComum.ConstsComuns.OBJ_TOTAL_LOSS)); float profitRatio = totalProfit / (totalProfit + totalLoss + 1); if (profitRatio > config.saveMinProfitRatio) { GPSolutionProxy proxy = new GPSolutionProxy(); proxy.solution = solution; proxy.tradeSystem = solution.GetPropriedade(UsoComum.ConstsComuns.OBJ_TRADESYSTEM) as TradeSystem; solution.RemovePropriedade(UsoComum.ConstsComuns.OBJ_TRADESYSTEM); SaveSolutionToCheck(proxy); } } }
public abstract void PrepareSolution(GPSolution solution);
public abstract void EndSolution(GPSolution solution);
public abstract void RunSolution(GPSolution solution);
public void Mutate() { IList <GPSolution> nextSolutions = new List <GPSolution>(); for (int i = 0; i < solutions.Count; i++) { if (i <= config.elitismRange * solutions.Count / 100) { nextSolutions.Add(solutions[i]); } else if (i <= config.mutationRange * solutions.Count / 100) { GPSolution solution = solutions[i]; solution.Mutate(20); nextSolutions.Add(solutions[i]); } else if (i <= config.generateChildRange * solutions.Count / 100) { GPSolution solution1 = solutions[Utils.RandomInt(0, config.generateChildRange / 100f * solutions.Count)]; GPSolution solution2 = solutions[Utils.RandomInt(0, config.generateChildRange / 100f * solutions.Count)]; GPSolution child2 = null; GPSolution child = solution1.CreateChildWith(solution2, out child2); nextSolutions.Add(child); child.Mutate(20); nextSolutions.Add(child2); child2.Mutate(20); i += 1; } else { nextSolutions.Add(template.CreateRandomSolution()); } } solutions.Clear(); solutions = nextSolutions; /* * * for (int i = config.elitismRange * solutions.Count/100; i < solutions.Count; i++) * { * GPSolution solution = solutions[i]; * * if (Utils.Random(0, 100) < config.spliceChancePerc) * { * //Pego um dos top 85% * GPSolution mateWith = solutions[Utils.Random(0, 85) / 100 * solutions.Count]; * solution.SpliceWith(mateWith); * } * if (Utils.Random(0, 100) <= config.mutationRatePerc) * { * solution.Mutate(); * } * //TODO: mutate, splice, etc * } * * for (int i = solutions.Count-1;i>=config.unfitRemovalPercent * config.poolSize / 100; i--) * { * solutions.RemoveAt(i); * } * for (int i = config.unfitRemovalPercent * config.poolSize / 100; i < config.poolSize; i++) * { * solutions.Add(template.CreateRandomSolution()); * }*/ }