From 9f0bf214b980af65a915c966ba44d683f43e59fc Mon Sep 17 00:00:00 2001 From: jrtechs Date: Sat, 23 Feb 2019 14:03:26 -0500 Subject: [PATCH] Added a line chart displaying the average cost and best cost. --- geneticAlgorithm/geneticAlgo.html | 112 ++++++++++++++++++++++-------- 1 file changed, 83 insertions(+), 29 deletions(-) diff --git a/geneticAlgorithm/geneticAlgo.html b/geneticAlgorithm/geneticAlgo.html index 8d9c8d8..e8f31d9 100644 --- a/geneticAlgorithm/geneticAlgo.html +++ b/geneticAlgorithm/geneticAlgo.html @@ -114,11 +114,13 @@ const naturalSelection = function(population, keepNumber, fitnessFunction) { let fitnessArray = []; + let total = 0; for(let i = 0; i < population.length; i++) { const fitness = fitnessFunction(population[i]); console.log(fitness); fitnessArray.push({fit:fitness, chrom: population[i]}); + total+= fitness; } fitnessArray.sort(predicateBy("fit")); @@ -130,7 +132,7 @@ { survivors.push(fitnessArray[i].chrom); } - return {survivors: survivors, bestFit: bestFitness, bestChrom: bestChromosome}; + return {average: total/population.length, survivors: survivors, bestFit: bestFitness, bestChrom: bestChromosome}; }; const blendGene = function(gene1, gene2, blendCoef) @@ -254,12 +256,10 @@ bestCostG = Number.MAX_VALUE, bestChromosomeG = genericChromosomeG; const runGeneticOptimizationforGraph = function() { - - - - let generationResult = naturalSelection(populationG, keepNumberG, costFunctionG); + stats.push([generationG, generationResult.bestFit, generationResult.average]); + if(bestCostG > generationResult.bestFit) { bestChromosomeG = generationResult.bestChrom; @@ -269,7 +269,6 @@ generationG++; - console.log("Generation " + generationG + " Best Cost: " + bestCostG); console.log(generationResult); @@ -279,12 +278,13 @@ mutatePopulation(populationG, mutationRateG); createGraph(); - }; + + let stats = []; const createGraph = function() { - var dataPoints = []; + var dataPoints = []; console.log(dataPoints); @@ -307,6 +307,29 @@ var chart = new google.visualization.ScatterChart(document.getElementById('chart_div')); chart.draw(data, options); + + //line chart stuff + var line_data = new google.visualization.DataTable(); + line_data.addColumn('number', 'Generation'); + line_data.addColumn('number', 'Best'); + line_data.addColumn('number', 'Average'); + + line_data.addRows(stats); + + console.log(stats); + + var lineChartOptions = { + hAxis: { + title: 'Generation' + }, + vAxis: { + title: 'Cost' + }, + colors: ['#AB0D06', '#007329'] + }; + + var chart = new google.visualization.LineChart(document.getElementById('line_chart')); + chart.draw(line_data, lineChartOptions); }; @@ -328,31 +351,63 @@ generationG = 0; - function resetPopulation() + function verifyForm() { + if(Number($("#populationSize").val()) <= 1) + { + alert("Population size must be greater than one."); + return false; + } + + if(Number($("#mutationRate").val()) > 1 || + Number($("#mutationRate").val()) < 0) + { + alert("Mutation rate must be between zero and one."); + return false; + } - autoRunning = false; - $("#runAutoOptimizer").val("Auto Run"); + if(Number($("#survivalSize").val()) < 0) + { + alert("Survival size can't be less than one."); + return false; + } + + if(Number($("#newBlood").val()) < 0) + { + alert("New organisms can't be a negative number."); + return false; + } + return true; + } - populationSizeG = $("#populationSize").val(); - mutationRateG = $("#mutationRate").val(); - keepNumberG = $("#survivalSize").val(); - newBloodNumberG = $("#newBlood").val(); - generationG = 0; + function resetPopulation() + { + if(verifyForm()) + { + stats = []; + autoRunning = false; + $("#runAutoOptimizer").val("Auto Run"); - populationG = createRandomPopulation(genericChromosomeG, populationSizeG); - createGraph(); + populationSizeG = $("#populationSize").val(); + mutationRateG = $("#mutationRate").val(); + keepNumberG = $("#survivalSize").val(); + newBloodNumberG = $("#newBlood").val(); + generationG = 0; + + populationG = createRandomPopulation(genericChromosomeG, populationSizeG); + createGraph(); + } } populationG = createRandomPopulation(genericChromosomeG, populationSizeG); - window.onload = function () { + window.onload = function (){ + google.charts.load('current', {packages: ['corechart', 'line']}); google.charts.load('current', {'packages':['corechart']}).then(function() { createGraph(); }) - }; @@ -382,8 +437,6 @@ runAutoOptimizer(); } - - //runGeneticOptimization(exampleOrganism, basicCostFunction, 100, 50, 0.01, 0.3, 20, 10); @@ -394,6 +447,8 @@
+
+ @@ -401,32 +456,31 @@

Population Variables

-
+
- +
- +
- +
- +

-
+ -