1+ {
2+ "nbformat" : 4 ,
3+ "nbformat_minor" : 0 ,
4+ "metadata" : {
5+ "colab" : {
6+ "provenance" : []
7+ },
8+ "kernelspec" : {
9+ "name" : " python3" ,
10+ "display_name" : " Python 3"
11+ }
12+ },
13+ "cells" : [
14+ {
15+ "cell_type" : " code" ,
16+ "metadata" : {
17+ "colab" : {
18+ "base_uri" : " https://localhost:8080/"
19+ },
20+ "id" : " dyucO8hkBMJG" ,
21+ "outputId" : " 4d160b21-695c-4595-9dd0-585f4123ac2d"
22+ },
23+ "source" : [
24+ " import numpy as np\n " ,
25+ " \n " ,
26+ " item_number = np.arange(1,11)\n " ,
27+ " population_size = (10, item_number.shape[0])\n " ,
28+ " initial_population_x = np.random.randint(2, size = population_size)\n " ,
29+ " initial_population_sigma = np.random.randint(range(0,10), 10)\n " ,
30+ " initial_population = []\n " ,
31+ " initial_population.append(initial_population_x)\n " ,
32+ " initial_population.append(initial_population_sigma)\n " ,
33+ " print(initial_population[0])"
34+ ],
35+ "execution_count" : null ,
36+ "outputs" : [
37+ {
38+ "output_type" : " stream" ,
39+ "text" : [
40+ " [[0 1 1 0 1 1 1 0 1 1]\n " ,
41+ " [1 1 1 1 1 0 0 0 1 1]\n " ,
42+ " [1 1 0 0 1 1 1 1 1 0]\n " ,
43+ " [1 0 0 0 1 1 1 0 0 0]\n " ,
44+ " [0 0 0 1 1 0 0 1 0 1]\n " ,
45+ " [1 0 0 1 1 1 0 1 1 0]\n " ,
46+ " [0 0 0 1 0 0 0 1 1 0]\n " ,
47+ " [1 1 0 0 1 1 0 0 0 1]\n " ,
48+ " [0 1 1 1 1 0 0 0 0 0]\n " ,
49+ " [1 0 0 0 1 0 1 0 0 1]]\n "
50+ ],
51+ "name" : " stdout"
52+ }
53+ ]
54+ },
55+ {
56+ "cell_type" : " code" ,
57+ "metadata" : {
58+ "colab" : {
59+ "base_uri" : " https://localhost:8080/"
60+ },
61+ "id" : " rvLr9PTuI04w" ,
62+ "outputId" : " 784dabe5-692c-4985-f2aa-b3fadd44faa0"
63+ },
64+ "source" : [
65+ " j = 10\n " ,
66+ " for i in range(j):\n " ,
67+ " print(i)"
68+ ],
69+ "execution_count" : null ,
70+ "outputs" : [
71+ {
72+ "output_type" : " stream" ,
73+ "text" : [
74+ " 0\n " ,
75+ " 1\n " ,
76+ " 2\n " ,
77+ " 3\n " ,
78+ " 4\n " ,
79+ " 5\n " ,
80+ " 6\n " ,
81+ " 7\n " ,
82+ " 8\n " ,
83+ " 9\n "
84+ ],
85+ "name" : " stdout"
86+ }
87+ ]
88+ },
89+ {
90+ "cell_type" : " code" ,
91+ "metadata" : {
92+ "id" : " rDT8QmcwLF9F"
93+ },
94+ "source" : [
95+ " import numpy as np\n " ,
96+ " import pandas as pd\n " ,
97+ " import random as rd\n " ,
98+ " from random import randint\n " ,
99+ " import matplotlib.pyplot as plt"
100+ ],
101+ "execution_count" : null ,
102+ "outputs" : []
103+ },
104+ {
105+ "cell_type" : " code" ,
106+ "metadata" : {
107+ "id" : " kDC40x5_Mipm"
108+ },
109+ "source" : [
110+ " def cal_fitness(weight, value, population, threshold):\n " ,
111+ " fitness = np.empty(population.shape[0])\n " ,
112+ " for i in range(population.shape[0]):\n " ,
113+ " S1 = np.sum(population[i] * value)\n " ,
114+ " S2 = np.sum(population[i] * weight)\n " ,
115+ " if S2 <= threshold:\n " ,
116+ " fitness[i] = S1\n " ,
117+ " else :\n " ,
118+ " fitness[i] = 0\n " ,
119+ " return fitness.astype(int)"
120+ ],
121+ "execution_count" : null ,
122+ "outputs" : []
123+ },
124+ {
125+ "cell_type" : " code" ,
126+ "metadata" : {
127+ "id" : " nwmnOecxM7f9"
128+ },
129+ "source" : [
130+ " def generate_offsprings_func(population, sigma, alpha):\n " ,
131+ " offsprings = np.empty((num_offsprings, parents.shape[1]))\n " ,
132+ " for i in range(offsprings.shape[0]):\n " ,
133+ " random_value = rd.randint(0,10)\n " ,
134+ " \t\t if (random_value > 5):\n " ,
135+ " \t\t\t offsprings[i, ]\n " ,
136+ " mutants[i,:] = offsprings[i,:]\n " ,
137+ " if random_value > mutation_rate:\n " ,
138+ " continue\n " ,
139+ " int_random_value = randint(0,offsprings.shape[1]-1)\n " ,
140+ " if mutants[i,int_random_value] == 0 :\n " ,
141+ " mutants[i,int_random_value] = 1\n " ,
142+ " else :\n " ,
143+ " mutants[i,int_random_value] = 0\n " ,
144+ " return offsprings"
145+ ],
146+ "execution_count" : null ,
147+ "outputs" : []
148+ },
149+ {
150+ "cell_type" : " code" ,
151+ "metadata" : {
152+ "id" : " Z6MLyKIXMow3"
153+ },
154+ "source" : [
155+ " def selection(fitness, num_parents, population):\n " ,
156+ " fitness = list(fitness)\n " ,
157+ " parents = np.empty((num_parents, population.shape[1]))\n " ,
158+ " for i in range(num_parents):\n " ,
159+ " index=weighted_random_choice(fitness)\n " ,
160+ " parents[i,:] = population[index, :]\n " ,
161+ " return parents\n " ,
162+ " \n " ,
163+ " def crossover(parents, num_offsprings):\n " ,
164+ " offsprings = np.empty((num_offsprings, parents.shape[1]))\n " ,
165+ " crossover_point = int(parents.shape[1]/2)\n " ,
166+ " crossover_rate = 0.9\n " ,
167+ " i=0\n " ,
168+ " while (parents.shape[0] < num_offsprings):\n " ,
169+ " parent1_index = i%parents.shape[0]\n " ,
170+ " parent2_index = (i+1)%parents.shape[0]\n " ,
171+ " x = rd.random()\n " ,
172+ " if x > crossover_rate:\n " ,
173+ " continue\n " ,
174+ " parent1_index = i%parents.shape[0]\n " ,
175+ " parent2_index = (i+1)%parents.shape[0]\n " ,
176+ " offsprings[i,0:crossover_point] = parents[parent1_index,0:crossover_point]\n " ,
177+ " offsprings[i,crossover_point:] = parents[parent2_index,crossover_point:]\n " ,
178+ " i=+1\n " ,
179+ " return offsprings\n " ,
180+ " \n " ,
181+ " def mutation(offsprings):\n " ,
182+ " mutants = np.empty((offsprings.shape))\n " ,
183+ " mutation_rate = 0.2\n " ,
184+ " for i in range(mutants.shape[0]):\n " ,
185+ " random_value = rd.random()\n " ,
186+ " mutants[i,:] = offsprings[i,:]\n " ,
187+ " if random_value > mutation_rate:\n " ,
188+ " continue\n " ,
189+ " int_random_value = randint(0,offsprings.shape[1]-1)\n " ,
190+ " if mutants[i,int_random_value] == 0 :\n " ,
191+ " mutants[i,int_random_value] = 1\n " ,
192+ " else :\n " ,
193+ " mutants[i,int_random_value] = 0\n " ,
194+ " return mutants\n " ,
195+ " \n " ,
196+ " def elitism(mutants, population, fitness, weight, value, threshold):\n " ,
197+ " elitis = np.empty((mutants.shape))\n " ,
198+ " elitis = mutants\n " ,
199+ " parents_max_loc = np.where(fitness == np.max(fitness))[0]\n " ,
200+ " parents_max_loc_real = parents_max_loc[0]\n " ,
201+ " fitness_mutants = cal_fitness(weight, value, mutants, threshold)\n " ,
202+ " mutants_min_loc = np.where(fitness_mutants == np.min(fitness_mutants))\n " ,
203+ " mutants_min_loc_real = mutants_min_loc[0]\n " ,
204+ " elitis[mutants_min_loc_real] = population[parents_max_loc_real]\n " ,
205+ " return elitis"
206+ ],
207+ "execution_count" : null ,
208+ "outputs" : []
209+ },
210+ {
211+ "cell_type" : " code" ,
212+ "metadata" : {
213+ "id" : " TxRkoHxBMvb1"
214+ },
215+ "source" : [
216+ " def optimize(weight, value, population, pop_size, num_generations, threshold):\n " ,
217+ " parameters, fitness_history = [], []\n " ,
218+ " num_parents = int(pop_size[0])\n " ,
219+ " num_offsprings = pop_size[0]\n " ,
220+ " for i in range(num_generations-1):\n " ,
221+ " fitness = cal_fitness(weight, value, population, threshold)\n " ,
222+ " fitness_history.append(fitness)\n " ,
223+ " parents = selection(fitness, num_parents, population)\n " ,
224+ " offsprings = crossover(parents, num_offsprings)\n " ,
225+ " mutants = mutation(offsprings)\n " ,
226+ " population = elitism(mutants, population, fitness, weight, value, threshold)\n " ,
227+ " \n " ,
228+ " fitness_last_gen = cal_fitness(weight, value, population, threshold)\n " ,
229+ " max_fitness = np.where(fitness_last_gen == np.max(fitness_last_gen))\n " ,
230+ " parameters.append(population[max_fitness[0][0],:])\n " ,
231+ " print (parameters, fitness_history)\n " ,
232+ " return parameters, fitness_history"
233+ ],
234+ "execution_count" : null ,
235+ "outputs" : []
236+ },
237+ {
238+ "cell_type" : " code" ,
239+ "metadata" : {
240+ "id" : " 3s0k7zZzMxOM"
241+ },
242+ "source" : [
243+ " def kalkulasi(nama_makanan, value_makanan, harga_makanan, uang) :\n " ,
244+ " \t numpy_nama_makanan = np.array([i for i in nama_makanan.split(',')])\n " ,
245+ " \t numpy_value_makanan = np.array([int(i) for i in value_makanan.split(',')])\n " ,
246+ " \t numpy_harga_makanan = np.array([int(i) for i in harga_makanan.split(',')])\n " ,
247+ " \n " ,
248+ " \t item_number = np.arange(1,11)\n " ,
249+ " \t weight = numpy_harga_makanan\n " ,
250+ " \t value = numpy_value_makanan\n " ,
251+ " \t knapsack_threshold = int(uang)\n " ,
252+ " \n " ,
253+ " \t solutions_per_pop = 10\n " ,
254+ " \t pop_size = (solutions_per_pop, item_number.shape[0])\n " ,
255+ " \t initial_population = np.random.randint(2, size = pop_size)\n " ,
256+ " \t initial_population = initial_population.astype(int)\n " ,
257+ " \t num_generations = 50\n " ,
258+ " \n " ,
259+ " \t parameters, fitness_history = optimize(weight, value, initial_population, pop_size, num_generations, knapsack_threshold)\n " ,
260+ " \t selected_items = item_number * parameters\n " ,
261+ " \n " ,
262+ " \t hasil_nama_makanan = []\n " ,
263+ " \t kalori = 0\n " ,
264+ " \t uang_yang_dibutuhkan = 0\n " ,
265+ " \t for i in selected_items[0] :\n " ,
266+ " \t\t if (i!=0) :\n " ,
267+ " \t\t\t hasil_nama_makanan.append(numpy_nama_makanan[int(i)-1])\n " ,
268+ " \t\t\t kalori = kalori + numpy_value_makanan[int(i)-1]\n " ,
269+ " \t\t\t uang_yang_dibutuhkan = uang_yang_dibutuhkan + numpy_harga_makanan[int(i)-1]\n " ,
270+ " \n " ,
271+ " \t hasil_string = \"\"\n " ,
272+ " \t for i in hasil_nama_makanan :\n " ,
273+ " \t\t hasil_string = hasil_string + i + \" ,\"\n " ,
274+ " \t total = []\n " ,
275+ " \t total.append(hasil_string)\n " ,
276+ " \t total.append(kalori)\n " ,
277+ " \t total.append(uang_yang_dibutuhkan)\n " ,
278+ " \n " ,
279+ " \t return total"
280+ ],
281+ "execution_count" : null ,
282+ "outputs" : []
283+ },
284+ {
285+ "cell_type" : " code" ,
286+ "metadata" : {
287+ "colab" : {
288+ "base_uri" : " https://localhost:8080/"
289+ },
290+ "id" : " raUezsc5LJGg" ,
291+ "outputId" : " 25a091c7-ed95-4c7f-b77e-42bf28703ef2"
292+ },
293+ "source" : [
294+ " nama_makanan = \" Tugas Alevo, Tugas TBI, Menyuci baju, Mengepel lantai, Menyetrika baju, Memandikan kucing, Mengisi bak mandi, Bermain badminton, Mengurus anak, Mengisi galon\"\n " ,
295+ " value_makanan = \" 4, 4, 3, 1, 2, 1, 1, 2, 3, 2\"\n " ,
296+ " harga_makanan = \" 5, 4, 2, 1, 2, 1, 1, 3, 3, 1\"\n " ,
297+ " uang = 15\n " ,
298+ " kalkulasi(nama_makanan, value_makanan, harga_makanan, uang)"
299+ ],
300+ "execution_count" : null ,
301+ "outputs" : [
302+ {
303+ "output_type" : " stream" ,
304+ "text" : [
305+ "[array([0., 1., 1., 1., 1., 1., 1., 0., 1., 1.])] [array([ 0, 7, 11, 10, 14, 6, 8, 3, 10, 5]), array([14, 17, 17, 17, 17, 17, 17, 17, 17, 17]), array([10, 17, 10, 10, 10, 14, 10, 10, 14, 14]), array([17, 17, 17, 15, 17, 17, 17, 17, 17, 17]), array([10, 14, 14, 10, 10, 17, 17, 17, 10, 10]), array([17, 15, 17, 13, 15, 15, 15, 17, 17, 17]), array([10, 14, 10, 10, 10, 10, 17, 17, 10, 10]), array([17, 17, 14, 17, 17, 17, 15, 17, 17, 17]), array([13, 17, 17, 17, 17, 17, 17, 17, 17, 17]), array([17, 17, 17, 17, 17, 17, 15, 17, 17, 17]), array([17, 17, 17, 17, 17, 17, 17, 17, 17, 17]), array([17, 17, 16, 17, 17, 13, 17, 13, 17, 17]), array([14, 17, 17, 17, 17, 17, 15, 17, 17, 17]), array([17, 17, 17, 17, 17, 17, 17, 17, 17, 17]), array([17, 17, 15, 17, 16, 17, 17, 17, 17, 15]), array([17, 17, 17, 17, 17, 17, 17, 17, 17, 17]), array([17, 17, 17, 17, 17, 17, 17, 17, 17, 17]), array([16, 17, 17, 14, 17, 17, 17, 17, 17, 14]), array([17, 17, 17, 17, 17, 16, 17, 17, 17, 17]), array([16, 17, 17, 17, 17, 15, 16, 17, 14, 14]), array([17, 17, 17, 17, 17, 17, 17, 17, 17, 17]), array([16, 14, 13, 17, 17, 14, 17, 17, 17, 14]), array([17, 17, 17, 17, 17, 17, 17, 15, 17, 17]), array([11, 17, 17, 17, 14, 12, 17, 14, 17, 17]), array([17, 17, 17, 17, 15, 17, 17, 17, 17, 17]), array([17, 17, 17, 17, 17, 12, 17, 17, 12, 13]), array([17, 17, 17, 17, 17, 17, 17, 17, 17, 17]), array([17, 17, 17, 17, 17, 17, 17, 12, 17, 17]), array([17, 17, 17, 17, 17, 17, 17, 17, 17, 17]), array([17, 15, 16, 17, 17, 17, 17, 17, 17, 16]), array([17, 17, 17, 17, 17, 17, 17, 17, 17, 17]), array([17, 16, 16, 17, 17, 17, 17, 17, 16, 16]), array([17, 17, 17, 17, 17, 17, 17, 17, 17, 17]), array([17, 17, 17, 17, 16, 17, 17, 17, 16, 16]), array([17, 17, 17, 17, 17, 17, 17, 17, 17, 17]), array([17, 17, 17, 17, 17, 17, 17, 17, 17, 17]), array([17, 17, 17, 17, 17, 17, 17, 17, 17, 17]), array([17, 17, 17, 17, 17, 17, 17, 17, 17, 17]), array([17, 17, 17, 17, 17, 17, 17, 17, 17, 17]), array([17, 17, 17, 17, 17, 17, 17, 17, 17, 17]), array([17, 17, 17, 17, 17, 17, 17, 17, 17, 17]), array([17, 17, 17, 17, 17, 17, 17, 17, 17, 17]), array([17, 17, 17, 17, 17, 17, 16, 17, 17, 17]), array([17, 17, 17, 17, 17, 17, 17, 17, 17, 17]), array([17, 17, 16, 17, 17, 17, 17, 17, 17, 17]), array([17, 17, 17, 17, 15, 17, 17, 17, 17, 17]), array([17, 16, 16, 16, 14, 17, 17, 15, 17, 17]), array([17, 17, 15, 17, 17, 17, 17, 15, 17, 17]), array([17, 17, 17, 16, 17, 17, 17, 17, 17, 16])]\n"
306+ ],
307+ "name" : " stdout"
308+ },
309+ {
310+ "output_type" : " execute_result" ,
311+ "data" : {
312+ "text/plain" : [
313+ " [' Tugas TBI, Menyuci baju, Mengepel lantai, Menyetrika baju, Memandikan kucing, Mengisi bak mandi, Mengurus anak, Mengisi galon,',\n " ,
314+ " 17,\n " ,
315+ " 15]"
316+ ]
317+ },
318+ "metadata" : {
319+ "tags" : []
320+ },
321+ "execution_count" : 31
322+ }
323+ ]
324+ }
325+ ]
326+ }
0 commit comments