-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutils.py
More file actions
176 lines (134 loc) · 4.5 KB
/
utils.py
File metadata and controls
176 lines (134 loc) · 4.5 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import json
import requests
import numpy as np
from collections import deque
import time
import os
import shutil
from datetime import datetime
import _pickle as cPickle
"""
에이전트의 신용등급 분포
대출거래고객의 신용등급 분포에 따라 랜덤 배정
2018년 12월 개인신용평가 관련 통계자료 기준
http://www.niceinfo.co.kr/creditrating/cb_score_3.nice
"""
CREDIT_DIST = np.array([0.2820866, 0.16822173, 0.1433743, 0.11848693, 0.10438483,
0.06542559, 0.03797563, 0.03051768, 0.03119231, 0.0183344])
"""
상위 5개 은행 대출 이자율의 평균
은행 순위: 2018년 9월 금감원 금융정보통계시스템 기준
http://fisis.fss.or.kr/fss/fsiview/indexw_ng.html
에이전트의 신용등급별에 따라 이자율이 다름
이자율 출처: 전국은행연합회
https://portal.kfb.or.kr/main/main.php
"""
RATES = [0.03734, 0.03734, 0.04508, 0.04508, 0.06042, 0.06042, 0.07946, 0.07946, 0.09475, 0.09475]
URI = "http://localhost:3000"
HEADERS = {'Content-type': 'application/json'}
class MovingAverage:
def __init__(self, window):
self.table = deque(maxlen=window)
self._avg = 0.
def update(self, item):
self.table.append(item)
self._avg = np.mean(self.table)
@property
def avg(self):
return self._avg
def start(http_port):
global URI
URI = "http://localhost:{}".format(http_port)
os.chdir("./server")
os.system("npm install --silent")
os.system("HTTP_PORT={} npm start &".format(http_port))
while True:
try:
res = requests.get(URI + "/isConnected")
msg = json.loads(res.text)["msg"]
if "connected" in msg.lower():
break
except Exception:
time.sleep(1)
pass
os.chdir("../")
def close():
requests.post(URI + "/stop", headers=HEADERS)
def reset(path):
requests.post(URI + "/reset", headers=HEADERS, data=json.dumps({"path": path}))
def get_time(is_success, timer):
if is_success:
time = timer
else:
try:
res = requests.get(URI + "/timer")
res = json.loads(res.text)
time = res["timer"] - res["remain"]
except KeyError:
time = timer
return time
def get_start_time():
res = requests.get(URI + "/timer")
res = json.loads(res.text)
if "msg" in res:
start_time = -1
else:
start_time = res["start"]
return start_time
def get_is_success():
res = requests.get(URI + "/status")
is_success = json.loads(res.text)["isSuccess"]
return is_success
def get_orderbook():
res = requests.get(URI + "/orderBook")
orderbook = json.loads(res.text)["orderBook"]
return orderbook
def get_is_alive():
res = requests.get(URI + "/status")
is_alive = json.loads(res.text)["isAlive"]
return is_alive
def get_stack():
res = requests.get(URI + "/stack")
stack = json.loads(res.text)["stack"]
return stack
def get_interest_rate():
credit = np.random.choice(np.arange(10), 1, p=CREDIT_DIST)[0]
rate = RATES[credit]
return rate
def set_stack(stack):
requests.post(URI + "/setStack", headers=HEADERS, data=json.dumps({"stack": stack}))
def set_timer(timer):
requests.post(URI + "/setTimer", headers=HEADERS, data=json.dumps({"timer": timer}))
def create_log_dir(argv):
if not os.path.isdir('./logs/'):
os.mkdir('./logs/')
my_args = argv
path_ = []
if len(my_args) != 1:
use_equality = ('=' in my_args[1])
else:
use_equality = False
now = datetime.now().isoformat()
path_.append(now)
for idx in range(1, len(my_args)):
if use_equality or idx % 2 == 1:
path_.append(my_args[idx][2:])
else:
path_.append(my_args[idx])
path = '_'.join(path_) + '/'
# 기존에 같은 이름의 폴더가 있을 경우 삭제
if os.path.isdir('./logs/' + path):
shutil.rmtree('./logs/' + path)
# 폴더 새로 생성
os.mkdir('./logs/' + path)
return path
def save_checkpoints(path, data, idx):
checkpoint_path = os.path.join('checkpoints', path)
os.makedirs(checkpoint_path, exist_ok=True)
with open(os.path.join(checkpoint_path, '{}.pkl'.format(idx)), 'wb') as f:
cPickle.dump(data, f)
files = os.listdir(checkpoint_path)
if len(files) > 10:
sorted_files = sorted(files, key=lambda x: int(x[:-4]))
file_to_remove = os.path.join(checkpoint_path, sorted_files[0])
os.remove(file_to_remove)