-
Notifications
You must be signed in to change notification settings - Fork 12
/
rQ_dnn.py
1598 lines (1348 loc) · 69.1 KB
/
rQ_dnn.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
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import sys
import os
# os.environ["CUDA_VISIBLE_DEVICES"]= str(2)
import tensorflow as tf
import numpy as np
import scipy.io as sio
import scipy.stats as sts
import pandas as pd
import time
tf.logging.set_verbosity(tf.logging.FATAL)
import matplotlib.pyplot as plt
from sklearn.neighbors import NearestNeighbors
np.random.seed(4)
import argparse
arg_lists = []
parser = argparse.ArgumentParser()
def add_argument_group(name):
arg = parser.add_argument_group(name)
arg_lists.append(arg)
return arg
def str2bool(v):
return v.lower() in ('true', '1')
# build a config
arg_lists = []
res_arg = add_argument_group('prediction')
res_arg.add_argument('--task', type=str, default='pred', help='')
res_arg.add_argument('--if_batch_nomalizing', type=str2bool, default='True', help='')
res_arg.add_argument('--use_momentun', type=str2bool, default='True', help='otherwise it uses adam')
res_arg.add_argument('--use_sigmoid', type=str2bool, default='True', help='otherwise it uses relu activation')
res_arg.add_argument('--dis', type=str, default='normal', help='')
res_arg.add_argument('--l2lambda', type=float, default=0.01, help='')
res_arg.add_argument('--lr0', type=float, default=0.01, help='')
res_arg.add_argument('--use_dropout', type=str2bool, default='False', help='')
res_arg.add_argument('--clusters', type=int, default=100, help='')
res_arg.add_argument('--maxiter', type=int, default=2100000, help='')
res_arg.add_argument('--display', type=int, default=20000, help='')
res_arg.add_argument('--batch_size', type=int, default=128, help='')
res_arg.add_argument('--decay_step', type=int, default=10000, help='')
res_arg.add_argument('--decay_rate', type=float, default=0.98, help='')
res_arg.add_argument('--decay_rate_stair', type=float, default=0.98, help='')
res_arg.add_argument('--cp', type=float, default=10, help='')
res_arg.add_argument('--ch', type=float, default=1, help='')
res_arg.add_argument('--num_of_period', type=int, default=11, help='the number of input periods (defualt is 11)')
res_arg.add_argument('--eager_mode', type=str2bool, default='False', help='')
res_arg.add_argument('--nodes', type=list, default=[350, 150], help='')
res_arg.add_argument('--num_outputs', type=int, default=11, help='')
res_arg.add_argument('--larry_test', type=str2bool, default='False', help='')
res_arg.add_argument('--sim_period', type=int, default=11, help='')
res_arg.add_argument('--if_print_eil_solutions', type=str2bool, default='True', help='')
res_arg.add_argument('--if_print_knn_solutions', type=str2bool, default='True', help='')
res_arg.add_argument('--if_simulation_data', type=str2bool, default='True', help='')
res_arg.add_argument('--if_print_rQ', type=str2bool, default='False', help='')
res_arg.add_argument('--if_print_rQ_test_set', type=str2bool, default='False', help='')
res_arg.add_argument('--if_print_knn_final_cost', type=str2bool, default='False', help='')
res_arg.add_argument('--use_current_trained_network', type=str2bool, default='False', help='If True, it does not reset and load any network in run_simulator()')
def update_checkpoint(str_num, addr):
''' This function removes the address in the saved checkpoint file'''
import re
addr = os.path.join(addr, 'checkpoint')
fille = open(addr, "r")
dis = ['beta', 'lognormal', 'normal', 'exponential', 'uniform']
cls = [200, 100, 10, 1]
out = ''
no_enter=False
for line in fille:
word_list = line.split(' ')
print word_list
for word in word_list:
print word
if '\n' in word:
word=re.sub('\n','', word)
word = re.sub('rq_runner_code/saved_networks/', '', word)
for d in dis:
if d in word:
for c in cls:
if str(c)+'/' in word:
word = re.sub(d+'/'+str(c)+'/', '', word)
break
break
for c in reversed(range(100)):
if str(c)+'/' in word:
word = re.sub(str(c)+'/', '', word)
break
out += word
out += '\n'
print out
fille.close()
fille = open(addr, "w+")
fille.write(out)
fille.close()
class rq(object):
def __init__(self, clusters, real_cluster, dis, config):
self.config = config
self.clusters = clusters
self.dis = dis
self.real_cluster = real_cluster
self.if_print_eil_solutions = True
self.if_print_knn_solutions = True
self.if_simulation_data = True
self.if_print_rQ = False
self.if_print_rQ_test_set = False
def get_data(self):
if self.config.if_simulation_data:
dirname = os.path.abspath('data/')
if self.config.larry_test:
# get data
train_mat_x = np.load(os.path.join(dirname,'rq_larry_x_train.npy'))
train_mat_y = np.load(os.path.join(dirname,'rq_larry_y_train.npy'))
test_mat_x = np.load(os.path.join(dirname,'rq_larry_x_test.npy'))
test_mat_y = np.load(os.path.join(dirname,'rq_larry_y_test.npy'))
index_mat_train = np.load(os.path.join(dirname,'rq_larry_ind_train.npy'))
index_mat_test = np.load(os.path.join(dirname,'rq_larry_ind_test.npy'))
self.ind_train = index_mat_train
else:
dirname = os.path.join(dirname,self.dis)
# get data
train_mat_x = sio.loadmat(os.path.join(dirname,'TrainX-nw-10000-'+str(self.real_cluster)+'-class.mat'))
train_mat_y = sio.loadmat(os.path.join(dirname,'TrainY-nw-10000-'+str(self.real_cluster)+'-class.mat'))
test_mat_x = sio.loadmat(os.path.join(dirname,'TestX-nw-10000-'+str(self.real_cluster)+'-class.mat'))
test_mat_y = sio.loadmat(os.path.join(dirname,'TestY-nw-10000-'+str(self.real_cluster)+'-class.mat'))
index_mat_train = sio.loadmat(os.path.join(dirname,'IndexX-nw-10000-'+str(self.real_cluster)+'-class.mat'))
index_mat_test = sio.loadmat(os.path.join(dirname,'IndexY-nw-10000-'+str(self.real_cluster)+'-class.mat'))
train_mat_x = train_mat_x['trainX']
train_mat_y = train_mat_y['trainY']
test_mat_x = test_mat_x['testX']
test_mat_y = test_mat_y['testY']
self.ind_train = index_mat_train['IndexX']
index_mat_test = index_mat_test['IndexY']
test_limit = 1+99
if self.clusters != 1:
self.train_x = train_mat_x[:,0,:]
print "self.train_x", np.shape(self.train_x)
self.test_x = test_mat_x[0:test_limit*2500,0,:]
else:
self.train_x = train_mat_x[:,:]
self.test_x = test_mat_x[0:test_limit*2500,:]
self.train_y = train_mat_y[:,0]
self.test_y = test_mat_y[0:test_limit*2500,0]
# get validation data
self.valid_x = self.test_x[0:1*2500,:]
self.valid_y = np.squeeze(test_mat_y[0:1*2500])
self.ind_valid = index_mat_test[0:1*2500,:]
self.test_x = self.test_x[1*2500:test_limit*2500,:]
print "self.test_x", np.shape(self.test_x)
self.test_y = np.squeeze(test_mat_y[1*2500:test_limit*2500])
self.ind_test = index_mat_test[1*2500:test_limit*2500,:]
if self.clusters == 1:
self.NoInputs = 1
elif self.clusters == 10 or self.clusters == 100 or self.clusters == 103:
self.NoInputs = 31
elif self.clusters == 203 or self.clusters == 200:
self.NoInputs = 36
else:
test_mat = np.genfromtxt('data/basket_test_data_w_mu_sigma.csv' ,dtype=float, delimiter=',',skip_header=1)
train_mat = np.genfromtxt('data/basket_train_data_w_mu_sigma.csv' ,dtype=float, delimiter=',',skip_header=1)
test_binary = np.genfromtxt('data/Basket_test_data_binary.csv' ,dtype=float, delimiter=',',skip_header=0)
train_binary = np.genfromtxt('data/Basket_train_data_binary.csv' ,dtype=float, delimiter=',',skip_header=0)
train_mat_ind = np.genfromtxt('data/train_ind.csv' , delimiter=',').astype(int)
test_mat_ind = np.genfromtxt('data/test_ind.csv' , delimiter=',').astype(int)
ind_train = np.expand_dims(train_mat_ind,1)
self.ind_train = ind_train[:9000,:]
self.ind_test = np.expand_dims(test_mat_ind,1)
self.ind_valid = ind_train[9000:,:]
# cluster number, day of week, month of year, department id, demand, mean of demand for training cluster,
# std of demand for training cluster
self.train_x = np.squeeze(np.array(train_binary[:9000,:43]))
self.test_x = np.squeeze(np.array(test_binary[:,:43]))
self.valid_x = np.squeeze(np.array(train_binary[9000:,:43]))
self.train_y = np.squeeze(np.array(train_binary[:9000,43]))
self.test_y = np.squeeze(np.array(test_binary[:,43]))
self.valid_y = np.squeeze(np.array(train_binary[9000:,43]))
train_size=len(self.train_y)
test_size=len(self.test_y)
valid_size=len(self.valid_y)
self.train_size = train_size
self.test_size = test_size
self.valid_size = valid_size
# define the required cost coefficients of the model.
self.zeros_tr =np.zeros((train_size,1))
self.zeros_te =np.zeros((test_size,1))
self.zeros_val =np.zeros((valid_size,1))
self.cp = self.config.cp
self.ch = self.config.ch
self.shrtg_cost_tr = self.cp*np.ones((train_size,1))
self.hld_cost_tr = self.ch*np.ones((train_size,1))
self.shrtg_cost_te = self.cp*np.ones((test_size,1))
self.hld_cost_te = self.ch*np.ones((test_size,1))
self.shrtg_cost_val = self.cp*np.ones((valid_size,1))
self.hld_cost_val = self.ch*np.ones((valid_size,1))
# get mu and sigma of each cluster. We use them later to obtain EIL cost
def get_mu_sigma(self):
if self.clusters == 1:
nn = 1
self.mu = np.zeros(nn)
self.sigma = np.zeros(nn)
yy = pd.DataFrame(self.train_y)
xx = pd.DataFrame(self.train_x)
for i in range(nn):
self.mu[i] = np.mean(yy.values)
self.sigma[i] = np.std(yy.values)
#
self.il_rep = 0
self.input_dim = len(self.train_x[0]) + self.il_rep
else:
nn = int(np.amax([np.amax(self.ind_train[:,0]), np.amax(self.ind_valid[:,0]),
np.amax(self.ind_test[:,0])])) + 1
a,b = np.unique(self.ind_train[:,0], return_index=True)
self.mu = np.zeros(nn)
self.sigma = np.zeros(nn)
yy = pd.DataFrame(self.train_y)
xx = pd.DataFrame(self.train_x)
ii = pd.DataFrame(self.ind_train)
# loop over the number of clusters and get mu and sigma of each
# cluster and save them.
for i in range(nn):
if len(yy[self.ind_train[:,0]==i]) == 0:
self.mu[i] = np.mean(yy.values)
self.sigma[i] = np.std(yy.values)
else:
self.mu[i] = np.mean(yy[self.ind_train[:,0]==i].values)
self.sigma[i] = np.std(yy[self.ind_train[:,0]==i].values)
#
self.il_rep = 0
self.input_dim = len(self.train_x[0]) + self.il_rep
def set_dnn_settings(self):
# if use newsvendor model, set it True, for (s,S) model set it False
# sS=False
self.rQ=True
self.EIL=True
self.aprx=False
# if it is ture, it uses relu activation function, otherwise uses sigmoid
self.ifRelu = True
config_tf = tf.ConfigProto()
# config_tf.gpu_options.per_process_gpu_memory_fraction = 0.1
config_tf.gpu_options.allow_growth = True
config_tf.intra_op_parallelism_threads = 1
self.sess = tf.InteractiveSession(config=config_tf)
cur_dir=os.path.realpath("./saved_networks")
cur_dir=os.path.join(cur_dir, self.dis)
self.model_dir=os.path.join(cur_dir, str(self.clusters))
if not os.path.exists(self.model_dir):
os.makedirs(self.model_dir)
self.config.maxiter = 45001
self.config.display = 7500
self.config.decay_rate = 0.0005
self.config.decay_rate_stair = 0.99
self.config.rl0 = 0.005
self.power = 0.75
self.config.l2lambda = 0.009
self.init_momentum = 0.9
self.config.decay_step = 15000
self.run_number = 0
self.config.batch_size = 128
self.var = 2.0/44
self.input_dim = len(self.train_x[0]) + self.il_rep
if self.rQ:
self.nodes = [self.input_dim, 90, 150, 56,2]
else:
self.nodes = [self.input_dim, 90, 150, 56,1]
self.NoHiLay = len(self.nodes) - 2
self.shrtg_cost_tr = self.cp*np.ones((self.train_size,1))
self.hld_cost_tr = self.ch*np.ones((self.train_size,1))
self.order_cost_tr = self.K*np.ones((self.train_size,1))
self.lambdaa_tr = self.lambdaa*np.ones((self.train_size,1))
self.l_tr = np.ones((self.train_size,1))
self.zeros_tr = np.zeros((self.train_size,1))
self.shrtg_cost_te = self.cp*np.ones((self.test_size,1))
self.hld_cost_te = self.ch*np.ones((self.test_size,1))
self.order_cost_te = self.K*np.ones((self.test_size,1))
self.lambdaa_te = self.lambdaa*np.ones((self.test_size,1))
self.l_te = np.ones((self.test_size,1))
self.zeros_te =np.zeros((self.test_size,1))
self.shrtg_cost_val = self.cp*np.ones((self.valid_size,1))
self.hld_cost_val = self.ch*np.ones((self.valid_size,1))
self.order_cost_val = self.K*np.ones((self.valid_size,1))
self.lambdaa_val = self.lambdaa*np.ones((self.valid_size,1))
self.l_val = np.ones((self.valid_size,1))
self.zeros_val = np.zeros((self.valid_size,1))
self.loss_type = 'L2'
for c,i in enumerate(self.ind_test[:,0]):
if self.clusters == 1:
i = 0
self.l_te[c,0] = self.l[int(i)]
for c,i in enumerate(self.ind_train[:,0]):
if self.clusters == 1:
i = 0
self.l_tr[c,0] = self.l[int(i)]
for c,i in enumerate(self.ind_valid[:,0]):
if self.clusters == 1:
i = 0
self.l_val[c,0] = self.l[int(i)]
# get K, lambda, and L (lead time demand)
def set_rq_settings(self):
self.K = 20
self.lambdaa = 1200
if self.clusters == 1:
self.l = self.mu/self.lambdaa
else:
# note that self.lambdaa is a scallar so that we do not need np.divide
self.l = self.mu/self.lambdaa
# get approximated solution of (r,Q) policy, since approximate the distribution by normal
def get_EIL_solution(self):
# get approximated solution of (r,Q) policy, since approximate the distribution by normal
#
# It uses the approximated mu and sigma, obtained in self.get_mu_sigma
# We assume *know* lambda. This is a much more reasonable assumption under the new parameters
# than under the old ones. Basically it means that we know the average demand per *year*,
# but the actual demand in any given lead time (2 weeks) depends on the features.
# Under the old parameters, we were saying we know the average demand per *day* but not the
# average demand over *5 days*, which does not make sense.
epsilon = 0.1
big_r = 10000
if self.clusters == 1:
self.Q_new = np.sqrt(2*self.K*self.lambdaa/self.ch)*np.ones(self.clusters)
self.r_new = big_r*np.ones(self.clusters)
else:
self.Q_new = np.squeeze(np.sqrt(2*self.K*self.lambdaa/self.ch)*np.ones((1,self.clusters)))
self.r_new = np.squeeze(big_r*np.ones((1,self.clusters)))
# print '(r,Q) is: ', r_new, Q_new
for cls in range(self.clusters):
notStop = True
i = 0
while notStop:
i += 1
# print "iteration ", i
# reset the value of r,Q
self.Q_old = self.Q_new[cls]
self.r_old = self.r_new[cls]
# update r
self.z = (self.Q_old*self.ch)/(self.cp*self.lambdaa)
if self.z >= 0 and self.z <= 1:
if self.sigma[cls] < 1e-8:
self.sigma[cls] = 1e-8
self.r_new[cls] = sts.norm.isf(self.z, self.mu[cls],self.sigma[cls]) # isf works with 1-cdf
elif self.z > 1:
self.r_new[cls] = -big_r
# update Q
self.z = (self.r_new[cls]-self.mu[cls])/self.sigma[cls]
self.zetta = sts.norm.pdf(self.z) - self.z*(1-sts.norm.cdf(self.z))
self.nr = self.zetta*self.sigma[cls]
self.Q_new[cls] = np.sqrt((2*self.lambdaa*(self.K+self.cp*self.nr))/self.ch)
# print 'z, zetta, (r,Q) is: ', z , zetta, r_new[cls], Q_new[cls]
# check if we should stop
if np.abs(self.Q_new[cls]-self.Q_old) < epsilon:
if np.abs(self.r_new[cls]-self.r_old) < epsilon:
notStop = False
if self.if_print_eil_solutions:
print cls, '( %0.2f' %self.r_new[cls] ,', %0.2f' %self.Q_new[cls], ')', 'g(r,Q) is: %0.2f' %(
self.ch*(self.r_new[cls] - self.lambdaa*self.l[cls] + self.Q_new[cls]/2) +
self.K*self.lambdaa/self.Q_new[cls] + self.cp*self.lambdaa*self.nr/self.Q_new[cls])
, i, 'iterations'
# It gets EIL cost based on given self.r_new and self.Q_new, obtained by
# self.get_EIL_solution(). It goes over all demand (which are self.train_y,
# self.valid_y, self.test_y)
def get_eil_cost(self, demand, ind):
# r_new_ and Q_new_ are the list with "clusters" members
if self.clusters == 1:
cost = 0
for d in demand:
approximate_nr = max(d-self.r_new,0)
cost += self.ch*(self.r_new - self.lambdaa*self.l[cls] + self.Q_new/2) + \
self.K*self.lambdaa/self.Q_new + self.cp*self.lambdaa*approximate_nr/self.Q_new
else:
cost = 0
for i, d in enumerate(demand):
r_new = self.r_new[int(ind[i][0])]
Q_new = self.Q_new[int(ind[i][0])]
approximate_nr = max(d-r_new,0)
cost += self.ch*(r_new - self.lambdaa*self.l[int(ind[i][0])] + Q_new/2) + \
self.K*self.lambdaa/Q_new + self.cp*self.lambdaa*approximate_nr/Q_new
return cost
def print_EIL_costs(self):
'''print EIL cost for all avilable demand data'''
# get optimal solutions
print 'optimal solutions'
cost_val = self.get_eil_cost(self.valid_y, self.ind_valid)
cost_tr = self.get_eil_cost(self.train_y, self.ind_train)
print '\t \t optimal' , '\t', 'DNN'
print 'train \t %0.1f' %cost_tr
print 'valid \t %0.1f' %cost_val
def get_aprx_solution(self):
'''get approximated solution of (r,Q) policy, since approximate the distribution by normal
It uses the approximated mu and sigma, obtained in self.get_mu_sigma
We assume *know* lambda. This is a much more reasonable assumption under the new parameters
than under the old ones. Basically it means that we know the average demand per *year*,
but the actual demand in any given lead time (2 weeks) depends on the features.
Under the old parameters, we were saying we know the average demand per *day* but not the
average demand over *5 days*, which doesn’t make sense.'''
def solve_nr(r):
''' for a given nr_ finds the r that obtains nr_'''
z = (r-mu)/sigma
zetta = sts.norm.pdf(z) - z*(1-sts.norm.cdf(z))
nr = zetta*sigma
return np.abs(nr-nr_)
def get_nr(r):
''' for a given x="r" obtains n(r). basically x is self.r_new[cls]'''
z = (r-mu)/sigma
zetta = sts.norm.pdf(z) - z*(1-sts.norm.cdf(z))
nr = zetta*sigma
return nr
def get_n2r(r):
''' for a given x="r" obtains n(r). basically x is self.r_new[cls]'''
z = (r-mu)/sigma
zetta = 0.5*((z*z+1)*(1-sts.norm.cdf(z)) - z*sts.norm.pdf(z))
n2r = zetta*sigma*sigma
return n2r
epsilon = 0.001
max_r = 2000
n2r = 1
if self.clusters == 1:
self.QP_new = np.sqrt(2*(self.K*self.lambdaa +
(self.ch + self.cp)*n2r/self.ch))*np.ones(self.clusters)
self.rP_new = np.zeros(self.clusters)
else:
self.QP_new = np.squeeze(np.sqrt(2*(self.K*self.lambdaa +
(self.ch + self.cp)*n2r/self.ch))*np.ones((1,self.clusters)))
self.rP_new = np.squeeze(np.zeros((1,self.clusters)))
for cls in range(self.clusters):
notStop = True
i = 0
#print "(r,Q)= (%0.2f" %(self.rP_new[cls]),", %0.2f )" %(self.QP_new[cls])
max_r = sts.norm.isf(1-(self.cp/(self.cp+self.ch+.0)), self.mu[cls], self.sigma[cls])
while notStop:
i += 1
# print "iteration ", i
# reset the value of r,Q
self.QP_old = self.QP_new[cls]
self.rP_old = self.rP_new[cls]
# update r
self.nr_ = (self.QP_old*self.ch)/(self.cp + self.ch)
if self.sigma[cls] < 1e-8:
self.sigma[cls] = 1e-8
sigma = self.sigma[cls]
mu = self.mu[cls]
nr_ = self.nr_
res = minimize_scalar(solve_nr, method='Golden', bracket=(0,max_r))
# get_nr(res.x) -> gives the n(r) of the obtained r
self.rP_new[cls] = res.x
# update Q
self.n2r = get_n2r(self.rP_new[cls])
self.QP_new[cls] = np.sqrt(2*(self.K*self.lambdaa +
(self.ch + self.cp)*self.n2r)/self.ch)
#print "nr= %0.2f" %nr_,", n2r= %0.2f" %self.n2r,", (r,Q)= (%0.2f" %(self.rP_new[cls]),", %0.2f )" %(self.QP_new[cls])
#print "(r,Q)= (%0.2f" %(self.rP_new[cls]),", %0.2f )" %(self.QP_new[cls])
# check if we should stop
if np.abs(self.QP_new[cls]-self.QP_old) < epsilon:
if np.abs(self.rP_new[cls]-self.rP_old) < epsilon:
notStop = False
if self.if_print_aprx_rq_solutions:
print cls, '( %0.2f' %(self.rP_new[cls]) ,', %0.2f' %self.QP_new[cls], ')', 'g(r,Q) is: %0.2f' \
%(self.ch*(self.rP_new[cls] - self.lambdaa*self.l[cls] + self.QP_new[cls]/2) +
self.K*self.lambdaa/self.QP_new[cls] +
(self.cp + self.ch)*self.lambdaa*self.n2r/self.QP_new[cls]) , i, 'iterations'
def variable_builder_rand(self, shape):
return tf.Variable(np.random.normal(0, self.var, shape))
def variable_builder_fix(self, liste):
result_list = []
for i in liste:
result_list += [tf.Variable(i)]
return result_list
# creat dnn training method
def create_train_method(self):
# import the data
#from tensorflow.examples.tutorials.mnist import input_data
# placeholders, which are the training data
self.x = tf.placeholder(tf.float64, shape=[None,self.input_dim], name='x')
self.y_ = tf.placeholder(tf.float64, shape=[None], name='y_')
self.init_IL = tf.placeholder(tf.float64, shape=[None,1], name='init_IL')
self.learning_rate = tf.placeholder(tf.float64, shape=[], name='lr')
self.zero = tf.placeholder(tf.float64, shape=[None,1], name='zero')
self.c_h = tf.placeholder(tf.float64, shape=[None,1], name='c_h')
self.c_p = tf.placeholder(tf.float64, shape=[None,1], name='c_p')
self.K_ = tf.placeholder(tf.float64, shape=[None,1], name='K')
self.L_ = tf.placeholder(tf.float64, shape=[None,1], name='L')
self.lambdaa_ = tf.placeholder(tf.float64, shape=[None,1], name='lambdaa')
self.w=[]
self.b=[]
self.best_w = []
self.best_b = []
self.layer = []
self.y=0
if self.run_number != 0:
self.w = self.variable_builder_fix(ww)
self.b = self.variable_builder_fix(bb)
# define the variables
for j in range(self.NoHiLay+1):
if self.run_number == 0:
self.w += [self.variable_builder_rand([self.nodes[j], self.nodes[j+1]])]
self.b += [self.variable_builder_rand(self.nodes[j+1])]
if j == 0:
if self.ifRelu:
self.layer += [tf.nn.relu(tf.matmul(self.x, self.w[j]) + self.b[j])]
else:
self.layer += [tf.nn.sigmoid(tf.matmul(self.x, self.w[j]) + self.b[j])]
elif j == self.NoHiLay:
self.y = tf.matmul(self.layer[j-1], self.w[j]) + self.b[j]
else:
self.layer += [tf.nn.sigmoid(tf.matmul(self.layer[j-1], self.w[j]) + self.b[j])]
# Passing global_step to minimize() will increment it at each step.
self.global_step = tf.Variable(0, trainable=False)
self.momentum = tf.Variable(self.init_momentum, trainable=False)
# r=y[:,0]; Q=y[:,1]
# prediction function (just one layer)
if self.rQ:
self.diff = tf.subtract(self.y_, self.y[:,0])
# tf.greater(x,y) returns the truth value of (x > y) element-wise.e.g. [[False False True]]
self.result = tf.greater(self.zero[:,0],self.diff)
# force the value of r be positive
self.r_positive = tf.greater(self.y[:,0], self.zero[:,0])
self.r_negative = tf.greater(self.zero[:,0], self.y[:,0])
self.r = tf.where(self.r_positive, self.y[:,0], self.zero[:,0])
self.r_penalty = tf.where(self.r_negative, self.y[:,0], self.zero[:,0])
# condition on the cost function
if self.EIL:
# get n(r)=(d-r)^+
self.nr = tf.where(self.result, self.zero[:,0], self.diff)
# tf.select(condition, t, e) : output should be taken from t (if true) or e (if false).
self.c = tf.where(self.result, self.zero[:,0], self.c_p[:,0])
# cost function
# tf.mul returns x * y element-wise.
self.cost_function = tf.reduce_sum(
tf.multiply(self.c_h[:,0], self.r - tf.multiply(self.lambdaa_[:,0],self.L_[:,0])
+ tf.scalar_mul(0.5, self.y[:,1]))
+ tf.divide(tf.multiply(self.lambdaa_[:,0], self.K_[:,0]), self.y[:,1])
+ tf.multiply(tf.divide(tf.multiply(self.lambdaa_[:,0],self.c), self.y[:,1]),
tf.abs(self.nr)))
self.c1 = tf.multiply(self.c_h[:,0], self.r -
tf.multiply(self.lambdaa_[:,0],self.L_[:,0])+ tf.scalar_mul(0.5, self.y[:,1]))
self.c2 = tf.divide(tf.multiply(self.lambdaa_[:,0], self.K_[:,0]), self.y[:,1])
self.c3 = tf.multiply(tf.divide(tf.multiply(self.lambdaa_[:,0], self.c), self.y[:,1]),
tf.abs(self.nr))
self.single_cost_function = self.c1 + self.c2 + self.c3
self.surrogate_cost_function = self.cost_function + 100*tf.reduce_sum(tf.abs(self.r_penalty))
elif self.aprx:
# get (d-r)^+
self.dmr = tf.where(self.result, self.zero[:,0], self.diff)
# get n^2(r)= 0.5*((d-r)^+)^2
self.nr = tf.scalar_mul(0.5, tf.multiply(self.dmr, self.dmr))
# tf.select(condition, t, e) : output should be taken from t (if true) or e (if false).
self.c = tf.where(self.result, self.zero[:,0], self.c_p[:,0] + self.c_h[:,0])
# cost function
# tf.mul returns x * y element-wise.
self.cost_function = tf.reduce_sum(
tf.multiply(self.c_h[:,0], self.r - tf.multiply(self.lambdaa_[:,0],self.L_[:,0])
+ tf.scalar_mul(0.5, self.y[:,1]))
+ tf.divide(tf.multiply(self.lambdaa_[:,0], self.K_[:,0]), self.y[:,1])
+ tf.multiply(tf.divide(self.c, self.y[:,1]),
self.nr))
self.c1 = tf.multiply(self.c_h[:,0], self.r -
tf.multiply(self.lambdaa_[:,0],self.L_[:,0])+ tf.scalar_mul(0.5, self.y[:,1]))
self.c2 = tf.divide(tf.multiply(self.lambdaa_[:,0], self.K_[:,0]), self.y[:,1])
self.c3 = tf.multiply(tf.divide(self.c, self.y[:,1]), self.nr)
self.single_cost_function = self.c1 + self.c2 + self.c3
self.surrogate_cost_function = self.cost_function + 100*tf.reduce_sum(tf.abs(self.r_penalty))
# the newsvendor problem
else:
self.diff = tf.subtract(self.y_, self.y[:,0])
# tf.greater(x,y) returns the truth value of (x > y) element-wise.e.g. [[False False True]]
self.result = tf.greater(self.zero[:,0], self.diff)
# tf.select(condition, t, e) : output should be taken from t (if true) or e (if false).
self.c = tf.where(self.result, self.c_h[:,0], self.c_p[:,0])
# cost function
# tf.mul returns x * y element-wise.
if (loss_type is 'L2'):
self.cost_function = tf.reduce_mean(tf.square(tf.multiply(self.diff, self.c)))/2
elif (loss_type is 'L1'):
self.cost_function = tf.reduce_mean(tf.abs(tf.multiply(self.diff, self.c)))/2
self.nw_cost_function = tf.reduce_sum(tf.abs(tf.multiply(self.diff, self.c)))
self.l2regularization = 0
for j in range(self.NoHiLay+1):
self.l2regularization += tf.reduce_sum(tf.square(self.w[j])) + tf.reduce_sum(tf.square(self.b[j]))
if self.rQ:
self.loss = self.surrogate_cost_function + self.config.l2lambda*self.l2regularization
else:
self.loss = self.cost_function + self.config.l2lambda*self.l2regularization
# define the training paramters and model, gradient model and feeding the function
#train_step = tf.train.GradientDescentOptimizer(learning_rate).minimize(loss, global_step=global_step)
# train_step = tf.train.MomentumOptimizer(learning_rate,0.9).minimize(loss, global_step=global_step)
learning_rate = tf.train.exponential_decay(self.config.rl0, self.global_step,
self.config.decay_step, self.config.decay_rate_stair, staircase=True)
self.train_step = tf.train.AdamOptimizer(learning_rate,0.9,0.999,1e-8).minimize(
self.loss, global_step=self.global_step)
# initilize the variables
self.saver = tf.train.Saverconfig.()
self.sess.run(tf.global_variables_initializer())
self.iter = 0
def run_dnn(self):
''' run the dnn method for a given number of iterations, print the results,
and holds them in self.train_result, self.val_result, self.test_result'''
self.train_result = []
self.test_result = []
self.val_result = []
# Train the Model for 1000 times. by defining the batch number we determine that it is sgd
if self.rQ:
for i in range(self.config.maxiter):
self.iter += 1
batch = np.random.randint(0,self.train_size, size=self.config.batch_size)
# lr = self.config.rl0
lr = self.config.rl0*np.power(1 + self.config.decay_rate*(i+1), -self.power)
self.sess.run(self.train_step, feed_dict={self.x:self.train_x[batch],
self.learning_rate:lr, self.c_p:self.shrtg_cost_tr[batch],
self.c_h:self.hld_cost_tr[batch], self.zero:self.zeros_tr[batch],
self.y_:self.train_y[batch], self.K_:self.order_cost_tr[batch]
, self.L_:self.l_tr[batch], self.lambdaa_:self.lambdaa_tr[batch]})
if np.mod(i, self.config.display) == 0:
feed_dict={self.x: self.train_x, self.y_: self.train_y, self.c_p:self.shrtg_cost_tr,
self.c_h:self.hld_cost_tr, self.zero:self.zeros_tr
, self.K_:self.order_cost_tr, self.L_:self.l_tr , self.lambdaa_:self.lambdaa_tr}
self.train_result += [self.sess.run(self.cost_function, feed_dict)]
# validation
feed_dict={self.x: self.valid_x, self.y_: self.valid_y, self.c_p:self.shrtg_cost_val,
self.c_h:self.hld_cost_val, self.zero:self.zeros_val,
self.K_:self.order_cost_val, self.L_:self.l_val, self.lambdaa_:self.lambdaa_val}
self.val_result += [self.sess.run(self.cost_function,feed_dict)]
print ("Iter" , i, "lr %.6f" %lr , "| Train %.2f" %self.train_result[-1] ,
"| Validation %.2f" %self.val_result[-1] , "||W|| %.2f" %(self.sess.run(self.l2regularization)),
"lmbd*||W|| %.2f" %(self.config.l2lambda*self.sess.run(self.l2regularization))
)
if self.if_print_rQ:
out = self.sess.run([self.cost_function, self.y],feed_dict)
r = np.unique(out[1][:,0])
q = np.unique(out[1][:,1])
print "(r,Q)= ", r, q
if self.if_print_rQ_test_set:
print ""
print self.sess.run(self.y,feed_dict={self.x: self.train_x})
print self.sess.run(self.y,feed_dict={self.x: self.valid_x})
self.saver.save(self.sess, self.model_dir+'/model', global_step=self.iter)
print "network weights are saved"
else:
for i in range(self.config.maxiter):
batch = np.random.randint(0,self.train_size, size=self.config.batch_size)
lr = self.config.rl0*np.power(1 + self.config.decay_rate*(i+1), -self.power)
self.sess.run(train_step, feed_dict={self.x:self.train_x[batch], self.y_:self.train_y[batch] ,
self.learning_rate:lr, self.c_p:self.shrtg_cost_tr[batch],
self.c_h:self.hld_cost_tr[batch], self.zero:self.zeros_tr[batch]})
if np.mod(i, self.config.display) == 0:
feed_dict={self.x: self.train_x, self.y_: self.train_y, self.c_p:self.shrtg_cost_tr,
self.c_h:self.hld_cost_tr, self.zero:self.zeros_tr}
self.train_result = self.sess.run(self.cost_function, feed_dict)
feed_dict={self.x: self.valid_x, self.y_: self.valid_y, self.c_p:self.shrtg_cost_val,
self.c_h:self.hld_cost_val, self.zero:self.zeros_val}
self.valid_result = self.sess.run(self.cost_function, feed_dict)
print ("Iter" , i, "lr %.6f" %lr , "| Train %.2f" %self.train_result ,
"| Test %.2f" %self.valid_result ,
"||W|| %.2f" %(self.sess.run(self.l2regularization)),
"lmbd*||W|| %.2f" %(l2lambda*self.sess.run(self.l2regularization))
)
def print_EIL_DNN_costs(self):
''' get optimal solutions, prints the EIL and DNN cost and the improvement of DNN over EIL '''
print 'optimal solutions'
cost_val = self.get_eil_cost(self.valid_y, self.ind_valid)
cost_tr = self.get_eil_cost(self.train_y, self.ind_train)
print '\t optimal' , '\t', ' DNN'
print 'train \t ', "%.1f" %cost_tr, '\t', "%.1f" %self.train_result[-1],\
'\t', "%.3f" %((self.train_result[-1] - cost_tr)/cost_tr)
print 'valid \t ', "%.1f" %cost_val, '\t', "%.1f" %self.val_result[-1],\
'\t', "%.3f" %((self.val_result[-1] - cost_val)/cost_val)
print 'avg train real cost', cost_tr/self.train_size, ',avg validation real cost', \
cost_val/self.valid_size
def print_EIL_DNN_KNN_costs(self):
''' get optimal solutions '''
print 'optimal solutions'
cost_val = self.get_eil_cost(self.valid_y, self.ind_valid)
cost_te = self.get_eil_cost(self.test_y, self.ind_test)
cost_tr = self.get_eil_cost(self.train_y, self.ind_train)
print '\t \t \t optimal' , '\t', ' DNN' , '\t', ' KNN'
print self.dis, self.clusters, 'train \t ', "%.1f" %cost_tr, '\t', "%.1f" %self.train_result[-1],\
'\t 0 \t', "%.3f" %((self.train_result[-1] - cost_tr)/cost_tr)
print self.dis, self.clusters, 'valid \t ', "%.1f" %cost_val, '\t', "%.1f" %self.val_result[-1],\
'\t 0 \t', "%.3f" %((self.val_result[-1] - cost_val)/cost_val)
print self.dis, self.clusters, 'test \t ', \
"%.1f" %cost_te, '\t', "%.1f" %self.test_result[-1],\
'\t', "%.1f" %(self.g_knn_total), \
'\t', "%.3f" %((self.test_result[-1] - cost_te)/cost_te)
print 'avg train real cost', cost_tr/self.train_size, ',avg validation real cost', \
cost_val/self.valid_size, ',avg test real cost', cost_te/self.test_size
def set_knn_settings(self, k_):
'''set k in knn algorithm '''
self.k_ = k_
def knn_saa(self):
'''runs a knn based on saa to obtain (r,Q) and it reports r_knn and Q_knn in the end. '''
big_r = 1000
zero_ = np.zeros(self.k_)
nbrs = NearestNeighbors(n_neighbors=self.k_, algorithm='ball_tree').fit(self.train_x)
_, knn_te = nbrs.kneighbors(self.test_x)
_, knn_val = nbrs.kneighbors(self.valid_x)
epsilon = 0.1
self.g_knn_total = 0
self.Q_knn = np.squeeze(np.sqrt(2*self.K*self.lambdaa/self.ch)*np.ones((1,self.test_size)))
self.r_knn = np.squeeze(10000*np.ones((1,self.test_size)))
if len(np.shape(self.Q_knn)) == 0:
self.Q_knn = np.expand_dims(self.Q_knn,0)
if len(np.shape(self.r_knn)) == 0:
self.r_knn = np.expand_dims(self.r_knn,0)
# print '(r,Q) is: ', r_knn, Q_knn
for te in range(self.test_size):
notStop = True
i = 0
while notStop:
i += 1
# print "iteration ", i
# reset the value of r,Q
Q_old = self.Q_knn[te]
r_old = self.r_knn[te]
# update r
z = (Q_old*self.ch)/(self.cp*self.lambdaa)
if self.clusters == 1:
index = 0
else:
index = self.ind_test[te,0]
if z >= 0 and z <= 1:
if self.sigma[int(index)] <= 1e-16:
self.r_knn[te] = self.mu[int(index)]
else:
self.r_knn[te] = sts.norm.isf(z,self.mu[int(index)],self.sigma[int(index)]) # isf works with 1-cdf
else:
self.r_knn[te] = -big_r
# update Q
# this nr is based on KNN-SAA
nr = 1/(.0 + self.k_)*sum(np.maximum(self.train_y[knn_te[te]] - self.r_knn[te], zero_))
#nr = zetta*self.sigma[int(index)]
self.Q_knn[te] = np.sqrt((2*self.lambdaa*(self.K+self.cp*nr))/self.ch)
# check if we should stop
if np.abs(self.Q_knn[te]-Q_old) < epsilon:
if np.abs(self.r_knn[te]-r_old) < epsilon:
notStop = False
g = self.ch*(self.r_knn[te] - self.lambdaa*self.l[int(index)] + self.Q_knn[te]/2) + \
self.K*self.lambdaa/self.Q_knn[te] + self.cp*self.lambdaa*nr/self.Q_knn[te]
self.g_knn_total += g
if self.config.if_print_knn_solutions:
print te, '(', self.r_knn[te] ,',', self.Q_knn[te], ')', 'g(r,Q) is: ', g , i, 'iterations'
if self.config.if_print_knn_final_cost:
print self.g_knn_total
def restore_dnn(self):
''' restore a saved dnn network'''
checkpoint = tf.train.get_checkpoint_state(self.model_dir)
if checkpoint and checkpoint.model_checkpoint_path:
self.saver.restore(self.sess, checkpoint.model_checkpoint_path)
print "Successfully loaded:", checkpoint.model_checkpoint_path
else:
print "Could not find old network weights in", self.model_dir
def call_dnn_fp(self):
''' calls the forward pass of train, validation, and the test datasets'''
self.rq_test = self.sess.run(self.y,feed_dict={self.x: self.test_x})
self.test_result = []
self.train_result = []
self.val_result = []
feed_dict={self.x: self.train_x, self.y_: self.train_y, self.c_p:self.shrtg_cost_tr,
self.c_h:self.hld_cost_tr, self.zero:self.zeros_tr
, self.K_:self.order_cost_tr, self.L_:self.l_tr , self.lambdaa_:self.lambdaa_tr}
self.train_result += [self.sess.run(self.cost_function, feed_dict)]
# validation
feed_dict={self.x: self.valid_x, self.y_: self.valid_y, self.c_p:self.shrtg_cost_val,
self.c_h:self.hld_cost_val, self.zero:self.zeros_val,
self.K_:self.order_cost_val, self.L_:self.l_val, self.lambdaa_:self.lambdaa_val}
self.val_result += [self.sess.run(self.cost_function,feed_dict)]
feed_dict={self.x: self.test_x, self.y_: self.test_y, self.c_p:self.shrtg_cost_te,
self.c_h:self.hld_cost_te, self.zero:self.zeros_te,
self.K_:self.order_cost_te, self.L_:self.l_te, self.lambdaa_:self.lambdaa_te}
self.test_result += [self.sess.run([self.cost_function, self.y],feed_dict)]
def get_rq_test(self, x_input, binary_x=False):
''' x =[day,month,department] is the non-binary input value
x_input = [[4,5,12], [1,5,2]]
x_input=[[4,5,12]] '''
if not binary_x:
binary_day = []
binary_month = []
binary_department_24 = []
binary_department_29 = []
A = np.identity(7)
for i in range(7):
binary_day += [A[i : i + 1]]
A = np.identity(12)
for i in range(12):
binary_month += [A[i : i + 1]]
A = np.identity(24)
for i in range(24):
binary_department_24 += [A[i : i + 1]]
A = np.identity(29)
for i in range(29):
binary_department_29 += [A[i : i + 1]]
x_binary = []
for x in x_input:
if self.clusters == 1:
x_binary += [np.ones(1)]
elif self.clusters == 10:
x_binary += [np.concatenate((binary_day[x[0]%2 + 1] , binary_department_24[x[2]%5 + 1]) , axis = 1)]
elif self.clusters == 100:
x_binary += [np.concatenate((binary_day[x[0]%5 + 1] , binary_department_24[x[2]%19 + 1]) , axis = 1)]
elif self.clusters == 200 or self.real_cluster == 103:
x_binary += [np.concatenate((binary_day[x[0]%7] , binary_department_29[x[2]%29]) , axis = 1)]
x_binary = np.squeeze(np.array(x_binary),axis=1)
else:
x_binary = x_input
# print np.shape(x_binary)
feed_dict={self.x: x_binary}
test_y = self.sess.run(self.y, feed_dict)
return (test_y)
def set_network(self):
''' set the best find network structure for EIL cost function'''
if self.clusters == 1 and self.dis == 'normal':
self.nodes = [1, 1, 0, 2]
elif self.clusters == 10 and self.dis == 'normal':
self.nodes = [31, 22, 20, 2]
elif self.clusters == 100 and self.dis == 'normal':
self.nodes = [31, 45, 76, 67, 2]
elif self.clusters == 200 and self.dis == 'normal':
self.nodes = [36, 50, 84, 46, 2]
elif self.clusters == 1 and self.dis == 'beta':
self.nodes = [1, 2, 2, 1, 2]
elif self.clusters == 10 and self.dis == 'beta':
self.nodes = [31, 63, 43, 2]
elif self.clusters == 100 and self.dis == 'beta':
self.nodes = [31, 45, 76, 67, 2]
elif self.clusters == 200 and self.dis == 'beta':
self.nodes = [36, 93, 87, 45, 2]
elif self.clusters == 1 and self.dis == 'lognormal':
self.nodes = [1, 1, 0, 2]
elif self.clusters == 10 and self.dis == 'lognormal':
self.nodes = [31, 61, 59, 2]
elif self.clusters == 100 and self.dis == 'lognormal':
self.nodes = [31, 43, 74, 39, 2]
elif self.clusters == 200 and self.dis == 'lognormal':
self.nodes = [36, 64, 42, 2]
elif self.clusters == 1 and self.dis == 'exponential':
self.nodes = [1, 2, 1, 2]
elif self.clusters == 10 and self.dis == 'exponential':
self.nodes = [31, 15, 10, 2]
elif self.clusters == 100 and self.dis == 'exponential':
self.nodes = [31, 56, 29, 14, 2]
elif self.clusters == 200 and self.dis == 'exponential':
self.nodes = [36, 70, 38, 2]
elif self.clusters == 1 and self.dis == 'uniform':
self.nodes = [1, 2, 2, 1, 2]
elif self.clusters == 10 and self.dis == 'uniform':
self.nodes = [31, 77, 49, 2]
elif self.clusters == 100 and self.dis == 'uniform':
self.nodes = [31, 24, 22, 2]
elif self.clusters == 200 and self.dis == 'uniform':
self.nodes = [36, 52, 71, 49, 2]
self.NoHiLay = len(self.nodes) - 2
def create_mem(self):
''' create the required memory to run the simulation for dnn, knn, and eil algorithms'''
self.dnn_cost = np.zeros((self.items, self.config.sim_period+1))
self.knn_cost = np.zeros((self.items, self.config.sim_period+1))
self.eil_cost = np.zeros((self.items, self.config.sim_period+1))
self.dnn_rQ = np.zeros((self.items, self.config.sim_period+1,2))
self.knn_rQ = np.zeros((self.items, self.config.sim_period+1,2))
self.eil_rQ = np.zeros((self.items, self.config.sim_period+1,2))
self.IL_dnn = np.zeros((self.items, self.config.sim_period+1))
self.IL_knn = np.zeros((self.items, self.config.sim_period+1))
self.IL_eil = np.zeros((self.items, self.config.sim_period+1))
self.AO_init= 0
self.AO_dnn = self.AO_init*np.ones((self.items, self.config.sim_period+1+int(365*max(self.l))))
self.AO_knn = self.AO_init*np.ones((self.items, self.config.sim_period+1+int(365*max(self.l))))
self.AO_eil = self.AO_init*np.ones((self.items, self.config.sim_period+1+int(365*max(self.l))))
def run_simulator(self):
''' This function loads the data and run a (r,Q) simulator to
compare knn, dnn, and eil. For DNN, it loads the pre-trained saved models,
but for knn and eil it calls them on the fly, so knn is quite expensive to run.
To load the dnn, it uses the function set_network, which has the address and info
of the best found network and hyper-parameters.
'''
try:
self.r_new
except:
self.if_print_eil_solutions = False
self.get_EIL_solution()