Skip to content

Commit

Permalink
more to go. stuck on some data issues
Browse files Browse the repository at this point in the history
  • Loading branch information
nurfnick committed Apr 23, 2024
1 parent d6bc0c5 commit aa50ba7
Showing 1 changed file with 146 additions and 41 deletions.
187 changes: 146 additions & 41 deletions Homework8/GA.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"metadata": {
"colab": {
"provenance": [],
"authorship_tag": "ABX9TyOpPNpBUBdQYTGcf0sJwarI",
"authorship_tag": "ABX9TyNRW9E75RblXpm5OcrV61K5",
"include_colab_link": true
},
"kernelspec": {
Expand Down Expand Up @@ -63,29 +63,32 @@
"\n",
"#create the function to evaluate against\n",
"\n",
"def evaluate(x,n):\n",
" x = np.array(x)\n",
" return 418.982887272433*n - sum(x*np.sin(np.sqrt(np.abs(x))))"
"def evaluate(x):\n",
" s = 0\n",
" for i in x:\n",
" s+=418.982887272433 - i*np.sin(np.sqrt(abs(i)))\n",
"\n",
" return s"
],
"metadata": {
"id": "26OLLbiuFZUD"
},
"execution_count": 18,
"execution_count": 41,
"outputs": []
},
{
"cell_type": "code",
"source": [
"evaluate([420.9687,420.9687],n)"
"evaluate([420.9687,420.9687])"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "2SnKeAulFcDb",
"outputId": "0cbb0f16-c575-46de-d74a-700c8f81831e"
"outputId": "7d9b80e2-44cb-4cdf-9ba5-2e1ffadd504f"
},
"execution_count": 19,
"execution_count": 42,
"outputs": [
{
"output_type": "execute_result",
Expand All @@ -95,7 +98,7 @@
]
},
"metadata": {},
"execution_count": 19
"execution_count": 42
}
]
},
Expand All @@ -113,7 +116,9 @@
" return x\n",
"\n",
"\n",
"populationSize = 8 #size of GA population\n",
"populationSize = 10 #size of GA population\n",
"\n",
"\n",
"#create initial population by calling the \"createChromosome\" function many times and adding each to a list of chromosomes (a.k.a., the \"population\")\n",
"def initializePopulation(): #n is size of population; d is dimensions of chromosome\n",
"\n",
Expand All @@ -122,7 +127,7 @@
"\n",
" for i in range(populationSize):\n",
" population.append(createChromosome(n))\n",
" populationFitness.append(evaluate(population[i],n))\n",
" populationFitness.append(evaluate(population[i]))\n",
"\n",
" tempZip = zip(population, populationFitness)\n",
" popVals = sorted(tempZip, key=lambda tempZip: tempZip[1], reverse = False)\n",
Expand All @@ -137,7 +142,7 @@
"metadata": {
"id": "ESLFVICxG9Qk"
},
"execution_count": 22,
"execution_count": 38,
"outputs": []
},
{
Expand All @@ -152,26 +157,26 @@
"base_uri": "https://localhost:8080/"
},
"id": "C0PY_zLWG-1Q",
"outputId": "07e81843-2e49-4224-c620-27dcb70b8ef9"
"outputId": "e2e8ba98-28dc-4415-ced8-192539db2532"
},
"execution_count": 24,
"execution_count": 12,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"[([404.1146682060388, 449.1530471895783], 132.53033175884536),\n",
" ([-310.1188587528966, 337.2710247821185], 701.8483695185636),\n",
" ([23.82641000158003, 178.19367282497115], 735.8108475652186),\n",
" ([73.00130668862175, 241.2664897151866], 739.6125402676463),\n",
" ([0.14082369026658625, 140.209703122782], 930.9292019383014),\n",
" ([274.02640683615505, -260.05922108516745], 937.4122993889677),\n",
" ([-398.4425244009062, -137.0827512333458], 1091.3898621600692),\n",
" ([-431.4228606636429, -95.97131947910367], 1208.319706559776)]"
"[([446.14353105779264, 250.54704019124802], 527.4133728145813),\n",
" ([-149.55511712002232, 457.5069782750403], 529.5175755072069),\n",
" ([341.5798881575133, 401.7493665558342], 587.1606585151048),\n",
" ([221.67542833548737, 98.15809607258109], 721.5727168785847),\n",
" ([235.9259771456252, 88.35921480816262], 755.2998456833657),\n",
" ([-159.75914916703096, 252.72064983338123], 897.1888728390384),\n",
" ([-62.253387400501026, -159.54732439472906], 910.5154124016872),\n",
" ([-424.9087346323346, -132.76709292353138], 1140.227575574424)]"
]
},
"metadata": {},
"execution_count": 24
"execution_count": 12
}
]
},
Expand Down Expand Up @@ -204,33 +209,34 @@
"\n",
" return offspring1, offspring2 #two offspring are returned\n",
"\n",
"crossover(pop[0][0],pop[1][0])"
"child1, child2 = crossover(pop[0][0],pop[1][0])\n",
"\n",
"evaluate(child1)"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "ylKte0suIux4",
"outputId": "33668d90-4ac3-4012-dcb8-54ff4959fa0d"
"outputId": "f5600df1-edfa-4689-b10f-87cbeea3afe2"
},
"execution_count": 85,
"execution_count": 31,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"([404.1146682060388, 449.1530471895783],\n",
" [-310.1188587528966, 337.2710247821185])"
"238.25919031028036"
]
},
"metadata": {},
"execution_count": 85
"execution_count": 31
}
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 33,
"metadata": {
"id": "E5M81SgLEU7T"
},
Expand All @@ -250,7 +256,7 @@
"\n",
"crossOverRate = 0.8 #currently not used in the implementation; neeeds to be used.\n",
"mutationRate = 0.05 #currently not used in the implementation; neeeds to be used.\n",
"eliteSolutions = 10 #currently not used in the implementation; neeed to use some type of elitism\n",
"eliteSolutions = 3 #currently not used in the implementation; neeed to use some type of elitism\n",
"\n",
"\n",
"\n",
Expand Down Expand Up @@ -280,9 +286,16 @@
"\n",
"def rouletteWheel(pop):\n",
"\n",
" matingPool = [] #initialize an empty array\n",
" rank = [i/(len(pop)*(len(pop)+1)/2) for i in range(len(pop),0,-1)] #get a probability based on rank\n",
" cumrank = np.cumsum(rank) #needed cummulative probability of those ranks.\n",
"\n",
" #create sometype of rouletteWheel selection -- can be based on fitness function or fitness rank\n",
" #(remember the population is always ordered from most fit to least fit, so pop[0] is the fittest chromosome in the population, and pop[populationSize-1] is the least fit!\n",
" while len(matingPool)<populationSize: #fill up the whole mating pool\n",
" p = myPRNG.random() #get a rando between 0 and 1\n",
" i = 0 #start with the most likely\n",
" while p>cumrank[i]: #grab the random int when finally surpass the p value\n",
" i+=1\n",
" matingPool.append(pop[i]) #add it into the pool\n",
"\n",
"\n",
" return matingPool\n",
Expand All @@ -297,7 +310,45 @@
"\n",
"\n",
"\n",
"\n",
"\n"
]
},
{
"cell_type": "code",
"source": [
"rouletteWheel(pop)"
],
"metadata": {
"id": "FEeSCCBxQtDW",
"outputId": "6082fcfd-9fb9-4afd-fa63-dc40536ad245",
"colab": {
"base_uri": "https://localhost:8080/"
}
},
"execution_count": 19,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"[([235.9259771456252, 88.35921480816262], 755.2998456833657),\n",
" ([-159.75914916703096, 252.72064983338123], 897.1888728390384),\n",
" ([341.5798881575133, 401.7493665558342], 587.1606585151048),\n",
" ([235.9259771456252, 88.35921480816262], 755.2998456833657),\n",
" ([-149.55511712002232, 457.5069782750403], 529.5175755072069),\n",
" ([-424.9087346323346, -132.76709292353138], 1140.227575574424),\n",
" ([341.5798881575133, 401.7493665558342], 587.1606585151048),\n",
" ([-149.55511712002232, 457.5069782750403], 529.5175755072069)]"
]
},
"metadata": {},
"execution_count": 19
}
]
},
{
"cell_type": "code",
"source": [
"#breeding -- uses the \"mating pool\" and calls \"crossover\" function\n",
"def breeding(matingPool):\n",
" #the parents will be the first two individuals, then next two, then next two and so on\n",
Expand All @@ -317,7 +368,7 @@
" childrenFitness.append(evaluate(child2))\n",
"\n",
" tempZip = zip(children, childrenFitness)\n",
" popVals = sorted(tempZip, key=lambda tempZip: tempZip[1], reverse = True)\n",
" popVals = sorted(tempZip, key=lambda tempZip: tempZip[1], reverse = False)\n",
"\n",
" #the return object is a sorted list of tuples:\n",
" #the first element of the tuple is the chromosome; the second element is the fitness value\n",
Expand All @@ -326,6 +377,35 @@
"\n",
" return popVals\n",
"\n",
"crossover(pop[0],pop[1])"
],
"metadata": {
"id": "03X8tEbfQ89p",
"outputId": "88fcf848-2bf7-48b2-d938-b5cf00036661",
"colab": {
"base_uri": "https://localhost:8080/"
}
},
"execution_count": 46,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"(([446.14353105779264, 250.54704019124802], 529.5175755072069),\n",
" ([-149.55511712002232, 457.5069782750403], 527.4133728145813))"
]
},
"metadata": {},
"execution_count": 46
}
]
},
{
"cell_type": "code",
"source": [
"\n",
"\n",
"\n",
"#insertion step\n",
"def insert(pop,kids):\n",
Expand All @@ -334,6 +414,10 @@
" #at the VERY LEAST evaluate the best solution from \"pop\" to make sure you are not losing a very good chromosome from last generation\n",
" #maybe want to keep the top 5? 10? solutions from pop -- it's up to you.\n",
"\n",
" for i in range(eliteSolutions):\n",
" kids.append(pop[i])\n",
"\n",
"\n",
" return kids\n",
"\n",
"\n",
Expand All @@ -347,9 +431,6 @@
"#the best solution should always be the first element...\n",
"def bestSolutionInPopulation(pop):\n",
" print (\"Best solution: \", pop[0][0])\n",
" print (\"Items selected: \", itemsSelected(pop[0][0]))\n",
" print (\"Value: \", pop[0][1])\n",
" print (\"Weight: \", calcWeight(pop[0][0]))\n",
"\n",
"\n",
"\n",
Expand All @@ -364,7 +445,7 @@
"\n",
" for j in range(Generations):\n",
"\n",
" mates=tournamentSelection(Population,10) #<--need to replace this with roulette wheel selection, e.g.: mates=rouletteWheel(Population)\n",
" mates=rouletteWheel(Population) #<--need to replace this with roulette wheel selection, e.g.: mates=rouletteWheel(Population)\n",
" Offspring = breeding(mates)\n",
" Population = insert(Population, Offspring)\n",
"\n",
Expand All @@ -382,8 +463,32 @@
"\n",
"\n",
"if __name__ == \"__main__\":\n",
" main()\n",
""
" main()\n"
],
"metadata": {
"id": "lZqIR1dVQqjB",
"outputId": "9755db05-f54c-47b1-ca32-995cd63aff8f",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 310
}
},
"execution_count": 35,
"outputs": [
{
"output_type": "error",
"ename": "ValueError",
"evalue": "setting an array element with a sequence. The requested array has an inhomogeneous shape after 1 dimensions. The detected shape was (2,) + inhomogeneous part.",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-35-068fac699cec>\u001b[0m in \u001b[0;36m<cell line: 56>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 55\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 56\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0m__name__\u001b[0m \u001b[0;34m==\u001b[0m \u001b[0;34m\"__main__\"\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 57\u001b[0;31m \u001b[0mmain\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;32m<ipython-input-35-068fac699cec>\u001b[0m in \u001b[0;36mmain\u001b[0;34m()\u001b[0m\n\u001b[1;32m 38\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 39\u001b[0m \u001b[0mmates\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mrouletteWheel\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mPopulation\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;31m#<--need to replace this with roulette wheel selection, e.g.: mates=rouletteWheel(Population)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 40\u001b[0;31m \u001b[0mOffspring\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mbreeding\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mmates\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 41\u001b[0m \u001b[0mPopulation\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0minsert\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mPopulation\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mOffspring\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 42\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m<ipython-input-34-6c3cccffa939>\u001b[0m in \u001b[0;36mbreeding\u001b[0;34m(matingPool)\u001b[0m\n\u001b[1;32m 14\u001b[0m \u001b[0mchildren\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mappend\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mchild2\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 15\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 16\u001b[0;31m \u001b[0mchildrenFitness\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mappend\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mevaluate\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mchild1\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 17\u001b[0m \u001b[0mchildrenFitness\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mappend\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mevaluate\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mchild2\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 18\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m<ipython-input-9-dc483f8ba73b>\u001b[0m in \u001b[0;36mevaluate\u001b[0;34m(x)\u001b[0m\n\u001b[1;32m 34\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 35\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mevaluate\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mx\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 36\u001b[0;31m \u001b[0mx\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0marray\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mx\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 37\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0;36m418.982887272433\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0mn\u001b[0m \u001b[0;34m-\u001b[0m \u001b[0msum\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mx\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msin\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msqrt\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mabs\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mx\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;31mValueError\u001b[0m: setting an array element with a sequence. The requested array has an inhomogeneous shape after 1 dimensions. The detected shape was (2,) + inhomogeneous part."
]
}
]
}
]
Expand Down

0 comments on commit aa50ba7

Please sign in to comment.