Skip to content

Commit

Permalink
Trabalho 3
Browse files Browse the repository at this point in the history
  • Loading branch information
Ian-Geraldi committed Sep 19, 2024
1 parent daceed7 commit 9aee601
Show file tree
Hide file tree
Showing 71 changed files with 458 additions and 97 deletions.
58 changes: 58 additions & 0 deletions Python Version/construtivos.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import numpy as np

def constByCost(instance):
descoberto = True
v_cobertura = np.zeros(instance.num_lin, dtype=np.int64)

v_sol = np.zeros(instance.num_col, dtype=np.int64)
cost_sol = 0

# Sort indices by the cost (equivalent of sortperm in Julia)
v_ind = np.argsort(instance.v_cost)

j = 0
while descoberto:
coluna = v_ind[j]
v_sol[coluna] = 1
cost_sol += instance.v_cost[coluna]

descoberto = False
for linha in range(instance.num_lin):
v_cobertura[linha] += instance.m_coverage[linha][coluna]
if v_cobertura[linha] == 0:
descoberto = True

j += 1

return v_sol, cost_sol, v_cobertura


def outroConst(instance):
print("Implemente outro metodo construtivo")


def randomConst(instance, chrms):
v_cobertura = np.zeros(instance.num_lin, dtype=np.int64)
v_sol = np.zeros(instance.num_col, dtype=np.int64)
cost_sol = 0

# Sort columns by their corresponding values in the chrms (random keys vector)
v_ind = np.argsort(chrms)

j = 0
descoberto = True

while descoberto:
coluna = v_ind[j] # Select the column based on sorted random keys
v_sol[coluna] = 1 # Mark the column as selected
cost_sol += instance.v_cost[coluna] # Add the cost of the selected column

descoberto = False
for linha in range(instance.num_lin):
v_cobertura[linha] += instance.m_coverage[linha][coluna]
if v_cobertura[linha] == 0:
descoberto = True # If any row is still uncovered, continue selecting

j += 1

return v_sol, cost_sol, v_cobertura
49 changes: 49 additions & 0 deletions Python Version/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import time
import random
import datetime

# Include necessary files (you can import relevant functions/modules)
from scp_instance import scpInstance
from construtivos import constByCost, outroConst, randomConst

########################################
# Leitura dos argumentos e da instancia
########################################

# Check if the correct number of arguments are passed
# ARGS in Julia refers to command-line arguments. In Python, use sys.argv.

instance_file = "Tarefa 2/data/IGC1.txt" # instance_file receives the name of the instance file
cod_metodo = 2 # Method code

instance = scpInstance(instance_file) # Load instance data

########################################
# Chamada do metodo construtivo
########################################

start_time = time.time() # Start time counter

if cod_metodo == 0:
S, cost, v_cobertura = constByCost(instance) # Call constByCost method
elif cod_metodo == 1:
S, cost, v_cobertura = outroConst(instance) # Call outroConst method
else:
seed = random.getrandbits(64) # Generate a random seed
random.seed(seed) # Set the random seed

# Write the seed to a text file
seed_file = "seed_{}.txt".format(datetime.datetime.now().strftime("%Y-%m-%d_%H-%M-%S"))
with open(seed_file, "w") as file:
file.write(str(seed))

chrms = [random.random() for _ in range(instance.num_col)] # Generate random keys vector
S, cost, v_cobertura = randomConst(instance, chrms) # Call random method

# Calculate total time
total_time = round(time.time() - start_time, 2)

# Print results
print(f"Tempo: {total_time}")
print(f"Custo: {cost}")
print(f"Solucao: {S}")
28 changes: 28 additions & 0 deletions Python Version/scp_instance.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
class scpInstance:
def __init__(self, filename: str):
with open(filename, 'r') as f:
s = f.read()

values = s.split()

v = 0 # iterator for values
self.num_lin = int(values[v])

v += 1 # v=1
self.num_col = int(values[v])

v += 1 # v=2
self.v_cost = [int(values[v + j]) for j in range(self.num_col)]

v += self.num_col # v = num_col + 2

self.m_coverage = [[0] * self.num_col for _ in range(self.num_lin)]

for i in range(self.num_lin):
v += 1
cl = int(values[v])

for _ in range(cl):
v += 1
j = int(values[v]) - 1 # adjust for 0-indexing in Python
self.m_coverage[i][j] = 1
14 changes: 14 additions & 0 deletions Results/scp41.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
Cost
5308,11.55
5144,9.53
4468,9.62
4782,9.41
5338,10.09
4841,9.6
5726,10.29
5180,9.84
4682,9.59
5929,10.04
Média dos custos: 5139.8
Média dos tempos: 9.956
Menor custo: 4468
14 changes: 14 additions & 0 deletions Results/scp42.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
Cost
4734,10.34
4717,9.77
5298,10.21
4531,9.99
5204,10.25
5759,10.04
4884,10.16
5025,9.89
5135,10.91
4979,10.49
Média dos custos: 5026.6
Média dos tempos: 10.205
Menor custo: 4531
14 changes: 14 additions & 0 deletions Results/scp51.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
Cost
5003,15.73
4915,17.2
4409,15.94
5470,15.98
4970,15.98
5263,14.94
4849,14.79
4747,15.68
5098,16.04
5345,15.43
Média dos custos: 5006.9
Média dos tempos: 15.771
Menor custo: 4409
14 changes: 14 additions & 0 deletions Results/scp52.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
Cost
5133,15.6
5488,15.97
5016,16.15
5109,15.54
5333,15.21
4996,15.56
5636,15.22
5135,15.37
4805,15.77
5136,15.97
Média dos custos: 5178.7
Média dos tempos: 15.636000000000001
Menor custo: 4805
14 changes: 14 additions & 0 deletions Results/scp61.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
Cost
3267,8.19
2149,7.81
2815,8.19
2809,8.0
2948,7.85
2724,7.69
3185,8.13
2564,8.11
3404,8.9
2917,7.92
Média dos custos: 2878.2
Média dos tempos: 8.079
Menor custo: 2149
1 change: 1 addition & 0 deletions Seeds/18-09/scp41 09-18_19-24-18.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
17849601288476634649
1 change: 1 addition & 0 deletions Seeds/18-09/scp41 09-18_19-24-44.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1002435381292223730
1 change: 1 addition & 0 deletions Seeds/18-09/scp41 09-18_19-25-09.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
16070948480802929411
1 change: 1 addition & 0 deletions Seeds/18-09/scp41 09-18_19-25-32.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
8024425746243728464
1 change: 1 addition & 0 deletions Seeds/18-09/scp41 09-18_19-25-56.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
4886361374427116623
1 change: 1 addition & 0 deletions Seeds/18-09/scp41 09-18_19-26-21.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
13650503759019191832
1 change: 1 addition & 0 deletions Seeds/18-09/scp41 09-18_19-26-44.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
13857812545251570568
1 change: 1 addition & 0 deletions Seeds/18-09/scp41 09-18_19-27-09.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
2013193365147955844
1 change: 1 addition & 0 deletions Seeds/18-09/scp41 09-18_19-27-33.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
15624723082352394786
1 change: 1 addition & 0 deletions Seeds/18-09/scp41 09-18_19-27-57.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
6481417028207412871
1 change: 1 addition & 0 deletions Seeds/18-09/scp42 09-18_19-28-22.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
17179622578954455228
1 change: 1 addition & 0 deletions Seeds/18-09/scp42 09-18_19-28-46.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
11336758802171886445
1 change: 1 addition & 0 deletions Seeds/18-09/scp42 09-18_19-29-10.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
12725217386008788776
1 change: 1 addition & 0 deletions Seeds/18-09/scp42 09-18_19-29-35.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
9851372773064871505
1 change: 1 addition & 0 deletions Seeds/18-09/scp42 09-18_19-30-00.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
16643742538210349762
1 change: 1 addition & 0 deletions Seeds/18-09/scp42 09-18_19-30-24.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
10513174103207308246
1 change: 1 addition & 0 deletions Seeds/18-09/scp42 09-18_19-30-48.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
5306427156230385911
1 change: 1 addition & 0 deletions Seeds/18-09/scp42 09-18_19-31-13.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
10579747226478762360
1 change: 1 addition & 0 deletions Seeds/18-09/scp42 09-18_19-31-37.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
11359080637261379752
1 change: 1 addition & 0 deletions Seeds/18-09/scp42 09-18_19-32-02.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
13505545801151595449
1 change: 1 addition & 0 deletions Seeds/18-09/scp51 09-18_19-32-43.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
8307103726477892430
1 change: 1 addition & 0 deletions Seeds/18-09/scp51 09-18_19-33-29.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3003638930416612492
1 change: 1 addition & 0 deletions Seeds/18-09/scp51 09-18_19-34-18.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
15768699232259987925
1 change: 1 addition & 0 deletions Seeds/18-09/scp51 09-18_19-35-04.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
15383440810392201162
1 change: 1 addition & 0 deletions Seeds/18-09/scp51 09-18_19-35-51.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
8253998459978174909
1 change: 1 addition & 0 deletions Seeds/18-09/scp51 09-18_19-36-36.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
14536725608669379578
1 change: 1 addition & 0 deletions Seeds/18-09/scp51 09-18_19-37-20.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
17267518286023023353
1 change: 1 addition & 0 deletions Seeds/18-09/scp51 09-18_19-38-05.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
16556520461240793925
1 change: 1 addition & 0 deletions Seeds/18-09/scp51 09-18_19-38-50.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
12702610454926479266
1 change: 1 addition & 0 deletions Seeds/18-09/scp51 09-18_19-39-36.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
15004737111013318952
1 change: 1 addition & 0 deletions Seeds/18-09/scp52 09-18_19-40-22.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
10885847200204097439
1 change: 1 addition & 0 deletions Seeds/18-09/scp52 09-18_19-41-07.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
11335061579529320500
1 change: 1 addition & 0 deletions Seeds/18-09/scp52 09-18_19-41-53.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
13126430031382591983
1 change: 1 addition & 0 deletions Seeds/18-09/scp52 09-18_19-42-39.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
7091643361271091599
1 change: 1 addition & 0 deletions Seeds/18-09/scp52 09-18_19-43-25.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
17134646410982031556
1 change: 1 addition & 0 deletions Seeds/18-09/scp52 09-18_19-44-09.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
317917808718770755
1 change: 1 addition & 0 deletions Seeds/18-09/scp52 09-18_19-44-54.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
9604472183692857838
1 change: 1 addition & 0 deletions Seeds/18-09/scp52 09-18_19-45-39.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
17018941487142204017
1 change: 1 addition & 0 deletions Seeds/18-09/scp52 09-18_19-46-24.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
16768692282223101932
1 change: 1 addition & 0 deletions Seeds/18-09/scp52 09-18_19-47-09.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
11368000872477153912
1 change: 1 addition & 0 deletions Seeds/18-09/scp61 09-18_19-47-53.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
17253748237429418813
1 change: 1 addition & 0 deletions Seeds/18-09/scp61 09-18_19-48-28.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
10383721710719538206
1 change: 1 addition & 0 deletions Seeds/18-09/scp61 09-18_19-49-04.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
2327699788587212200
1 change: 1 addition & 0 deletions Seeds/18-09/scp61 09-18_19-49-40.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
13269362266738931174
1 change: 1 addition & 0 deletions Seeds/18-09/scp61 09-18_19-50-15.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
17012193021213316177
1 change: 1 addition & 0 deletions Seeds/18-09/scp61 09-18_19-50-51.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
16369634669870936140
1 change: 1 addition & 0 deletions Seeds/18-09/scp61 09-18_19-51-26.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
8303097003220717217
1 change: 1 addition & 0 deletions Seeds/18-09/scp61 09-18_19-52-02.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
6992028001467649130
1 change: 1 addition & 0 deletions Seeds/18-09/scp61 09-18_19-52-39.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
7557315045822904773
1 change: 1 addition & 0 deletions Seeds/18-09/scp61 09-18_19-53-18.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
18040275412386011667
File renamed without changes.
2 changes: 1 addition & 1 deletion Tarefa 2/main.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ include("construtivos.jl")

# import from the data folder

instance_file = "Tarefa 2/data/scpb2.txt" # instance_file recebe o nome do arquivo da instancia
instance_file = "Tarefa 2/data/IGC1.txt" # instance_file recebe o nome do arquivo da instancia
cod_metodo = 1


Expand Down
3 changes: 1 addition & 2 deletions Tarefa 2/scpInstance.jl
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ struct scpInstance #<: AbstractInstance
m_coverage = zeros(Int64, num_lin, num_col)

for i = 1:num_lin
println("Building the instance... $(i)/$(num_lin) lines done")
v=v+1
cl = parse(Int64, values[v])

Expand All @@ -53,5 +54,3 @@ struct scpInstance #<: AbstractInstance
end

################################################################################


1 change: 0 additions & 1 deletion Tarefa 3/.~lock.TrabalhoImplementacao_decoder.pptx#

This file was deleted.

Loading

0 comments on commit 9aee601

Please sign in to comment.