Skip to content

Commit a75c043

Browse files
committed
Created using Colaboratory
1 parent e2248d9 commit a75c043

1 file changed

Lines changed: 284 additions & 2 deletions

File tree

Genetic_Algorithm_Assignment.ipynb

Lines changed: 284 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,7 @@
370370
" else:\n",
371371
" #These will be non solutions. Therefore the fitness is reduced by multiplying by 0.5\n",
372372
" #The file size is stored\n",
373-
" fitness.loc[i,'fitness'] = duration*0.1\n",
373+
" fitness.loc[i,'fitness'] = duration*0.5\n",
374374
" filesize.loc[i,'size'] = size\n",
375375
" \n",
376376
" #The PER solution size and duration values are reset\n",
@@ -590,7 +590,7 @@
590590
" total_duration += df.loc[i,'duration']\n",
591591
" \n",
592592
" if(total_size>4500):\n",
593-
" fitness = total_duration*0.1\n",
593+
" fitness = total_duration*0.5\n",
594594
" return fitness,total_size\n",
595595
" else:\n",
596596
" fitness = total_duration\n",
@@ -3389,6 +3389,288 @@
33893389
"colab_type": "code",
33903390
"colab": {}
33913391
},
3392+
"source": [
3393+
"def mutate():\n",
3394+
" #Get 4 distinct values\n",
3395+
" state = True\n",
3396+
" a = 0\n",
3397+
" b = 0\n",
3398+
" c = 0\n",
3399+
" target_id = 0\n",
3400+
" scaleFactor = 0.5\n",
3401+
" while(state):\n",
3402+
" a = np.random.randint(0,popSize)\n",
3403+
" b = np.random.randint(0,popSize)\n",
3404+
" c = np.random.randint(0,popSize)\n",
3405+
" target_id = np.random.randint(0,popSize)\n",
3406+
" state = checkDistinct(a,b,c,target_id)\n",
3407+
" #print(\"a:\" +str(a) +\" b :\"+str(b) +\" c:\" + str(c) +\" target id:\" +str(target_id) )\n",
3408+
" #Get the respective solution items as arrays\n",
3409+
" a_item = population.iloc[a,1:].values\n",
3410+
" b_item = population.iloc[b,1:].values\n",
3411+
" c_item = population.iloc[c,1:].values\n",
3412+
" target_item = population.iloc[target_id,1:].values\n",
3413+
" #Add the weighted difference\n",
3414+
" for i in range(2):\n",
3415+
" a_item[i] = a_item[i] + scaleFactor*(b_item[i]-c_item[i])\n",
3416+
" \n",
3417+
" return a_item, target_item,target_id"
3418+
],
3419+
"execution_count": 0,
3420+
"outputs": []
3421+
},
3422+
{
3423+
"cell_type": "code",
3424+
"metadata": {
3425+
"id": "Ij0vnppYBGa-",
3426+
"colab_type": "code",
3427+
"colab": {}
3428+
},
3429+
"source": [
3430+
"def crossover(donor,target):\n",
3431+
" #crossover rate\n",
3432+
" CR = 0.1\n",
3433+
" \n",
3434+
" #size of the arrays\n",
3435+
" size = len(donor)\n",
3436+
" \n",
3437+
" #trial vector\n",
3438+
" trial_vector = np.empty([size])\n",
3439+
" \n",
3440+
" #create an array with random values\n",
3441+
" random_array = [random.uniform(0,1) for x in range(size)]\n",
3442+
" for i in range(size):\n",
3443+
" if(random_array[i] <=CR or i == random.randint(1,size)):\n",
3444+
" #replace with donor\n",
3445+
" trial_vector[i] = donor[i]\n",
3446+
" else:\n",
3447+
" #replace with target\n",
3448+
" trial_vector[i] = target[i]\n",
3449+
" \n",
3450+
" return trial_vector"
3451+
],
3452+
"execution_count": 0,
3453+
"outputs": []
3454+
},
3455+
{
3456+
"cell_type": "code",
3457+
"metadata": {
3458+
"id": "MTIc92SoBKUd",
3459+
"colab_type": "code",
3460+
"colab": {}
3461+
},
3462+
"source": [
3463+
"def checkDistinct(a,b,c,d):\n",
3464+
" if(a != b != c !=d != a != c and b != d ):\n",
3465+
" return False\n",
3466+
" else:\n",
3467+
" return True"
3468+
],
3469+
"execution_count": 0,
3470+
"outputs": []
3471+
},
3472+
{
3473+
"cell_type": "code",
3474+
"metadata": {
3475+
"id": "QTkeZjMBBNam",
3476+
"colab_type": "code",
3477+
"colab": {}
3478+
},
3479+
"source": [
3480+
"def selection(trial_vector,target):\n",
3481+
" fitness_trial,size_trial = getFitness(trial_vector)\n",
3482+
" fitness_target,size_target = getFitness(trial_vector)\n",
3483+
" \n",
3484+
" \n",
3485+
" if(fitness_trial>fitness_target):\n",
3486+
" target = trial_vector\n",
3487+
" else : \n",
3488+
" target = target\n",
3489+
" \n",
3490+
" return target"
3491+
],
3492+
"execution_count": 0,
3493+
"outputs": []
3494+
},
3495+
{
3496+
"cell_type": "code",
3497+
"metadata": {
3498+
"id": "H4et_MbNBQQm",
3499+
"colab_type": "code",
3500+
"colab": {}
3501+
},
3502+
"source": [
3503+
"def replace_to_population(offspring,target_id):\n",
3504+
" population.iloc[target_id,1:] = offspring"
3505+
],
3506+
"execution_count": 0,
3507+
"outputs": []
3508+
},
3509+
{
3510+
"cell_type": "code",
3511+
"metadata": {
3512+
"id": "d4JeqFSUBUfy",
3513+
"colab_type": "code",
3514+
"colab": {}
3515+
},
3516+
"source": [
3517+
"def iterate(n):\n",
3518+
" #value = ackley_function(solution[0],solution[1])\n",
3519+
" for i in range(n):\n",
3520+
" donor, target,target_id = mutate()\n",
3521+
" trial_vector = crossover(donor,target)\n",
3522+
" offspring = selection(trial_vector,target)\n",
3523+
" replace_to_population(offspring,target_id)"
3524+
],
3525+
"execution_count": 0,
3526+
"outputs": []
3527+
},
3528+
{
3529+
"cell_type": "code",
3530+
"metadata": {
3531+
"id": "AUxCaqBt_q9k",
3532+
"colab_type": "code",
3533+
"colab": {}
3534+
},
3535+
"source": [
3536+
"popSize = 100\n",
3537+
"population = createInitPop(popSize)"
3538+
],
3539+
"execution_count": 0,
3540+
"outputs": []
3541+
},
3542+
{
3543+
"cell_type": "code",
3544+
"metadata": {
3545+
"id": "3vMEcpBIBawr",
3546+
"colab_type": "code",
3547+
"colab": {}
3548+
},
3549+
"source": [
3550+
"iterate(1000)"
3551+
],
3552+
"execution_count": 0,
3553+
"outputs": []
3554+
},
3555+
{
3556+
"cell_type": "code",
3557+
"metadata": {
3558+
"id": "dvQTXM1HBcUc",
3559+
"colab_type": "code",
3560+
"colab": {}
3561+
},
3562+
"source": [
3563+
"finalFitness = []\n",
3564+
"finalSize = []\n",
3565+
"#Get Fitness and Size information for the Last Population\n",
3566+
"for i in range(popSize):\n",
3567+
" fitness,size = getFitness(population.iloc[i,1:].values)\n",
3568+
" finalFitness.append(fitness)\n",
3569+
" finalSize.append(size)"
3570+
],
3571+
"execution_count": 0,
3572+
"outputs": []
3573+
},
3574+
{
3575+
"cell_type": "code",
3576+
"metadata": {
3577+
"id": "yBBu8t91DGnF",
3578+
"colab_type": "code",
3579+
"colab": {
3580+
"base_uri": "https://localhost:8080/",
3581+
"height": 34
3582+
},
3583+
"outputId": "356b1b56-ac91-4007-bc10-8981b7f96039"
3584+
},
3585+
"source": [
3586+
"max(finalFitness)"
3587+
],
3588+
"execution_count": 499,
3589+
"outputs": [
3590+
{
3591+
"output_type": "execute_result",
3592+
"data": {
3593+
"text/plain": [
3594+
"573"
3595+
]
3596+
},
3597+
"metadata": {
3598+
"tags": []
3599+
},
3600+
"execution_count": 499
3601+
}
3602+
]
3603+
},
3604+
{
3605+
"cell_type": "code",
3606+
"metadata": {
3607+
"id": "qtqbDM4eDUIV",
3608+
"colab_type": "code",
3609+
"colab": {}
3610+
},
3611+
"source": [
3612+
"#Getting the Optimum solution Information to Variables\n",
3613+
"Best_Solution_id = finalFitness.index(max(finalFitness))\n",
3614+
"Best_Solution_Size = finalSize[Best_Solution_id] \n",
3615+
"Best_Solution_Duration = finalDuration[Best_Solution_id]\n",
3616+
"Best_Solution = chromosome.iloc[Best_Solution_id]\n"
3617+
],
3618+
"execution_count": 0,
3619+
"outputs": []
3620+
},
3621+
{
3622+
"cell_type": "code",
3623+
"metadata": {
3624+
"id": "MBlGAsC6FGFb",
3625+
"colab_type": "code",
3626+
"colab": {
3627+
"base_uri": "https://localhost:8080/",
3628+
"height": 289
3629+
},
3630+
"outputId": "d80b3e48-b3ff-4879-ae71-ece12612a4ec"
3631+
},
3632+
"source": [
3633+
"#Printing the Optimum Solution Information\n",
3634+
"print(\"Optimum Solution ID : \" + str(Best_Solution_id))\n",
3635+
"print(\"Optimum Fitness : \" + str(finalFitness[Best_Solution_id]))\n",
3636+
"print(\"Optimum Solution File Size : \" + str(Best_Solution_Size) + \"MB\")\n",
3637+
"print(\"Optimum Solution Duration : \" + str(Best_Solution_Duration) + \" Minutes\")\n",
3638+
"print(\"Optimum Solution : \")\n",
3639+
"print(str(Best_Solution[1:]))"
3640+
],
3641+
"execution_count": 501,
3642+
"outputs": [
3643+
{
3644+
"output_type": "stream",
3645+
"text": [
3646+
"Optimum Solution ID : 4\n",
3647+
"Optimum Fitness : 573\n",
3648+
"Optimum Solution File Size : 4225MB\n",
3649+
"Optimum Solution Duration : 444 Minutes\n",
3650+
"Optimum Solution : \n",
3651+
"file 1 1\n",
3652+
"file 2 0\n",
3653+
"file 3 1\n",
3654+
"file 4 1\n",
3655+
"file 5 0\n",
3656+
"file 6 0\n",
3657+
"file 7 0\n",
3658+
"file 8 0\n",
3659+
"file 9 0\n",
3660+
"file 10 1\n",
3661+
"Name: 4, dtype: int64\n"
3662+
],
3663+
"name": "stdout"
3664+
}
3665+
]
3666+
},
3667+
{
3668+
"cell_type": "code",
3669+
"metadata": {
3670+
"id": "sjpx9UGUFGry",
3671+
"colab_type": "code",
3672+
"colab": {}
3673+
},
33923674
"source": [
33933675
""
33943676
],

0 commit comments

Comments
 (0)