-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
executable file
·141 lines (113 loc) · 3.58 KB
/
main.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
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
#!/usr/bin/env python
import sys
import copy
import os
import os
import logging
from IMAGE import image
from PROJECT import project
from CONVEX import convex_hull
from COMPUTE import compute_x_or_y
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
def _write(fname, **data):
try:
logging.debug(f"Writing to {fname}: \n {data}")
with open(fname, 'w') as f:
for key, matrix in data.items():
f.write(key)
f.write('\n')
if key == 'b':
logging.debug("b")
line = ' '.join(list(map(str, matrix)))
logging.debug(line)
f.write(line)
f.write('\n')
if key == 'A':
logging.debug(f"{key}: {type(A[0])}")
if type(A[0]) != list:
line = ' '.join(list(map(str, matrix)))
logging.debug(line)
f.write(line)
f.write('\n')
else:
for row in matrix:
line = ' '.join(list(map(str, row)))
logging.debug(line)
f.write(line)
f.write('\n')
except Exception as e:
print(e)
def _parser(filepath, identifier = 'A'):
with open(filepath) as f:
A = []
_idx = 0
lines = f.readlines()
#find the starting position
for idx, line in enumerate(lines):
line = line.strip()
#prfloat(line)
if line[0] == identifier:
_idx = idx
break
for idx, line in enumerate(lines[_idx+1:]):
if not line[0].isnumeric() and not line[0] == '-':
break
else:
A.append(list(map(float, line.split(' '))))
if identifier == 'b':
A = list(map(float, line.split(' ')))
logging.debug(f"Parsed {identifier}: \n {A}")
return A
argv = sys.argv
arg = sys.argv[1]
if arg == 'project':
input_file = argv[2]
k = int(argv[3])
A = _parser(input_file, identifier = 'A')
b = _parser(input_file, identifier = 'b')
logging.debug(f"Projecting with k: {k}")
logging.debug(f"A : {A}")
logging.debug(f"b : {b}")
n = len(A[0])
A, b = project(A,b, k, n)
logging.debug(f"Result from project: ")
logging.debug(f"\t A: {A}")
logging.debug(f"\t b : {b}")
output_file = argv[4]
_write(output_file, **{
'A' : A,
'b' : b
})
elif arg == 'image':
input_file_1 = argv[2]
input_file_2 = argv[3]
output_file = argv[4]
A = _parser(input_file_1, identifier = 'A')
b = _parser(input_file_1, identifier = 'b')
M = _parser(input_file_2, identifier = 'M')
logging.debug(f"M : {M}")
logging.debug(f"A : {A}")
logging.debug(f"b : {b}")
A, b = image(M, A, b)
_write(output_file, **{
'A' : A,
'b' : b
})
elif arg == 'H_representation':
print("H Representation")
input_file = argv[2]
output_file = argv[3]
X = _parser(input_file, identifier = 'X')
A, b = convex_hull(X)
_write(output_file, **{
'A' : A,
'b' : b
})
elif arg == 'compute_x_or_y':
input_file = argv[2]
output_file = argv[3]
A = _parser(input_file, identifier = 'A')
b = _parser(input_file, identifier = 'b')
BOOL, x = compute_x_or_y(A, b)
logging.debug(f"{BOOL} : {x}")