School Commit Init
This commit is contained in:
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
@@ -0,0 +1,470 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "b8210b19",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## A.I. Assignment 5\n",
|
||||
"\n",
|
||||
"## Learning Goals\n",
|
||||
"\n",
|
||||
"By the end of this lab, you should be able to:\n",
|
||||
"* Get more familiar with tensors in pytorch \n",
|
||||
"* Create a simple multilayer perceptron model with pytorch\n",
|
||||
"* Visualise the parameters\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"### Task\n",
|
||||
"\n",
|
||||
"Build a fully connected feed forward network that adds two bits. Determine the a propper achitecture for this network (what database you use for this problem? how many layers? how many neurons on each layer? what is the activation function? what is the loss function? etc)\n",
|
||||
"\n",
|
||||
"Create at least 3 such networks and compare their performance (how accurate they are?, how farst they are trained to get at 1 accuracy?)\n",
|
||||
"\n",
|
||||
"Display for the best one the weights for each layer.\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 55,
|
||||
"id": "e3614e5f",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import torch\n",
|
||||
"import torch.nn as nn\n",
|
||||
"from collections import OrderedDict\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "eaf778c4",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### 1st Model"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 56,
|
||||
"id": "5ee7e7d7",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# your code here\n",
|
||||
"model = nn.Sequential(OrderedDict([\n",
|
||||
" ('hidden', nn.Linear(2,3)),\n",
|
||||
" ('sigmoid1', nn.Sigmoid()),\n",
|
||||
" ('output', nn.Linear(3,2)),\n",
|
||||
"]))"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 57,
|
||||
"id": "665ae958",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Sequential(\n",
|
||||
" (hidden): Linear(in_features=2, out_features=3, bias=True)\n",
|
||||
" (sigmoid1): Sigmoid()\n",
|
||||
" (output): Linear(in_features=3, out_features=2, bias=True)\n",
|
||||
")\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"print(model)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 58,
|
||||
"id": "e26f0d3e",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"tensor([[0., 0.],\n",
|
||||
" [0., 1.],\n",
|
||||
" [1., 0.],\n",
|
||||
" [1., 1.]])\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"# your code here\n",
|
||||
"data_in = torch.tensor([[0.,0.],[0.,1.],[1.,0.],[1.,1.]])\n",
|
||||
"print(data_in)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 59,
|
||||
"id": "4fb16bbc",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"tensor([[0., 0.],\n",
|
||||
" [0., 1.],\n",
|
||||
" [0., 1.],\n",
|
||||
" [1., 0.]])\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"# your code here\n",
|
||||
"data_target = torch.tensor([[0.,0.],[0.,1.],[0.,1.],[1.,0.]])\n",
|
||||
"print(data_target)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 60,
|
||||
"id": "69d920ed",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# your code here\n",
|
||||
"criterion = nn.L1Loss()\n",
|
||||
"optimizer = torch.optim.SGD(model.parameters(), lr=0.1)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 61,
|
||||
"id": "cde91f6f",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Epoch 1: Loss = 0.41974443197250366\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Epoch 1001: Loss = 0.3748631477355957\n",
|
||||
"Epoch 2001: Loss = 0.37467941641807556\n",
|
||||
"Epoch 3001: Loss = 0.37417155504226685\n",
|
||||
"Epoch 4001: Loss = 0.37154191732406616\n",
|
||||
"Epoch 5001: Loss = 0.3255583643913269\n",
|
||||
"Epoch 6001: Loss = 0.19477465748786926\n",
|
||||
"tensor([[-0., 0.],\n",
|
||||
" [-0., 1.],\n",
|
||||
" [-0., 1.],\n",
|
||||
" [1., 0.]], grad_fn=<RoundBackward0>)\n",
|
||||
"Model reached 100% accuracy at epoch 6449\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"# your code here\n",
|
||||
"# Train the model\n",
|
||||
"for epoch in range(10000):\n",
|
||||
" # reset the gradients to zero before each forward and backward pass\n",
|
||||
" optimizer.zero_grad() \n",
|
||||
" # forward pass\n",
|
||||
" outputs = model(data_in) \n",
|
||||
" \n",
|
||||
" # accuracy\n",
|
||||
" predicted = (outputs.round() == data_target)\n",
|
||||
" accuracy = predicted.sum().item() / (data_target == data_target).sum().item()\n",
|
||||
" if accuracy == 1:\n",
|
||||
" print(outputs.round())\n",
|
||||
" print(f\"Model reached 100% accuracy at epoch {epoch+1}\")\n",
|
||||
" break\n",
|
||||
" # calculate loss\n",
|
||||
" \n",
|
||||
" loss1 = criterion(outputs, data_target)\n",
|
||||
" if epoch % 1000 == 0:\n",
|
||||
" print(f'Epoch {epoch+1}: Loss = {loss1}')\n",
|
||||
" # backward pass\n",
|
||||
" loss1.backward() \n",
|
||||
" # update weights\n",
|
||||
" optimizer.step() "
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 62,
|
||||
"id": "dff3ec1a",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"accuracy: 100.0%\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"# your code here\n",
|
||||
"# visualize the resuts\n",
|
||||
"outputs = model(data_in)\n",
|
||||
"predicted = (outputs.round() == data_target)\n",
|
||||
"accuracy = predicted.sum().item() / (data_target == data_target).sum().item()\n",
|
||||
"print(f\"accuracy: {accuracy*100}%\")\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 63,
|
||||
"id": "c1a7518b",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"hidden.weight tensor([[ 3.6313, -3.4167],\n",
|
||||
" [ 1.3280, -0.5874],\n",
|
||||
" [ 0.3637, -2.2525]])\n",
|
||||
"hidden.bias tensor([ 2.5554, -0.1830, 0.7138])\n",
|
||||
"output.weight tensor([[ 1.7310, -0.1016, -2.1195],\n",
|
||||
" [-2.4398, 1.7537, 0.6634]])\n",
|
||||
"output.bias tensor([-0.1873, 1.1474])\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"# your code here\n",
|
||||
"# print model wights\n",
|
||||
"for name, param in model.named_parameters():\n",
|
||||
" print(name, param.data)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "5b569b90",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### 2nd Model"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 66,
|
||||
"id": "4cdf09ba",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Sequential(\n",
|
||||
" (hidden1): Linear(in_features=2, out_features=3, bias=True)\n",
|
||||
" (sigmoid1): Sigmoid()\n",
|
||||
" (hidden2): Linear(in_features=3, out_features=4, bias=True)\n",
|
||||
" (sigmoid2): Sigmoid()\n",
|
||||
" (output1): Linear(in_features=4, out_features=2, bias=True)\n",
|
||||
" (relu): ReLU()\n",
|
||||
")\n",
|
||||
"tensor([[0., 0.],\n",
|
||||
" [0., 1.],\n",
|
||||
" [1., 0.],\n",
|
||||
" [1., 1.]])\n",
|
||||
"tensor([[0., 0.],\n",
|
||||
" [0., 1.],\n",
|
||||
" [0., 1.],\n",
|
||||
" [1., 0.]])\n",
|
||||
"tensor([[0.0000, 0.0000],\n",
|
||||
" [0.0000, 1.0630],\n",
|
||||
" [0.0000, 1.0580],\n",
|
||||
" [0.0000, 0.0000]], grad_fn=<ReluBackward0>)\n",
|
||||
"accuracy: 87.5%\n",
|
||||
"hidden1.weight tensor([[3.7943, 3.9107],\n",
|
||||
" [1.3786, 1.1741],\n",
|
||||
" [0.5741, 0.9622]])\n",
|
||||
"hidden1.bias tensor([-0.3776, -1.5335, -0.2704])\n",
|
||||
"hidden2.weight tensor([[-2.0642, 1.3785, 0.4542],\n",
|
||||
" [ 2.6336, -1.9812, -1.4470],\n",
|
||||
" [-0.2936, -0.5167, 0.0780],\n",
|
||||
" [-1.2268, 1.2764, 0.6379]])\n",
|
||||
"hidden2.bias tensor([ 0.4410, -0.3201, -0.1641, -0.3203])\n",
|
||||
"output1.weight tensor([[ 0.0149, 0.3398, -0.2382, 0.0658],\n",
|
||||
" [-2.4774, 3.5136, 0.1576, -1.6128]])\n",
|
||||
"output1.bias tensor([-0.2175, 0.2636])\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"model = nn.Sequential(OrderedDict([\n",
|
||||
" ('hidden1', nn.Linear(2,3)),\n",
|
||||
" ('sigmoid1', nn.Sigmoid()),\n",
|
||||
" ('hidden2', nn.Linear(3,4)),\n",
|
||||
" ('sigmoid2', nn.Sigmoid()),\n",
|
||||
" ('output1', nn.Linear(4,2)),\n",
|
||||
" ('relu', nn.ReLU()),\n",
|
||||
"]))\n",
|
||||
"print(model)\n",
|
||||
"data_in = torch.tensor([[0.,0.],[0.,1.],[1.,0.],[1.,1.]])\n",
|
||||
"print(data_in)\n",
|
||||
"data_target = torch.tensor([[0.,0.],[0.,1.],[0.,1.],[1.,0.]])\n",
|
||||
"print(data_target)\n",
|
||||
"criterion = nn.L1Loss()\n",
|
||||
"optimizer = torch.optim.SGD(model.parameters(), lr=0.1)\n",
|
||||
"for epoch in range(10000):\n",
|
||||
" # reset the gradients to zero before each forward and backward pass\n",
|
||||
" optimizer.zero_grad() \n",
|
||||
" # forward pass\n",
|
||||
" outputs = model(data_in) \n",
|
||||
" \n",
|
||||
" # accuracy\n",
|
||||
" predicted = (outputs.round() == data_target)\n",
|
||||
" accuracy = predicted.sum().item() / (data_target == data_target).sum().item()\n",
|
||||
" if accuracy == 1:\n",
|
||||
" print(f\"Model 1 reached 100% accuracy at epoch {epoch+1}\")\n",
|
||||
" break\n",
|
||||
" # calculate loss\n",
|
||||
" loss1 = criterion(outputs, data_target)\n",
|
||||
" # backward pass\n",
|
||||
" loss1.backward() \n",
|
||||
" # update weights\n",
|
||||
" optimizer.step() \n",
|
||||
"outputs = model(data_in)\n",
|
||||
"print(outputs)\n",
|
||||
"predicted = (outputs.round() == data_target)\n",
|
||||
"accuracy = predicted.sum().item() / (data_target == data_target).sum().item()\n",
|
||||
"print(f\"accuracy: {accuracy*100}%\")\n",
|
||||
"for name, param in model.named_parameters():\n",
|
||||
" print(name, param.data)\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "7d95ac7a",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### 3th Model"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 69,
|
||||
"id": "d0bea66c",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Sequential(\n",
|
||||
" (hidden1): Linear(in_features=2, out_features=3, bias=True)\n",
|
||||
" (relu1): ReLU()\n",
|
||||
" (output1): Linear(in_features=3, out_features=2, bias=True)\n",
|
||||
")\n",
|
||||
"tensor([[0., 0.],\n",
|
||||
" [0., 1.],\n",
|
||||
" [1., 0.],\n",
|
||||
" [1., 1.]])\n",
|
||||
"tensor([[0., 0.],\n",
|
||||
" [0., 1.],\n",
|
||||
" [0., 1.],\n",
|
||||
" [1., 0.]])\n",
|
||||
"Epoch 1: Loss = 0.3959614038467407\n",
|
||||
"Epoch 101: Loss = 0.11625192314386368\n",
|
||||
"Epoch 201: Loss = 0.08679977804422379\n",
|
||||
"Epoch 301: Loss = 0.06458555907011032\n",
|
||||
"Epoch 401: Loss = 0.06256230920553207\n",
|
||||
"Epoch 501: Loss = 0.06250278651714325\n",
|
||||
"Epoch 601: Loss = 0.06250019371509552\n",
|
||||
"Epoch 701: Loss = 0.0625000074505806\n",
|
||||
"Epoch 801: Loss = 0.0625\n",
|
||||
"Epoch 901: Loss = 0.0625\n",
|
||||
"accuracy: 87.5%\n",
|
||||
"hidden1.weight tensor([[-0.0102, 0.3543],\n",
|
||||
" [ 0.3771, 1.0492],\n",
|
||||
" [-0.8659, 0.8689]])\n",
|
||||
"hidden1.bias tensor([-0.5027, -0.3777, -0.0030])\n",
|
||||
"output1.weight tensor([[-0.5325, 0.9537, -0.7395],\n",
|
||||
" [ 0.1828, -0.4768, 0.9471]])\n",
|
||||
"output1.bias tensor([-2.4464e-07, 5.0000e-01])\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"model = nn.Sequential(OrderedDict([\n",
|
||||
" ('hidden1', nn.Linear(2,3)),\n",
|
||||
" ('relu1', nn.ReLU()),\n",
|
||||
" ('output1', nn.Linear(3,2)),\n",
|
||||
"]))\n",
|
||||
"print(model)\n",
|
||||
"data_in = torch.tensor([[0.,0.],[0.,1.],[1.,0.],[1.,1.]])\n",
|
||||
"print(data_in)\n",
|
||||
"data_target = torch.tensor([[0.,0.],[0.,1.],[0.,1.],[1.,0.]])\n",
|
||||
"print(data_target)\n",
|
||||
"criterion = nn.MSELoss()\n",
|
||||
"optimizer = torch.optim.SGD(model.parameters(), lr=0.1)\n",
|
||||
"for epoch in range(1000):\n",
|
||||
" # reset the gradients to zero before each forward and backward pass\n",
|
||||
" optimizer.zero_grad() \n",
|
||||
" # forward pass\n",
|
||||
" outputs = model(data_in) \n",
|
||||
" \n",
|
||||
" # accuracy\n",
|
||||
" predicted = (outputs.round() == data_target)\n",
|
||||
" accuracy = predicted.sum().item() / (data_target == data_target).sum().item()\n",
|
||||
" if accuracy == 1:\n",
|
||||
" print(f\"Model 1 reached 100% accuracy at epoch {epoch+1}\")\n",
|
||||
" break\n",
|
||||
" # calculate loss\n",
|
||||
" loss1 = criterion(outputs, data_target)\n",
|
||||
" if epoch % 100 == 0:\n",
|
||||
" print(f'Epoch {epoch+1}: Loss = {loss1}')\n",
|
||||
" # backward pass\n",
|
||||
" loss1.backward() \n",
|
||||
" # update weights\n",
|
||||
" optimizer.step() \n",
|
||||
"outputs = model(data_in)\n",
|
||||
"predicted = (outputs.round() == data_target)\n",
|
||||
"accuracy = predicted.sum().item() / (data_target == data_target).sum().item()\n",
|
||||
"print(f\"accuracy: {accuracy*100}%\")\n",
|
||||
"for name, param in model.named_parameters():\n",
|
||||
" print(name, param.data)\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "e29c65a2",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3 (ipykernel)",
|
||||
"language": "python",
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"codemirror_mode": {
|
||||
"name": "ipython",
|
||||
"version": 3
|
||||
},
|
||||
"file_extension": ".py",
|
||||
"mimetype": "text/x-python",
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.12.2"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 5
|
||||
}
|
||||
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
@@ -0,0 +1,635 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"attachments": {},
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"# Lab 8: Evolutionary computation\n",
|
||||
"\n",
|
||||
"### Consider the following example:\n",
|
||||
"\n",
|
||||
"Determine the minimum of the function $f(x)= x_1^2+...+x_n^2$ with $x_i \\in [-5.12, 5.12]$, $i \\in \\overline{(1, n)}$\n",
|
||||
"\n",
|
||||
"We have an example of steady state genetic algorithm with: representation an array of real numbers; 100 individuals; crossover $$child = \\alpha \\cdot (parent1 - parent2) + parent2 ;$$ mutation - reinitialise on a random position the individual's value."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 29,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Result: The detected minimum point after 10000 iterations is f(-0.00 -0.00) = 0.00\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"\n",
|
||||
"\n",
|
||||
"from random import randint, random\n",
|
||||
"from operator import add\n",
|
||||
"from math import cos, pi\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"def individual(length, vmin, vmax):\n",
|
||||
" '''\n",
|
||||
" Create a member of the population - an individual\n",
|
||||
"\n",
|
||||
" length: the number of genes (components)\n",
|
||||
" vmin: the minimum possible value \n",
|
||||
" vmax: the maximum possible value \n",
|
||||
" '''\n",
|
||||
" return [ (random()*(vmax-vmin)+vmin) for x in range(length) ]\n",
|
||||
" \n",
|
||||
"def population(count, length, vmin, vmax):\n",
|
||||
" \"\"\"\n",
|
||||
" Create a number of individuals (i.e. a population).\n",
|
||||
"\n",
|
||||
" count: the number of individuals in the population\n",
|
||||
" length: the number of values per individual\n",
|
||||
" vmin: the minimum possible value \n",
|
||||
" vmax: the maximum possible value \n",
|
||||
" \"\"\"\n",
|
||||
" return [ individual(length, vmin, vmax) for x in range(count) ]\n",
|
||||
"\n",
|
||||
"def fitness(individual):\n",
|
||||
" \"\"\"\n",
|
||||
" Determine the fitness of an individual. Lower is better.(min problem)\n",
|
||||
" For this problem we have the Rastrigin function\n",
|
||||
" \n",
|
||||
" individual: the individual to evaluate\n",
|
||||
" \"\"\"\n",
|
||||
" n=len(individual)\n",
|
||||
" f=0;\n",
|
||||
" for i in range(n):\n",
|
||||
" f=f+individual[i]*individual[i]\n",
|
||||
" return f\n",
|
||||
" \n",
|
||||
"def mutate(individual, pM, vmin, vmax): \n",
|
||||
" '''\n",
|
||||
" Performs a mutation on an individual with the probability of pM.\n",
|
||||
" If the event will take place, at a random position a new value will be\n",
|
||||
" generated in the interval [vmin, vmax]\n",
|
||||
"\n",
|
||||
" individual:the individual to be mutated\n",
|
||||
" pM: the probability the mutation to occure\n",
|
||||
" vmin: the minimum possible value \n",
|
||||
" vmax: the maximum possible value\n",
|
||||
" '''\n",
|
||||
" if pM > random():\n",
|
||||
" p = randint(0, len(individual)-1)\n",
|
||||
" individual[p] = random()*(vmax-vmin)+vmin\n",
|
||||
" return individual\n",
|
||||
" \n",
|
||||
"def crossover(parent1, parent2):\n",
|
||||
" '''\n",
|
||||
" crossover between 2 parents\n",
|
||||
" '''\n",
|
||||
" child=[]\n",
|
||||
" alpha=random()\n",
|
||||
" for x in range(len(parent1)):\n",
|
||||
" child.append(alpha*(parent1[x]-parent2[x])+parent2[x])\n",
|
||||
" return child\n",
|
||||
"\n",
|
||||
"def iteration(pop, pM, vmin, vmax):\n",
|
||||
" '''\n",
|
||||
" an iteration\n",
|
||||
"\n",
|
||||
" pop: the current population\n",
|
||||
" pM: the probability the mutation to occure\n",
|
||||
" vmin: the minimum possible value \n",
|
||||
" vmax: the maximum possible value\n",
|
||||
" '''\n",
|
||||
" i1=randint(0,len(pop)-1)\n",
|
||||
" i2=randint(0,len(pop)-1)\n",
|
||||
" if (i1!=i2):\n",
|
||||
" c=crossover(pop[i1],pop[i2])\n",
|
||||
" c=mutate(c, pM, vmin, vmax)\n",
|
||||
" f1=fitness(pop[i1])\n",
|
||||
" f2=fitness(pop[i2])\n",
|
||||
" '''\n",
|
||||
" the repeated evaluation of the parents can be avoided\n",
|
||||
" if next to the values stored in the individuals we \n",
|
||||
" keep also their fitnesses \n",
|
||||
" '''\n",
|
||||
" fc=fitness(c)\n",
|
||||
" if(f1>f2) and (f1>fc):\n",
|
||||
" pop[i1]=c\n",
|
||||
" if(f2>f1) and (f2>fc):\n",
|
||||
" pop[i2]=c\n",
|
||||
" return pop\n",
|
||||
"\n",
|
||||
"def main(noIteratii=10000):\n",
|
||||
" #PARAMETERS:\n",
|
||||
" \n",
|
||||
" #population size\n",
|
||||
" dimPopulation = 100\n",
|
||||
" #individual size\n",
|
||||
" dimIndividual = 2\n",
|
||||
" #the boundries of the search interval\n",
|
||||
" vmin = -5.12\n",
|
||||
" vmax = 5.12\n",
|
||||
" #the mutation probability\n",
|
||||
" pM=0.01\n",
|
||||
" \n",
|
||||
" P = population(dimPopulation, dimIndividual, vmin, vmax)\n",
|
||||
" for i in range(noIteratii):\n",
|
||||
" P = iteration(P, pM, vmin, vmax)\n",
|
||||
"\n",
|
||||
" #print the best individual\n",
|
||||
" graded = [ (fitness(x), x) for x in P]\n",
|
||||
" graded = sorted(graded)\n",
|
||||
" result=graded[0]\n",
|
||||
" fitnessOptim=result[0]\n",
|
||||
" individualOptim=result[1]\n",
|
||||
" print('Result: The detected minimum point after %d iterations is f(%3.2f %3.2f) = %3.2f'% \\\n",
|
||||
" (noIteratii,individualOptim[0],individualOptim[1], fitnessOptim) )\n",
|
||||
" \n",
|
||||
"main()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"attachments": {},
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Exercise 1: Construct a similar algorithm to the one provided as an example for the Bukin function N.6 (search the internet for this function).\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 25,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Result: The detected minimum point after 10000 iterations is f(-9.81 0.96) = 0.00\n",
|
||||
"0.0\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"# your code here\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"from random import randint, random\n",
|
||||
"from operator import add\n",
|
||||
"from math import cos, pi\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"def individual():\n",
|
||||
" return [ (random()*(-5+15)-15), (random()*(3+3)-3)]\n",
|
||||
" \n",
|
||||
"def population(count):\n",
|
||||
" return [ individual() for x in range(count) ]\n",
|
||||
"\n",
|
||||
"def fitness(individual):\n",
|
||||
" return (100 *((abs(individual[1] - 0.01*(individual[0]**2)))**(1/2))) + (0.01 * abs(individual[0] + 10))\n",
|
||||
" \n",
|
||||
"def mutate(individual, pM): \n",
|
||||
" if pM > random():\n",
|
||||
" p = randint(0, 1)\n",
|
||||
" if p == 0:\n",
|
||||
" individual[0] = random()*(-5+15)-15\n",
|
||||
" else:\n",
|
||||
" individual[1] = random()*(3+3)-3\n",
|
||||
" return individual\n",
|
||||
" \n",
|
||||
"def crossover(parent1, parent2):\n",
|
||||
" child=[]\n",
|
||||
" alpha=random()\n",
|
||||
" for x in range(len(parent1)):\n",
|
||||
" child.append(alpha*(parent1[x]-parent2[x])+parent2[x])\n",
|
||||
" return child\n",
|
||||
"\n",
|
||||
"def iteration(pop, pM):\n",
|
||||
" i1=randint(0,len(pop)-1)\n",
|
||||
" i2=randint(0,len(pop)-1)\n",
|
||||
" if (i1!=i2):\n",
|
||||
" c=crossover(pop[i1],pop[i2])\n",
|
||||
" c=mutate(c, pM)\n",
|
||||
" f1=fitness(pop[i1])\n",
|
||||
" f2=fitness(pop[i2])\n",
|
||||
" fc=fitness(c)\n",
|
||||
" if(f1>f2) and (f1>fc):\n",
|
||||
" pop[i1]=c\n",
|
||||
" if(f2>f1) and (f2>fc):\n",
|
||||
" pop[i2]=c\n",
|
||||
" return pop\n",
|
||||
"\n",
|
||||
"def main(noIteratii=10000):\n",
|
||||
" #PARAMETERS:\n",
|
||||
" \n",
|
||||
" #population size\n",
|
||||
" dimPopulation = 100\n",
|
||||
" #the mutation probability\n",
|
||||
" pM=0.01\n",
|
||||
" \n",
|
||||
" P = population(dimPopulation)\n",
|
||||
" for i in range(noIteratii):\n",
|
||||
" P = iteration(P, pM)\n",
|
||||
"\n",
|
||||
" #print the best individual\n",
|
||||
" graded = [ (fitness(x), x) for x in P]\n",
|
||||
" graded = sorted(graded)\n",
|
||||
" result=graded[0]\n",
|
||||
" fitnessOptim=result[0]\n",
|
||||
" individualOptim=result[1]\n",
|
||||
" print('Result: The detected minimum point after %d iterations is f(%3.2f %3.2f) = %3.2f'% \\\n",
|
||||
" (noIteratii,individualOptim[0],individualOptim[1], fitnessOptim) )\n",
|
||||
" print(fitness((-10,1)))\n",
|
||||
"main()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"attachments": {},
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Consider the knapsack problem:\n",
|
||||
"\n",
|
||||
"Consider a Knapsack with a total volum equal with $V_{max}$.\n",
|
||||
"\n",
|
||||
"There are $n$ objects, with values $(p_i)_{n}$ and volumes $(v_i)_n$.\n",
|
||||
"\n",
|
||||
"Solve this problem using a generationist Genetic Algorithm, with a binary representation.\n",
|
||||
"\n",
|
||||
"Exercise 2: Initialization\n",
|
||||
"Objective: Implement the initialization step of a genetic algorithm."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 34,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"[[0 1 0 0 1 1 1 0]\n",
|
||||
" [0 1 0 1 0 1 0 0]\n",
|
||||
" [0 1 0 1 0 1 0 1]\n",
|
||||
" [1 0 0 0 0 0 0 1]\n",
|
||||
" [0 1 0 1 0 1 1 1]\n",
|
||||
" [0 1 0 1 0 1 1 1]\n",
|
||||
" [1 1 0 0 1 1 0 0]\n",
|
||||
" [1 0 0 0 0 0 1 0]\n",
|
||||
" [1 0 0 1 0 0 1 0]\n",
|
||||
" [1 1 1 1 0 0 0 0]]\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"import random\n",
|
||||
"import numpy as np\n",
|
||||
"\n",
|
||||
"def individual(chromosome_length):\n",
|
||||
" return np.array([randint(0,1) for _ in range(chromosome_length)])\n",
|
||||
"\n",
|
||||
"def initialize_population(population_size, chromosome_length):\n",
|
||||
" # generate random a population with population_size number of individuals\n",
|
||||
" # each individual with the size chromosome_length\n",
|
||||
" # IN: population_size, chromosome_length\n",
|
||||
" # OUT: population\n",
|
||||
" return np.array([individual(chromosome_length) for _ in range(population_size)])\n",
|
||||
" # your code here\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"# Test the initialization step\n",
|
||||
"population_size = 10\n",
|
||||
"chromosome_length = 8\n",
|
||||
"weights = np.array([1, 4, 5, 7, 3, 2, 4 ,1])\n",
|
||||
"values = np.array([2, 3, 1, 4, 2, 3, 4, 5])\n",
|
||||
"limit = 14\n",
|
||||
"population = initialize_population(population_size, chromosome_length)\n",
|
||||
"print(population)\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"attachments": {},
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Exercise 3: Fitness Evaluation\n",
|
||||
"\n",
|
||||
"Objective: Implement the fitness evaluation step of a genetic algorithm."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 35,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"[12 10 15 7 0 0 10 6 10 0]\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"def evaluate_fitness(population):\n",
|
||||
" # evaluate the fitness of each individual in the population\n",
|
||||
" # IN: population\n",
|
||||
" # OUT: fitness_scores\n",
|
||||
" # your code here\n",
|
||||
" total_weights = (weights * population).sum(axis=1)\n",
|
||||
" scores = (values * population).sum(axis=1)\n",
|
||||
" scores[total_weights > limit] = 0\n",
|
||||
" return scores\n",
|
||||
" \n",
|
||||
"# Test the fitness evaluation step\n",
|
||||
"fitness_scores = evaluate_fitness(population)\n",
|
||||
"print(fitness_scores)\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"attachments": {},
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Exercise 4: Selection\n",
|
||||
"\n",
|
||||
"Objective: Implement the selection step of a genetic algorithm."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 36,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"[[0 1 0 1 0 1 0 0]\n",
|
||||
" [0 1 0 1 0 1 0 1]]\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"def select_parents(population, fitness_scores):\n",
|
||||
" # select two parents from the population based on the fitness - \n",
|
||||
" # the better the fitness, the higher the chance to be selected\n",
|
||||
" # IN: population, fitness_scores\n",
|
||||
" # OUT: selected_parents\n",
|
||||
" # your code here\n",
|
||||
" probs = ((fitness_scores + 1) / (fitness_scores + 1).sum())\n",
|
||||
" indexes = np.random.choice(population.shape[0], size = 2, replace=False, p=probs)\n",
|
||||
" return population[indexes]\n",
|
||||
" \n",
|
||||
"# Test the selection step\n",
|
||||
"parents = select_parents(population, fitness_scores)\n",
|
||||
"print(parents)\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"attachments": {},
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Exercise 5: Crossover\n",
|
||||
"\n",
|
||||
"Objective: Implement the crossover step of a genetic algorithm."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 39,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"[0. 1. 0. 1. 0. 1. 0. 1.]\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"def crossover(parents):\n",
|
||||
" # create new offspring by combining the parents\n",
|
||||
" # IN: parents\n",
|
||||
" # OUT: offspring\n",
|
||||
"\n",
|
||||
" # your code here\n",
|
||||
" offspring = np.zeros((parents.shape[1],))\n",
|
||||
" for i in range(parents.shape[1]):\n",
|
||||
" if parents[0][i] == parents[1][i]:\n",
|
||||
" offspring[i] = parents[0][i]\n",
|
||||
" else:\n",
|
||||
" offspring[i] = random.randint(0, 1)\n",
|
||||
" return offspring\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"# Test the crossover step\n",
|
||||
"offspring = crossover(parents)\n",
|
||||
"print(offspring)\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"attachments": {},
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Exercise 6: Mutation\n",
|
||||
"\n",
|
||||
"Objective: Implement the mutation step of a genetic algorithm."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 40,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"[0.0, 1.0, 0.0, 1.0, 0.0, 1.0, 0.0, 1.0]\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"def mutate(chromosome, mutation_rate):\n",
|
||||
" # mutate the chromosome by randomly flipping bits\n",
|
||||
" # IN: chromosome, mutation_rate\n",
|
||||
" # OUT: mutated_chromosome\n",
|
||||
"\n",
|
||||
" # your code here\n",
|
||||
" if random.random() <= mutation_rate:\n",
|
||||
" for i in range(len(chromosome)):\n",
|
||||
" chromosome[i] = random.randint(0, 1)\n",
|
||||
" return chromosome\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"# Test the mutation step\n",
|
||||
"mutation_rate = 0.1\n",
|
||||
"mutated_offspring = [mutate(child, mutation_rate) for child in offspring]\n",
|
||||
"print(mutated_offspring)\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"attachments": {},
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Exercise 7: Complete Genetic Algorithm\n",
|
||||
"\n",
|
||||
"Objective: Combine all the steps of a genetic algorithm to solve a specific problem."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 56,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"[[1 1 0 0 0 1 1 1]\n",
|
||||
" [1 1 0 0 0 1 1 1]\n",
|
||||
" [1 1 0 0 0 1 1 1]\n",
|
||||
" [1 1 0 0 0 1 1 1]\n",
|
||||
" [1 1 0 0 0 1 1 1]\n",
|
||||
" [1 1 0 0 0 1 1 1]\n",
|
||||
" [1 1 0 0 0 1 1 1]\n",
|
||||
" [1 1 0 0 0 1 1 1]\n",
|
||||
" [1 1 0 0 0 1 1 1]\n",
|
||||
" [1 1 0 0 0 1 1 1]]\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"def genetic_algorithm(population_size, chromosome_length, generations, mutation_rate):\n",
|
||||
" \n",
|
||||
" # complete genetic algorithm\n",
|
||||
" # IN: population_size, chromosome_length, generations, mutation_rate\n",
|
||||
" # OUT: population\n",
|
||||
"\n",
|
||||
"\n",
|
||||
" # initialize the population\n",
|
||||
" population = initialize_population(population_size, chromosome_length)\n",
|
||||
" # your code here\n",
|
||||
" \n",
|
||||
"\n",
|
||||
" for _ in range(generations):\n",
|
||||
" # Fitness evaluation\n",
|
||||
" # your code here\n",
|
||||
" fitnesses = evaluate_fitness(population)\n",
|
||||
" \n",
|
||||
" # Selection\n",
|
||||
" \n",
|
||||
" # your code here\n",
|
||||
" parents = select_parents(population, fitnesses)\n",
|
||||
"\n",
|
||||
" # Crossover\n",
|
||||
"\n",
|
||||
" # your code here\n",
|
||||
" child = crossover(parents)\n",
|
||||
"\n",
|
||||
" # Mutation\n",
|
||||
" \n",
|
||||
" # your code here\n",
|
||||
" mutated_child = mutate(child, mutation_rate)\n",
|
||||
"\n",
|
||||
" # Replace the population with the new generation\n",
|
||||
" \n",
|
||||
" # your code here\n",
|
||||
" minimum = fitnesses.min()\n",
|
||||
" for i in range(fitnesses.shape[0]):\n",
|
||||
" if minimum == fitnesses[i]:\n",
|
||||
" population[i] = mutated_child\n",
|
||||
" break\n",
|
||||
"\n",
|
||||
" return population\n",
|
||||
"\n",
|
||||
"# Test the complete genetic algorithm\n",
|
||||
"population_size = 10\n",
|
||||
"chromosome_length = 8\n",
|
||||
"generations = 100\n",
|
||||
"mutation_rate = 0.1\n",
|
||||
"\n",
|
||||
"final_population = genetic_algorithm(population_size, chromosome_length, generations, mutation_rate)\n",
|
||||
"print(final_population)\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"attachments": {},
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Exercise 8: Extract the result from the final population\n",
|
||||
"\n",
|
||||
"Objective: Get the best individual from the final population.\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 57,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Maximum is 17\n",
|
||||
"Chosen values [2 3 0 0 0 3 4 5] with weights [1 4 0 0 0 2 4 1]\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"# determine the best individual from the final population and print it out\n",
|
||||
"\n",
|
||||
"# your code here\n",
|
||||
"\n",
|
||||
"fitnesses = evaluate_fitness(final_population)\n",
|
||||
"maximum = fitnesses.max()\n",
|
||||
"print(f\"Maximum is {maximum}\")\n",
|
||||
"for i in range(0, fitnesses.shape[0]):\n",
|
||||
" if fitnesses[i] == maximum:\n",
|
||||
" print(f\"Chosen values {final_population[i] * values} with weights {final_population[i] * weights}\")\n",
|
||||
" break"
|
||||
]
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernel_info": {
|
||||
"name": "python"
|
||||
},
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3",
|
||||
"language": "python",
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"codemirror_mode": {
|
||||
"name": "ipython",
|
||||
"version": 3
|
||||
},
|
||||
"file_extension": ".py",
|
||||
"mimetype": "text/x-python",
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.12.2"
|
||||
},
|
||||
"nteract": {
|
||||
"version": "nteract-front-end@1.0.0"
|
||||
},
|
||||
"orig_nbformat": 4
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 2
|
||||
}
|
||||
@@ -0,0 +1,198 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"attachments": {},
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Lab. 12\n",
|
||||
"\n",
|
||||
"### Solve the following problem using Genetic Algorithms:\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"Problem: Weighted N-Queen Problem\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"You are given an N×N chessboard, and each cell of the board has an associated weight. Your task is to find a valid placement of N queens such that the total weight of the queens is maximized, and no two queens threaten each other.\n",
|
||||
"\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"attachments": {},
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"\n",
|
||||
"\n",
|
||||
"In the traditional N-Queen Problem, the goal is to place N queens on an N×N chessboard in such a way that no two queens threaten each other. In this variation, we introduce weights to the queens and aim to find a placement that maximizes the total weight of the queens while satisfying the constraint of non-threatening positions.\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"Constraints:\n",
|
||||
"\n",
|
||||
"1. There should be exactly one queen in each row and each column.\n",
|
||||
"2. No two queens should be placed in the same diagonal, i.e., they should not threaten each other.\n",
|
||||
"3. The placement should maximize the total weight of the queens.\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"Representation:\n",
|
||||
"\n",
|
||||
"Use a permutation-based representation. Each permutation represents the column position of the queen for each row. \n",
|
||||
"\n",
|
||||
"For example, if N=4, a valid permutation [2, 4, 1, 3] indicates that the queen in the first row is placed in column 2, the queen in the second row is placed in column 4, and so on.\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"Genetic Algorithm Steps:\n",
|
||||
"\n",
|
||||
"1. *Initialization*: Generate an initial population of permutations randomly.\n",
|
||||
"\n",
|
||||
"2. *Fitness Evaluation*: Evaluate the fitness of each permutation by calculating the total weight of the queens while considering the non-threatening positions.\n",
|
||||
"\n",
|
||||
"3. *Selection*: Select a subset of permutations from the population based on their fitness, using selection techniques like tournament selection or roulette wheel selection.\n",
|
||||
"\n",
|
||||
"4. *Crossover*: Perform crossover (recombination) on the selected permutations to create new offspring permutations.\n",
|
||||
"\n",
|
||||
"5. *Mutation*: Introduce random changes (mutations) in the offspring permutations to maintain diversity in the population.\n",
|
||||
"\n",
|
||||
"6. *Fitness Evaluation for the new individuals*: Evaluate the fitness of the new population.\n",
|
||||
"\n",
|
||||
"7. *Form the new population*: Select the surviving individuals based on scores, with chances direct proportional with their performance.\n",
|
||||
"\n",
|
||||
"8. Repeat steps 3-7 for a certain number of generations or until a termination condition is met (e.g., a maximum number of iterations or a satisfactory solution is found).\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"9. *Termination*: Return the best-performing individual (permutation) found as the solution to the problem.\n",
|
||||
"\n",
|
||||
"Note: The fitness function used in this problem should calculate the total weight of the queens based on the positions specified by the permutation. Additionally, the fitness function should penalize solutions that violate the non-threatening constraint by assigning a lower fitness score to such permutations."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 223,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"[[0 1 2 3]\n",
|
||||
" [1 0 4 5]\n",
|
||||
" [2 4 0 6]\n",
|
||||
" [3 5 6 0]]\n",
|
||||
"[[3 0 1 2]\n",
|
||||
" [3 1 2 0]\n",
|
||||
" [3 1 2 0]\n",
|
||||
" [0 1 2 3]\n",
|
||||
" [2 0 1 3]]\n",
|
||||
"[2 0 3 1]\n",
|
||||
"14\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"import numpy as np\n",
|
||||
"import random\n",
|
||||
"\n",
|
||||
"np.random.seed(40)\n",
|
||||
"\n",
|
||||
"n = 4\n",
|
||||
"weights = np.array([[0, 1, 2, 3], [1, 0, 4, 5], [2, 4, 0, 6], [3, 5, 6, 0]])\n",
|
||||
"\n",
|
||||
"print(weights)\n",
|
||||
"\n",
|
||||
"def initialization(chromosome_size: int, population_size: int) -> np.ndarray:\n",
|
||||
" return np.array([np.random.permutation(chromosome_size) for _ in range(population_size)])\n",
|
||||
"\n",
|
||||
"def fitness(chromosome: np.ndarray, weights: np.ndarray) -> int:\n",
|
||||
" score = 0\n",
|
||||
" for i in range(len(chromosome)):\n",
|
||||
" if not is_attacked(chromosome, [i, chromosome[i]]):\n",
|
||||
" score += weights[i, chromosome[i]]\n",
|
||||
" return score\n",
|
||||
"\n",
|
||||
"def is_attacked(chromosome: np.ndarray, queen: list) -> bool:\n",
|
||||
" for i in range(len(chromosome)):\n",
|
||||
" if i == queen[0]:\n",
|
||||
" continue\n",
|
||||
" if abs(i - queen[0]) == abs(chromosome[i] - queen[1]):\n",
|
||||
" return True\n",
|
||||
" return False\n",
|
||||
"\n",
|
||||
"def selection(population: np.ndarray, weights: np.ndarray) -> np.ndarray:\n",
|
||||
" return random.choices(population, weights=[fitness(chromosome, weights)+1 for chromosome in population], k=2)\n",
|
||||
"\n",
|
||||
"def crossover(parent1: np.ndarray, parent2: np.ndarray) -> np.ndarray:\n",
|
||||
" point1, point2 = sorted(np.random.choice(len(parent1)+1, 2, replace=False))\n",
|
||||
" offspring = np.full_like(parent1, -1)\n",
|
||||
" offspring[point1:point2] = parent1[point1:point2]\n",
|
||||
"\n",
|
||||
" idx = 0\n",
|
||||
" while idx != len(offspring) and offspring[idx] != -1:\n",
|
||||
" idx += 1\n",
|
||||
" for gene in parent2:\n",
|
||||
" if gene not in offspring:\n",
|
||||
" offspring[idx] = gene\n",
|
||||
" while idx != len(offspring) and offspring[idx] != -1:\n",
|
||||
" idx += 1\n",
|
||||
" return offspring\n",
|
||||
"\n",
|
||||
"def mutation(chromosome: np.ndarray, probability: np.float32) -> np.ndarray:\n",
|
||||
" if random.random() < probability:\n",
|
||||
" point1, point2 = np.random.choice(len(chromosome), 2, replace=False)\n",
|
||||
" chromosome[point1], chromosome[point2] = chromosome[point2], chromosome[point1]\n",
|
||||
" return chromosome\n",
|
||||
"\n",
|
||||
"def genetic_algorithm(n: int, weights: np.ndarray, population_size: int, generations: int, mutation_probability: np.float32) -> np.ndarray:\n",
|
||||
" population = initialization(n, population_size)\n",
|
||||
" print(population)\n",
|
||||
" for _ in range(generations):\n",
|
||||
" parent1, parent2 = selection(population, weights)\n",
|
||||
" offspring = crossover(parent1, parent2)\n",
|
||||
" offspring = mutation(offspring, mutation_probability)\n",
|
||||
" if fitness(offspring, weights) > fitness(parent1, weights):\n",
|
||||
" population[np.where((population == parent1).all(axis=1))[0][0]] = offspring\n",
|
||||
" elif fitness(offspring, weights) > fitness(parent2, weights):\n",
|
||||
" population[np.where((population == parent2).all(axis=1))[0][0]] = offspring\n",
|
||||
" return max(population, key=lambda x: fitness(x, weights))\n",
|
||||
" \n",
|
||||
"\n",
|
||||
"# print(initialization(n, 5))\n",
|
||||
"# print(fitness(np.array([2, 0, 3, 1]), weights))\n",
|
||||
"# print(crossover(np.array([1, 3, 0, 2]), np.array([1, 3, 0, 2])))\n",
|
||||
"\n",
|
||||
"champion = genetic_algorithm(n, weights, 5, 100, 0.6)\n",
|
||||
"print(champion)\n",
|
||||
"print(fitness(champion, weights))\n",
|
||||
"\n",
|
||||
"\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernel_info": {
|
||||
"name": "python"
|
||||
},
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3",
|
||||
"language": "python",
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"codemirror_mode": {
|
||||
"name": "ipython",
|
||||
"version": 3
|
||||
},
|
||||
"file_extension": ".py",
|
||||
"mimetype": "text/x-python",
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.12.2"
|
||||
},
|
||||
"nteract": {
|
||||
"version": "nteract-front-end@1.0.0"
|
||||
},
|
||||
"orig_nbformat": 4
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 2
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
,YearsExperience,Salary
|
||||
0,1.2000000000000002,39344.0
|
||||
1,1.4000000000000001,46206.0
|
||||
2,1.6,37732.0
|
||||
3,2.1,43526.0
|
||||
4,2.3000000000000003,39892.0
|
||||
5,3.0,56643.0
|
||||
6,3.1,60151.0
|
||||
7,3.3000000000000003,54446.0
|
||||
8,3.3000000000000003,64446.0
|
||||
9,3.8000000000000003,57190.0
|
||||
10,4.0,63219.0
|
||||
11,4.1,55795.0
|
||||
12,4.1,56958.0
|
||||
13,4.199999999999999,57082.0
|
||||
14,4.6,61112.0
|
||||
15,5.0,67939.0
|
||||
16,5.199999999999999,66030.0
|
||||
17,5.3999999999999995,83089.0
|
||||
18,6.0,81364.0
|
||||
19,6.1,93941.0
|
||||
20,6.8999999999999995,91739.0
|
||||
21,7.199999999999999,98274.0
|
||||
22,8.0,101303.0
|
||||
23,8.299999999999999,113813.0
|
||||
24,8.799999999999999,109432.0
|
||||
25,9.1,105583.0
|
||||
26,9.6,116970.0
|
||||
27,9.7,112636.0
|
||||
28,10.4,122392.0
|
||||
29,10.6,121873.0
|
||||
|
File diff suppressed because it is too large
Load Diff
Binary file not shown.
|
After Width: | Height: | Size: 141 KiB |
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,154 @@
|
||||
sepal length (cm),sepal width (cm),petal length (cm),petal width (cm),iris_name
|
||||
5.1,3.5,1.4,0.2,setosa
|
||||
4.9,3,1.4,0.2,setosa
|
||||
4.7,3.2,1.3,0.2,setosa
|
||||
4.6,3.1,1.5,0.2,setosa
|
||||
5,3.6,1.4,0.2,setosa
|
||||
5.4,3.9,1.7,0.4,setosa
|
||||
4.6,3.4,1.4,0.3,setosa
|
||||
5,3.4,1.5,0.2,setosa
|
||||
4.4,2.9,1.4,0.2,setosa
|
||||
4.9,3.1,1.5,0.1,setosa
|
||||
5.4,3.7,1.5,0.2,setosa
|
||||
4.8,3.4,1.6,0.2,setosa
|
||||
4.8,3,1.4,0.1,setosa
|
||||
4.3,3,1.1,0.1,setosa
|
||||
5.8,4,1.2,0.2,setosa
|
||||
5.7,4.4,1.5,0.4,setosa
|
||||
5.4,3.9,1.3,0.4,setosa
|
||||
5.1,3.5,1.4,0.3,setosa
|
||||
5.7,3.8,1.7,0.3,setosa
|
||||
5.1,3.8,1.5,0.3,setosa
|
||||
5.4,3.4,1.7,0.2,setosa
|
||||
5.1,3.7,1.5,0.4,setosa
|
||||
4.6,3.6,1,0.2,setosa
|
||||
4.6,3.6,1,,setosa
|
||||
5.1,3.3,1.7,0.5,setosa
|
||||
4.8,3.4,1.9,0.2,setosa
|
||||
5,3,1.6,0.2,setosa
|
||||
5,3.4,1.6,0.4,setosa
|
||||
5.2,3.5,1.5,0.2,setosa
|
||||
5.2,3.4,1.4,0.2,setosa
|
||||
4.7,3.2,1.6,0.2,setosa
|
||||
4.8,3.1,1.6,0.2,setosa
|
||||
5.4,3.4,1.5,0.4,setosa
|
||||
5.2,4.1,1.5,0.1,setosa
|
||||
5.5,4.2,1.4,0.2,setosa
|
||||
4.9,3.1,1.5,0.2,setosa
|
||||
5,3.2,1.2,0.2,setosa
|
||||
5.5,3.5,1.3,0.2,setosa
|
||||
4.9,3.6,1.4,0.1,setosa
|
||||
4.4,3,1.3,0.2,setosa
|
||||
5.1,3.4,1.5,0.2,setosa
|
||||
,3,1.3,0.2,
|
||||
5,3.5,1.3,0.3,setosa
|
||||
4.5,2.3,1.3,0.3,setosa
|
||||
4.4,3.2,1.3,0.2,setosa
|
||||
5,3.5,1.6,0.6,setosa
|
||||
5.1,3.8,1.9,0.4,setosa
|
||||
4.8,3,1.4,0.3,setosa
|
||||
5.1,3.8,1.6,0.2,setosa
|
||||
4.6,3.2,1.4,0.2,setosa
|
||||
5.3,3.7,1.5,0.2,setosa
|
||||
5,3.3,1.4,0.2,setosa
|
||||
7,3.2,4.7,1.4,versicolor
|
||||
6.4,3.2,4.5,1.5,versicolor
|
||||
6.4,,4.5,1.5,versicolor
|
||||
6.9,3.1,4.9,1.5,versicolor
|
||||
5.5,2.3,4,1.3,versicolor
|
||||
6.5,2.8,4.6,1.5,versicolor
|
||||
5.7,2.8,4.5,1.3,versicolor
|
||||
6.3,3.3,4.7,1.6,versicolor
|
||||
4.9,2.4,3.3,1,versicolor
|
||||
6.6,2.9,4.6,1.3,versicolor
|
||||
5.2,2.7,3.9,1.4,versicolor
|
||||
5,2,3.5,1,versicolor
|
||||
5.9,3,4.2,1.5,versicolor
|
||||
6,2.2,4,1,versicolor
|
||||
6.1,2.9,4.7,1.4,versicolor
|
||||
5.6,2.9,3.6,1.3,versicolor
|
||||
6.7,3.1,4.4,1.4,versicolor
|
||||
5.6,3,4.5,1.5,versicolor
|
||||
5.8,2.7,4.1,1,versicolor
|
||||
6.2,2.2,4.5,1.5,versicolor
|
||||
5.6,2.5,3.9,1.1,versicolor
|
||||
5.9,3.2,4.8,1.8,versicolor
|
||||
6.1,2.8,4,1.3,versicolor
|
||||
6.3,2.5,4.9,1.5,versicolor
|
||||
6.1,2.8,4.7,1.2,versicolor
|
||||
6.4,2.9,4.3,1.3,versicolor
|
||||
6.6,3,4.4,1.4,versicolor
|
||||
6.8,2.8,4.8,1.4,versicolor
|
||||
6.7,3,5,1.7,versicolor
|
||||
6,2.9,4.5,1.5,versicolor
|
||||
5.7,2.6,3.5,1,versicolor
|
||||
5.5,2.4,3.8,1.1,versicolor
|
||||
5.5,2.4,3.7,1,versicolor
|
||||
5.8,2.7,3.9,1.2,versicolor
|
||||
6,2.7,5.1,1.6,versicolor
|
||||
5.4,3,4.5,1.5,versicolor
|
||||
6,3.4,4.5,1.6,versicolor
|
||||
6.7,3.1,4.7,1.5,versicolor
|
||||
6.3,2.3,4.4,1.3,versicolor
|
||||
5.6,3,4.1,1.3,versicolor
|
||||
5.5,2.5,4,1.3,versicolor
|
||||
5.5,2.6,4.4,1.2,versicolor
|
||||
6.1,3,4.6,1.4,versicolor
|
||||
5.8,2.6,4,1.2,versicolor
|
||||
5,2.3,3.3,1,versicolor
|
||||
5.6,2.7,4.2,1.3,versicolor
|
||||
5.7,3,4.2,1.2,versicolor
|
||||
5.7,2.9,4.2,1.3,versicolor
|
||||
6.2,2.9,4.3,1.3,versicolor
|
||||
5.1,2.5,3,1.1,versicolor
|
||||
5.7,2.8,4.1,1.3,versicolor
|
||||
6.3,3.3,6,2.5,virginica
|
||||
5.8,2.7,5.1,1.9,virginica
|
||||
7.1,3,5.9,2.1,virginica
|
||||
6.3,2.9,5.6,1.8,virginica
|
||||
6.5,3,5.8,2.2,virginica
|
||||
7.6,3,6.6,2.1,virginica
|
||||
4.9,2.5,4.5,1.7,virginica
|
||||
7.3,2.9,6.3,1.8,virginica
|
||||
6.7,2.5,5.8,1.8,virginica
|
||||
7.2,3.6,6.1,2.5,virginica
|
||||
6.5,3.2,5.1,2,virginica
|
||||
6.4,2.7,5.3,1.9,virginica
|
||||
6.8,3,5.5,2.1,virginica
|
||||
5.7,2.5,5,2,virginica
|
||||
5.8,2.8,5.1,2.4,virginica
|
||||
6.4,3.2,5.3,2.3,virginica
|
||||
6.5,3,5.5,1.8,virginica
|
||||
7.7,3.8,6.7,2.2,virginica
|
||||
7.7,2.6,6.9,2.3,virginica
|
||||
6,2.2,5,1.5,virginica
|
||||
6.9,3.2,5.7,2.3,virginica
|
||||
5.6,2.8,4.9,2,virginica
|
||||
7.7,2.8,6.7,2,virginica
|
||||
6.3,2.7,4.9,1.8,virginica
|
||||
6.7,3.3,5.7,2.1,virginica
|
||||
7.2,3.2,6,1.8,virginica
|
||||
6.2,2.8,4.8,1.8,virginica
|
||||
6.1,3,4.9,1.8,virginica
|
||||
6.4,2.8,5.6,2.1,virginica
|
||||
7.2,3,5.8,1.6,virginica
|
||||
7.4,2.8,6.1,1.9,virginica
|
||||
7.9,3.8,6.4,2,virginica
|
||||
6.4,2.8,5.6,2.2,virginica
|
||||
6.3,2.8,5.1,1.5,virginica
|
||||
6.1,2.6,5.6,1.4,virginica
|
||||
7.7,3,6.1,2.3,virginica
|
||||
6.3,3.4,5.6,2.4,virginica
|
||||
6.4,3.1,5.5,1.8,virginica
|
||||
6,3,4.8,1.8,virginica
|
||||
6.9,3.1,5.4,2.1,virginica
|
||||
6.7,3.1,5.6,2.4,virginica
|
||||
6.9,3.1,5.1,2.3,virginica
|
||||
5.8,2.7,5.1,1.9,virginica
|
||||
6.8,3.2,5.9,2.3,virginica
|
||||
6.7,3.3,5.7,2.5,virginica
|
||||
6.7,3,5.2,2.3,virginica
|
||||
6.3,2.5,5,1.9,virginica
|
||||
6.5,3,5.2,2,virginica
|
||||
6.2,3.4,5.4,2.3,virginica
|
||||
5.9,3,5.1,1.8,virginica
|
||||
|
Binary file not shown.
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,26 @@
|
||||
Hours,Scores
|
||||
2.5,21
|
||||
5.1,47
|
||||
3.2,27
|
||||
8.5,75
|
||||
3.5,30
|
||||
1.5,20
|
||||
9.2,88
|
||||
5.5,60
|
||||
8.3,81
|
||||
2.7,25
|
||||
7.7,85
|
||||
5.9,62
|
||||
4.5,41
|
||||
3.3,42
|
||||
1.1,17
|
||||
8.9,95
|
||||
2.5,30
|
||||
1.9,24
|
||||
6.1,67
|
||||
7.4,69
|
||||
2.7,30
|
||||
4.8,54
|
||||
3.8,35
|
||||
6.9,76
|
||||
7.8,86
|
||||
|
@@ -0,0 +1,98 @@
|
||||
Hours,Scores
|
||||
2.5,21
|
||||
5.1,47
|
||||
3.2,27
|
||||
8.5,75
|
||||
3.5,30
|
||||
1.5,20
|
||||
9.2,88
|
||||
5.5,60
|
||||
8.3,81
|
||||
2.7,25
|
||||
7.7,85
|
||||
5.9,62
|
||||
4.5,41
|
||||
3.3,42
|
||||
1.1,17
|
||||
8.9,95
|
||||
2.5,30
|
||||
1.9,24
|
||||
6.1,67
|
||||
7.4,69
|
||||
2.7,30
|
||||
4.8,54
|
||||
3.8,35
|
||||
6.9,76
|
||||
7.8,86
|
||||
4.2,49
|
||||
9.5,90
|
||||
5.8,63
|
||||
2.3,23
|
||||
4.7,50
|
||||
1.6,19
|
||||
9.0,92
|
||||
6.2,68
|
||||
8.1,82
|
||||
3.9,38
|
||||
2.2,22
|
||||
7.1,73
|
||||
6.3,66
|
||||
5.3,56
|
||||
4.4,45
|
||||
1.8,21
|
||||
9.4,93
|
||||
6.7,71
|
||||
3.7,37
|
||||
7.3,77
|
||||
4.1,44
|
||||
8.0,79
|
||||
3.1,28
|
||||
1.3,16
|
||||
5.7,59
|
||||
2.8,29
|
||||
7.9,80
|
||||
6.6,72
|
||||
3.6,36
|
||||
2.4,26
|
||||
4.6,48
|
||||
8.4,84
|
||||
1.7,18
|
||||
5.2,53
|
||||
6.4,64
|
||||
7.6,78
|
||||
9.3,94
|
||||
3.4,33
|
||||
2.1,20
|
||||
5.4,55
|
||||
8.2,83
|
||||
1.4,15
|
||||
9.6,98
|
||||
6.8,74
|
||||
7.5,70
|
||||
4.3,43
|
||||
6.5,65
|
||||
8.7,87
|
||||
3.0,27
|
||||
2.9,31
|
||||
1.2,14
|
||||
9.7,96
|
||||
4.9,51
|
||||
5.6,57
|
||||
8.6,89
|
||||
2.6,32
|
||||
7.2,72
|
||||
1.0,12
|
||||
6.0,61
|
||||
4.0,40
|
||||
5.0,52
|
||||
6.8,67
|
||||
2.0,18
|
||||
3.0,34
|
||||
7.0,73
|
||||
9.8,99
|
||||
5.8,60
|
||||
4.4,44
|
||||
6.1,63
|
||||
3.7,37
|
||||
8.0,84
|
||||
|
||||
|
@@ -0,0 +1,49 @@
|
||||
import torch
|
||||
from torch import nn
|
||||
import numpy as np
|
||||
import matplotlib.pyplot as plt
|
||||
|
||||
def plot_decision_boundary(model: torch.nn.Module, X: torch.Tensor, y: torch.Tensor) -> None:
|
||||
"""Plots decision boundaries of a given PyTorch model, in comparison to the ground truth.
|
||||
|
||||
Args:
|
||||
model (torch.nn.Module): The PyTorch model to visualize.
|
||||
X (torch.Tensor): The input tensor for the model.
|
||||
y (torch.Tensor): The ground truth tensor.
|
||||
|
||||
Returns:
|
||||
None.
|
||||
|
||||
"""
|
||||
# Transfer the model and data to CPU
|
||||
device = torch.device("cpu")
|
||||
model.to(device)
|
||||
X, y = X.to(device), y.to(device)
|
||||
|
||||
# Create a grid of prediction boundaries
|
||||
x_min, x_max = X[:, 0].min() - 0.1, X[:, 0].max() + 0.1
|
||||
y_min, y_max = X[:, 1].min() - 0.1, X[:, 1].max() + 0.1
|
||||
xx, yy = np.meshgrid(np.linspace(x_min, x_max, 101), np.linspace(y_min, y_max, 101))
|
||||
|
||||
# Convert the grid to a PyTorch tensor
|
||||
X_to_pred_on = torch.from_numpy(np.column_stack((xx.ravel(), yy.ravel()))).float().to(device)
|
||||
|
||||
# Make predictions using the model
|
||||
model.eval()
|
||||
with torch.no_grad():
|
||||
y_logits = model(X_to_pred_on)
|
||||
|
||||
# Determine if this is a binary or multi-class classification problem
|
||||
if len(torch.unique(y)) > 2:
|
||||
y_pred = torch.softmax(y_logits, dim=1).argmax(dim=1) # multi-class
|
||||
else:
|
||||
y_pred = torch.round(torch.sigmoid(y_logits)) # binary
|
||||
|
||||
# Reshape the prediction tensor and plot the decision boundary
|
||||
y_pred = y_pred.reshape(xx.shape).detach().numpy()
|
||||
plt.contourf(xx, yy, y_pred, cmap=plt.cm.RdYlBu, alpha=0.7)
|
||||
|
||||
# Plot the original data points
|
||||
plt.scatter(X[:, 0], X[:, 1], c=y, s=40, cmap=plt.cm.RdYlBu)
|
||||
plt.xlim(xx.min(), xx.max())
|
||||
plt.ylim(yy.min(), yy.max())
|
||||
Reference in New Issue
Block a user