-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathread_file.py
More file actions
141 lines (111 loc) · 2.43 KB
/
read_file.py
File metadata and controls
141 lines (111 loc) · 2.43 KB
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import matplotlib.pyplot as plt
import numpy as np
import pdb
# import matplotlib.pyplot as plt
# import numpy as np
# def readFile(filename,w,v):
# f = open(filename, "r")
# i=0
# for line in f:
# if line[0]=="i":
# data = line.split()
# w[i]=int(data[1])
# v[i,0]=int(data[2])
# v[i,1]=int(data[3])
# i=i+1
# else:
# if line[0]=="W":
# data = line.split()
# W=int(data[1])
# f.close()
# return W
# def readPoints(filename,p):
# f = open(filename, "r")
# nbPND = 0
# for line in f:
# nbPND += 1
# YN = np.zeros((nbPND,p))
# f = open(filename, "r")
# i=0
# for line in f:
# data = line.split()
# for j in range(p):
# YN[i][j]=int(data[j])
# i=i+1
# f.close()
# return YN
# w = np.zeros(100, dtype = int)
# v = np.zeros((100,2), dtype = int)
# filename = "data\\100_items\\2KP100-TA-0.dat"
# W = readFile(filename, w, v)
# print(W)
# file_y = "data\\100_items\\2KP100-TA-0.eff"
# Yn = readPoints(file_y, 2)
# print(Yn)
def readFile(filename,w,v):
f = open(filename, "r")
i=0
for line in f:
if line[0]=="i":
data = line.split()
w[i]=int(data[1])
v[i,0]=int(data[2])
v[i,1]=int(data[3])
i=i+1
else:
if line[0]=="W":
data = line.split()
W=int(data[1])
f.close()
return W, w,v
# def readPoints(filename,p):
# f = open(filename, "r")
# nbPND = 0
# for line in f:
# nbPND += 1
# YN = np.zeros((nbPND,p))
# f = open(filename, "r")
# i=0
# for line in f:
# data = line.split()
# for j in range(p):
# YN[i][j]=int(data[j])
# i=i+1
# f.close()
# return YN
def readPoints(filename,p):
f = open(filename, "r")
nbPND = 0
for line in f:
nbPND += 1
YN = np.zeros((nbPND,p))
f = open(filename, "r")
i=0
for line in f:
data = line.split()
for j in range(p):
YN[i][j]=int(data[j])
i=i+1
f.close()
return YN
"""
w = np.zeros(100,dtype=int)
v = np.zeros((100,2),dtype=int)
filename = "Data/100_items/2KP100-TA-0.dat"
capacity, weights, values = readFile(filename,w,v) # items
p = 2
file_eff = "data\\100_items\\2KP100-TA-0.eff"
YN = readPoints(file_eff,p)
print(YN)
# print("First 10 weights (w):", w[:10].reshape(-1,1))
# print("First 10 profits (v):", v[:10])
f = open(filename, "r")
lines = f.readlines()
f.close()
# Find the first 10 lines with item data (starting with 'i')
count = 0
for line in lines:
if line[0] == "i" and count < 10:
print(line.strip()) # Print the raw line
count += 1
"""