-
Notifications
You must be signed in to change notification settings - Fork 1
/
solver_base_tf2.py
executable file
·129 lines (104 loc) · 4.04 KB
/
solver_base_tf2.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
# python3
# Make this standard template for testing and training
from __future__ import division
from __future__ import print_function
import sys
import os
import shutil
sys.path.append('%s/gcn' % os.path.dirname(os.path.realpath(__file__)))
import time
import random
import scipy.io as sio
import numpy as np
import scipy.sparse as sp
from multiprocessing import Queue
from copy import deepcopy
import networkx as nx
import tensorflow as tf
from collections import deque
from gcn.utils import *
import warnings
warnings.filterwarnings('ignore')
from runtime_config import flags, FLAGS
# Settings (FLAGS)
from heuristics_mwcds import *
if not hasattr(flags.FLAGS, 'epsilon'):
flags.DEFINE_float('epsilon', 1.0, 'initial exploration rate')
if not hasattr(flags.FLAGS, 'epsilon_min'):
flags.DEFINE_float('epsilon_min', 0.001, 'minimal exploration rate')
if not hasattr(flags.FLAGS, 'epsilon_decay'):
flags.DEFINE_float('epsilon_decay', 0.985, 'exploration rate decay per replay')
if not hasattr(flags.FLAGS, 'gamma'):
flags.DEFINE_float('gamma', 1.0, 'gamma')
# Some preprocessing
num_supports = 1 + FLAGS.max_degree
nsr = np.power(10.0, -FLAGS.snr_db / 20.0)
class Solver(object):
def __init__(self, input_flags, memory_size):
self.feature_size = input_flags.feature_size
self.memory = deque(maxlen=memory_size)
self.reward_mem = deque(maxlen=memory_size)
self.flags = input_flags
self.delta = 0.000001 # prevent empty solution
self.gamma = self.flags.gamma # discount rate
self.epsilon = self.flags.epsilon # exploration rate
self.epsilon_min = self.flags.epsilon_min
self.epsilon_decay = self.flags.epsilon_decay
self.learning_rate = self.flags.learning_rate
self.sess = None
self.saver = None
def _build_model(self):
raise NotImplementedError
def makestate(self, adj, wts_nn):
reduced_nn = wts_nn.shape[0]
norm_wts = np.amax(wts_nn) + 1e-9
if self.flags.predict == 'mwis':
features = np.ones([reduced_nn, self.flags.feature_size])
else:
features = np.multiply(np.ones([reduced_nn, self.flags.feature_size]), wts_nn / norm_wts)
features_raw = features.copy()
features = sp.lil_matrix(features)
if self.flags.predict == 'mwis':
features = preprocess_features(features)
else:
features = sparse_to_tuple(features)
support = simple_polynomials(adj, self.flags.max_degree)
state = {"features": features, "support": support, "features_raw": features_raw}
return state
def act(self, state, train):
raise NotImplementedError
def predict(self, state):
raise NotImplementedError
def memorize(self, state, act_vals, solu, next_state, reward):
self.memory.append((state.copy(), act_vals.copy(), solu.copy(), next_state.copy(), reward))
self.reward_mem.append(reward)
def load(self, name):
ckpt = tf.train.get_checkpoint_state(name)
if ckpt:
with self.sess.as_default():
self.saver.restore(self.sess, ckpt.model_checkpoint_path)
print('loaded ' + ckpt.model_checkpoint_path)
def save(self, name):
with self.sess.as_default():
self.saver.save(self.sess, os.path.join(name, "model.ckpt"))
def mellowmax(self, q_vec, omega, beta):
c = np.max(q_vec)
a_size = np.size(q_vec)
mellow = c + np.log(np.sum(np.exp(omega * (q_vec - c))) / a_size) / omega
return mellow
def utility(self, adj_0, wts_0, train=False):
"""
GCN for per utility function
"""
adj = adj_0.copy()
wts_nn = np.reshape(wts_0, (wts_0.shape[0], self.flags.feature_size))
# GCN
state = self.makestate(adj, wts_nn)
act_vals, _ = self.act(state, train)
gcn_wts = act_vals.numpy()
return gcn_wts, state
# use gpu 0
os.environ['CUDA_VISIBLE_DEVICES'] = str(0)
# Initialize session
config = tf.compat.v1.ConfigProto()
config.gpu_options.allow_growth = True