forked from mh07607/IMGA_cuda
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtaichi_knap_readdata.py
63 lines (51 loc) · 1.69 KB
/
taichi_knap_readdata.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import taichi as ti
if __name__ == "__main__":
ti.init(arch=ti.cpu, default_fp=ti.f64)
from taichi_rng import *
knap_sack_items = []
# Open the file in read mode
with open('data/f2_l-d_kp_20_878.txt', 'r') as file:
# Iterate over each line in the file
for line in file:
# Split the line into weight and profit
profit,weight = map(int, line.split())
# Append the tuple (weight, profit) to the data list
knap_sack_items.append((weight, profit))
# print(knap_sack_items)
KNAPSACK_WEIGHT = knap_sack_items[0][0]
TOTAL_ITEMS = knap_sack_items[0][1]
knap_sack_items = knap_sack_items[1:]
KNAPSACK_ITEMS = ti.Vector.field(n=2, dtype=ti.i32, shape=(len(knap_sack_items),))
TYPE_GENOME = ti.types.vector(TOTAL_ITEMS, ti.i32)
for i in range(TOTAL_ITEMS):
KNAPSACK_ITEMS[i][0] = knap_sack_items[i][0]
KNAPSACK_ITEMS[i][1] = knap_sack_items[i][1]
@ti.func
def _generate_genome_isl(isl_ind: ti.i32):
genome = TYPE_GENOME([0 for _ in range(TOTAL_ITEMS)])
for i in range(TOTAL_ITEMS):
numb = randint_isl(0,1, isl_ind)
genome[i] = numb
return genome
@ti.func
def calculate_value(genome):
chromo_value = 0
for i in range(TOTAL_ITEMS):
if genome[i] == 1:
chromo_value+= KNAPSACK_ITEMS[i][0]
return chromo_value
@ti.func
def calculate_weight(genome):
chromo_weight = 0
for i in range(TOTAL_ITEMS):
if genome[i] == 1:
chromo_weight+= KNAPSACK_ITEMS[i][1]
return chromo_weight
@ti.func
def calculate_fitness(genome):
value = calculate_value(genome)
weight = calculate_weight(genome)
fitness = 0
if weight < KNAPSACK_WEIGHT:
fitness = value
return fitness