Академический Документы
Профессиональный Документы
Культура Документы
Laboratorio 1
Leer el archivo CSV que contiene los datos del problema de la mochila:
http://localhost:8890/notebooks/pCloud%20Sync/DocumentsTries/UTEC/IO2018-1/TP%20Julia/JuliaLab01.ipynb# 1/7
04/04/2018 JuliaLab01
In [2]:
using CSV
using DataFrames
#cd(dirname(@__FILE__))
Instance = CSV.read("Items.csv"; header = [:ItemID,:Profit, :Weight], nullable = fal
Out[2]:
1 100000 498389 0
2 0 5094 485
3 1 4506 6326
4 2 4416 9248
5 3 992 5421
6 4 6649 322
7 5 9237 5795
8 6 6457 8043
9 7 7815 2845
10 8 9446 4955
11 9 3422 9252
12 10 8791 1009
13 11 9359 5901
14 12 5667 1122
15 13 1598 1094
16 14 2007 8738
17 15 544 8574
18 16 5334 5715
19 17 2766 8882
20 18 3994 1367
21 19 9893 9984
22 20 2633 3299
23 21 7131 9433
24 22 5428 5682
25 23 6700 72
26 24 617 7874
27 25 5874 138
http://localhost:8890/notebooks/pCloud%20Sync/DocumentsTries/UTEC/IO2018-1/TP%20Julia/JuliaLab01.ipynb# 2/7
04/04/2018 JuliaLab01
28 26 7720 3856
29 27 4419 7145
30 28 9794 7995
⋮ ⋮ ⋮ ⋮
La primera fila no corresponde a un objeto, sólo a los parámetros de la instancia del problema: aquí, es una
instancia con N=100,000 objetos y una mochila de capacidad K=498389. Podemos recuperar esos valores N y
K:
In [3]:
N = Instance[1,1]
K = Instance[1,2]
Out[3]:
498389
Limpiamos los datos eliminando la fila 1 para quedarnos sólo con los objetos:
In [4]:
deleterows!(Instance, 1)
head(Instance)
Out[4]:
1 0 5094 485
2 1 4506 6326
3 2 4416 9248
4 3 992 5421
5 4 6649 322
6 5 9237 5795
Creamos un nuevo tipo de objeto, el tipo 'Item'. Cada item se define por su ID, por su valor y si peso:
In [5]:
struct Item
ID::Int
profit::Int
weight::Int
end
Creamos un nuevo array 'I' inicialmente vacío y colocamos todos los items en ese array:
http://localhost:8890/notebooks/pCloud%20Sync/DocumentsTries/UTEC/IO2018-1/TP%20Julia/JuliaLab01.ipynb# 3/7
04/04/2018 JuliaLab01
In [6]:
I = Item[]
for i = 1:nrow(Instance)
item = Item(Instance[i,:ItemID],Instance[i,:Profit], Instance[i,:Weight])
push!(I,item)
end
Para recuperar el item que corresponde al tercer elemento del array 'I':
In [7]:
I[3]
Out[7]:
In [8]:
sort!(I, lt=(x,y)->isless(x.ID,y.ID))
Out[8]:
100000-element Array{Item,1}:
Item(0, 5094, 485)
Item(1, 4506, 6326)
Item(2, 4416, 9248)
Item(3, 992, 5421)
Item(4, 6649, 322)
Item(5, 9237, 5795)
Item(6, 6457, 8043)
Item(7, 7815, 2845)
Item(8, 9446, 4955)
Item(9, 3422, 9252)
Item(10, 8791, 1009)
Item(11, 9359, 5901)
Item(12, 5667, 1122)
⋮
Item(99988, 9983, 8187)
Item(99989, 5124, 7233)
Item(99990, 9430, 160)
Item(99991, 3142, 2227)
Item(99992, 1659, 9524)
Item(99993, 8132, 7929)
Item(99994, 5283, 1080)
Item(99995, 7659, 2836)
Item(99996, 6649, 7719)
Item(99997, 4488, 6497)
Item(99998, 5227, 9644)
Item(99999, 1723, 3211)
http://localhost:8890/notebooks/pCloud%20Sync/DocumentsTries/UTEC/IO2018-1/TP%20Julia/JuliaLab01.ipynb# 4/7
04/04/2018 JuliaLab01
In [9]:
sort!(I, lt=(x,y)->isless(y.weight,x.weight))
Out[9]:
100000-element Array{Item,1}:
Item(1371, 5109, 10000)
Item(45639, 4936, 10000)
Item(71954, 3570, 10000)
Item(85882, 4698, 10000)
Item(97202, 4561, 10000)
Item(7495, 2829, 9999)
Item(30760, 7809, 9999)
Item(34064, 5345, 9999)
Item(38027, 2671, 9999)
Item(42686, 8710, 9999)
Item(56220, 5357, 9999)
Item(60956, 8970, 9999)
Item(67969, 2469, 9999)
⋮
Item(17190, 8194, 1)
Item(18051, 2408, 1)
Item(19456, 9919, 1)
Item(34792, 6500, 1)
Item(38793, 2902, 1)
Item(50984, 4414, 1)
Item(60268, 2760, 1)
Item(66335, 4611, 1)
Item(70184, 520, 1)
Item(82119, 9997, 1)
Item(83149, 8690, 1)
Item(84143, 4462, 1)
Imaginamos que queremos constituir la lista de los objetos que colocar en la mochila. Para eso, creamos un
array inicialmente vacío 'content' de objetos:
In [10]:
knapsack = []
Out[10]:
0-element Array{Any,1}
Si por ejemplo colocamos los cuarto y decimo objetos del array 'I' en la mochila:
In [11]:
push!(knapsack, I[4])
push!(knapsack, I[10])
println(knapsack)
Podemos programar una función que cuenta el valor total de los objetos en la mochila:
http://localhost:8890/notebooks/pCloud%20Sync/DocumentsTries/UTEC/IO2018-1/TP%20Julia/JuliaLab01.ipynb# 5/7
04/04/2018 JuliaLab01
In [12]:
function knapsackProfit(content_array)
i = 1
profit = 0
while i <= length(content_array)
profit += content_array[i].profit
i += 1
end
return profit
end
Out[12]:
In [13]:
totalProfit = knapsackProfit(knapsack)
println(totalProfit)
13408
Podemos escribir una función más completa que nos calcula todos los parámetros de la mochila: número de
objetos, peso total y valor total:
In [14]:
function knapsackParams(content_array)
i = 1
profit = 0
weight = 0
while i <= length(content_array)
profit += content_array[i].profit
weight += content_array[i].weight
i += 1
end
return length(content_array), profit, weight
end
Out[14]:
In [15]:
2
13408
19999
Ejercicio 1:
Escribir una función 'getMaxValueItem(content_array)' que retorna el peso y el valor del objeto más valioso de
la mochila.
http://localhost:8890/notebooks/pCloud%20Sync/DocumentsTries/UTEC/IO2018-1/TP%20Julia/JuliaLab01.ipynb# 6/7
04/04/2018 JuliaLab01
In [ ]:
Ejercicio 2:
Escribir una función 'printItems(content_array)' que imprime la lista de objetos en la mochila en orden
decreciente de valores.
In [ ]:
Ejercicio 3:
Escribir una función 'fillKnapsack(items_array, content_array)' que llena el array 'content_array' con una
selección arbitraria de objetos de 'items_array' de tal manera que el peso total de la mochila no sea mayor que
su capacidad máxima K.
In [ ]:
Ejercicio 4:
Cómo podriamos mejorar esa función para maximizar el valor total de la mochila sin exeder su capacidad
maxima? Escribir tres nuevas funciones con tres ideas distintas: 'fillKnapsack1(items_array, content_array)',
'fillKnapsack2(items_array, content_array)' y 'fillKnapsack3(items_array, content_array)'. Comparar los
resultados.
In [ ]:
http://localhost:8890/notebooks/pCloud%20Sync/DocumentsTries/UTEC/IO2018-1/TP%20Julia/JuliaLab01.ipynb# 7/7