def _collect_training_data(self): '''Realize training data collection through self-play.''' for i in range(self.n_games): # generate self-play training data self.board = Board(self.row, self.column) self.board.set_state() AI = Alpha(model_file=self.init_model, use_gpu=self.use_gpu) board_states, mcts_probs, current_players = [], [], [] while (True): move, move_probs = AI.self_play(self.row, self.column, self.board.board_state) board_states.append(self.board.current_state()) mcts_probs.append(move_probs) self.board.move(move) current_players.append(self.board.get_cur_player()) end, winner = self.board.who_win() if end: winners = np.zeros(len(current_players)) if winner != 0: winners[np.array(current_players) == winner] = 1.0 winners[np.array(current_players) != winner] = -1.0 print(winners) play_data = zip(board_states, mcts_probs, winners) break play_data = list(play_data)[:] self.episode_len = len(play_data) # print(play_data) # add data to buffer self.buffer.extend(play_data) print(len(self.buffer))
def test_model_finalization(self): # Initialize Alpha alpha = Alpha(2, {0: 2, 1: 2}, {0: LEN_SIMPLE_OPS, 1: 1}) # Set alpha_e for edge of op on ground level alpha.parameters[0][0][(0, 1)] = nn.Parameter(tensor([10., 0., 0., 0.])) alpha.parameters[1][0][(0, 1)] = nn.Parameter(tensor([10., 0.])) #Create simple model model = Model(alpha=alpha, primitives=SIMPLE_OPS, channels_in=1, channels_start=2, stem_multiplier=1, num_classes=5, test_mode=True) # Input x = tensor([[ # feature 1 [[1.]] ]]) # Expected output y = tensor([[ # feature 1 [[2.]] ]]) learnt_model = LegacyLearntModel(model) assert (learnt_model(x).equal(y))
def test_2level(self): ''' Testing HierarchicalOperation create_dag with 2 levels. ''' x = tensor([[ # feature 1 [[1, 1], [1, 1]] ]]) # Initialize Alpha alpha = Alpha(2, {0: 3, 1: 3}, {0: LEN_SIMPLE_OPS, 1: 1}) # Create hierarchical operation hierarchical_op = HierarchicalOperation.create_dag( level=1, alpha=alpha, alpha_dag=alpha.parameters[1][0], primitives=SIMPLE_OPS, channels_in=1) y = tensor([[[[0.7500, 0.7500], [0.7500, 0.7500]], [[1.1250, 1.1250], [1.1250, 1.1250]], [[0.5625, 0.5625], [0.5625, 0.5625]], [[0.84375, 0.84375], [0.84375, 0.84375]], [[0.84375, 0.84375], [0.84375, 0.84375]], [[1.265625, 1.265625], [1.265625, 1.265625]]]]) assert (y.equal(hierarchical_op(x)))
def test_1level(self): ''' Testing MNAS with just 1 level. Equivalent to darts in this case. Only Mixed Operations of primitives on nodes. Only tests base case of create_dag. ''' x = tensor([[ # feature 1 [[1, 1], [1, 1]] ]]) # Initialize Alpha alpha = Alpha(1, {0: 3}, {0: LEN_SIMPLE_OPS}) hierarchical_op = HierarchicalOperation.create_dag( level=0, alpha=alpha, alpha_dag=alpha.parameters[0][0], primitives=SIMPLE_OPS, channels_in=1) y = tensor([[ # feature 1 [[1.5, 1.5], [1.5, 1.5]], # feature 2 [[2.25, 2.25], [2.25, 2.25]] ]]) assert (y.equal(hierarchical_op(x)))
def _AI_player(self): '''the interface for AI Parameters required and updated: board status, which side to play Return: the next gomoku piece coordinate (x, y) Gomoku Board status: 0 means no pieces, 1 means black pieces and -1 means white pieces ''' self.human = False if self.is_start == False: return # AI_program AI = MCTS() AI = Alpha(model_file=self.model_file, use_gpu=False) [x, y] = AI.play(self.row, self.column, self.board) self._draw_piece(x, y, self.is_black) self.board[x][y] = self._ternary_op(1, -1, self.is_black) self.last_x, self.last_y = x, y self._gomoku_who_win() self.is_black = not self.is_black self.l_info.config( text=self._ternary_op('黑方行棋', '白方行棋', self.is_black)) self.human = True
def __init__(self): super(QtradeEnv, self).__init__() self.root_dir = '/Users/liuyehong/Dropbox/CICC/Algorithm_Trading/Platform2/OHLC/data/1Min/' self.list_dir = [d for d in os.listdir(self.root_dir) if '.csv' in d] self.df_dir = np.random.choice(self.list_dir) self.df = pd.read_csv(self.root_dir + self.df_dir) self.alpha = Alpha(self.df) self.cost = 0 #-0.00005 self.interest_rate = 0 / 240 / 240 # internal interest rate (necessary to avoid stuck of long-term training.) self.window = 50 self.cash = 1 self.stock = 0 self.t = self.window + 1 self.i = 0 self.T = len(self.df) self.total_steps = int(self.T / 5.) self.list_asset = np.ones(self.T) self.list_holding = np.ones(self.T) # alpha self.close = self.alpha.close self.high = self.alpha.high self.low = self.alpha.low self.open = self.alpha.open self.vol = self.alpha.vol self.close_diff = self.alpha.close_diff() self.high_diff = self.alpha.high_diff() self.low_diff = self.alpha.low_diff() self.open_diff = self.alpha.open_diff() self.ma = self.alpha.moving_average(window=self.window) self.ema = self.alpha.EMA(window=self.window) self.dema = self.alpha.DEMA(window=self.window) self.kama = self.alpha.KAMA(window=self.window) self.sma = self.alpha.SMA(window=self.window) self.tema = self.alpha.TEMA(window=self.window) self.trima = self.alpha.TRIMA(window=self.window) self.linearreg_slope = self.alpha.LINEARREG_SLOPE(window=self.window) self.mstd = self.alpha.moving_std(window=self.window) self.bollinger_lower_bound = self.alpha.bollinger_lower_bound( window=self.window, width=1) self.bollinger_upper_bound = self.alpha.bollinger_upper_bound( window=self.window, width=1) self.moving_max = self.alpha.moving_max(window=self.window) self.moving_min = self.alpha.moving_min(window=self.window) self.moving_med = self.alpha.moving_med(window=self.window) # Actions of the format Buy x%, Sell x%, Hold, etc. # Action space range must be symetric and the order matters. self.action_space = spaces.Box(low=np.array([-np.inf, -np.inf]), high=np.array([np.inf, np.inf]), dtype=np.float16) # Prices contains the OHCL values for the last five prices self.observation_space = spaces.Box(low=-np.inf, high=np.inf, shape=(1, self.window, 4), dtype=np.float16)
def __init__(self): super(Window,self).__init__() self.set_size(600,600) self.alpha = Alpha(self.get_size()[0], self.get_size()[1], 10) pyglet.clock.schedule_interval(self.update,1.0/30.0)
def run_alpha(self): log = [] log_row = [] for rows in self.text.get(1.0, END): if rows == '\n': if log_row != []: log.append(log_row) log_row = [] if rows.isalpha(): log_row.append(rows) #opened_file = deepcopy(self.opened_file) if self.radio_v.get() == 2: self.alpha = Alpha(log, splines='node') else: self.alpha = Alpha(log) print(self.alpha) print("direct succesor:", self.alpha.ds) print("causality:",self.alpha.cs) print("inversion causality:",self.alpha.inv_cs) print("parralell:",self.alpha.pr) print("no relation:",self.alpha.ind) self.graph_name = self.opened_filename.split('/')[-1][:-4] self.alpha.create_graph(self.graph_name, view=False) self.show_graph()
def __init__(self, str1, str2): str1, str2 = list(str1), list(str2) fixGenomes(str1, str2) #fixGenomes remove genes imapeaveis print 'Genomas apos remocao de genes imapeaveis: \n%r\n%r' % (str1, str2) self.genoma1 = str1 self.genoma2 = str2 fmly1 = getFmly(self.genoma1) fmly2 = getFmly(self.genoma2) # listaDeFamilias = [alpha(posG1, posG2, geneId) para 'gene' em 'Genoma'] self.listaDeFamilias = [Alpha(fmly1[i], fmly2[i], i) for i in fmly1]
def test_2level_model(self): x = tensor([[ # feature 1 [[1., 1.], [1., 1.]] ]]) # Initialize Alpha alpha = Alpha(2, {0: 3, 1: 3}, {0: LEN_SIMPLE_OPS, 1: 1}) model = Model(alpha=alpha, primitives=SIMPLE_OPS, channels_in=1, channels_start=2, stem_multiplier=1, num_classes=5) raise NotImplementedError
def __init__(self, num_levels: int, num_nodes_at_level: Dict[int, int], num_ops_at_level: Dict[int, int], primitives: dict, channels_in: int, beta: float, image_height: int, image_width: int, writer=None, test_mode=False): ''' - Initializes member variables - Registers alpha parameters by creating a dummy alpha using the constructor and using get_alpha_level to get the alpha for a given level. This tensor is wrapped with nn.Parameter to indicate that is a Parameter for this controller (thus requires gradient computation with respect to itself). This nn.Parameter is added to the nn.ParameterList that is self.alphas. - Registers weights parameters by creating a model from aforementioned dummy alpha ''' # Superclass constructor super().__init__() # Initialize member variables self.num_levels = num_levels self.num_nodes_at_level = num_nodes_at_level self.num_ops_at_level = num_ops_at_level self.primitives = primitives self.channels_in = channels_in self.beta = beta self.writer = writer # Initialize Alpha self.alpha = Alpha(num_levels=self.num_levels, num_nodes_at_level=self.num_nodes_at_level, num_ops_at_level=self.num_ops_at_level) # Initialize model with initial alpha, self.model = BetaVAE(alpha=self.alpha, beta=beta, primitives=self.primitives, channels_in=self.channels_in, image_height=image_height, image_width=image_width, writer=writer, test_mode=test_mode) if not test_mode and torch.cuda.is_available(): self.model = self.model.cuda()
def test_initialization(self): num_levels = 3 num_nodes_at_level = {0: 3, 1: 3, 2: 3} num_ops_at_level = {0: 5, 1: 3, 2: 3} testAlpha = Alpha(num_levels, num_nodes_at_level, num_ops_at_level) # Check parameters for i in range(0, num_levels): alpha_i = testAlpha.parameters[i] for op_num in range(0, num_ops_at_level[i + 1]): for node_a in range(0, num_nodes_at_level[i]): for node_b in range(node_a + 1, num_nodes_at_level[i]): if i == 0: num_parameters = num_ops_at_level[i] + 2 else: num_parameters = num_ops_at_level[i] + 1 assert (alpha_i[op_num][(node_a, node_b)].equal( zeros(num_parameters)))
def __init__(self): super(QtradeEnv, self).__init__() self.dir = './data/BTCUSDT.csv' self.df = pd.read_csv(self.dir) self.alpha = Alpha(self.df) self.cost = 0.00 self.interest_rate = 0.0/240/240 # internal interest rate self.window = 50 self.cash = 1 self.stock = 0 self.t = self.window + 1 self.T = len(self.df) self.steps = 0 self.list_asset = np.ones(self.T) self.list_holding = np.ones(self.T) self.list_profit = np.zeros(self.T) # alpha self.close = self.alpha.close self.high = self.alpha.high self.low = self.alpha.low self.open = self.alpha.open self.vol = self.alpha.vol self.close_diff = self.alpha.close_diff() self.high_diff = self.alpha.high_diff() self.low_diff = self.alpha.low_diff() self.open_diff = self.alpha.open_diff() self.ma = self.alpha.moving_average(window=self.window) self.ema = self.alpha.EMA(window=self.window) self.mstd = self.alpha.moving_std(window=self.window) self.bollinger_lower_bound = self.alpha.bollinger_lower_bound(window=self.window, width=1) self.bollinger_upper_bound = self.alpha.bollinger_upper_bound(window=self.window, width=1) # Actions of the format Buy x%, Sell x%, Hold, etc. # Action space range must be symetric and the order matters. self.action_space = spaces.Box( low=np.array([-np.inf, -np.inf]), high=np.array([np.inf, np.inf]), dtype=np.float16) # Prices contains the OHCL values for the last five prices self.observation_space = spaces.Box( low=-np.inf, high=np.inf, shape=(1, self.window, 9), dtype=np.float16)
from alpha import Alpha def read_logs(path): logs = [] for line in open(path): logs.append(tuple(line.split())) return logs logs = read_logs("../logs/log2.txt") alpha = Alpha(logs) alpha.create_graph('graph2')
def __init__(self, num_levels: int, num_nodes_at_level: Dict[int, int], num_ops_at_level: Dict[int, int], primitives: dict, channels_in: int, channels_start: int, stem_multiplier: int, num_classes: int, loss_criterion, num_cells: int, writer=None, test_mode=False): ''' - Initializes member variables - Registers alpha parameters by creating a dummy alpha using the constructor and using get_alpha_level to get the alpha for a given level. This tensor is wrapped with nn.Parameter to indicate that is a Parameter for this controller (thus requires gradient computation with respect to itself). This nn.Parameter is added to the nn.ParameterList that is self.alphas. - Registers weights parameters by creating a model from aforementioned dummy alpha ''' # Superclass constructor super().__init__() # Initialize member variables self.num_levels = num_levels self.num_nodes_at_level = num_nodes_at_level self.num_ops_at_level = num_ops_at_level self.primitives = primitives self.channels_in = channels_in self.channels_start = channels_start self.stem_multiplier = stem_multiplier self.num_classes = num_classes self.loss_criterion = loss_criterion self.writer = writer self.num_cells = num_cells self.test_mode = test_mode self.graph_added = False # Initialize Alpha for both types of cells # Normal Cell self.alpha_normal = Alpha(num_levels=self.num_levels, num_nodes_at_level=self.num_nodes_at_level, num_ops_at_level=self.num_ops_at_level, randomize=True) self.alpha_reduce = Alpha(num_levels=self.num_levels, num_nodes_at_level=self.num_nodes_at_level, num_ops_at_level=self.num_ops_at_level, randomize=True) # Initialize model with initial alpha self.model = Model(alpha_normal=self.alpha_normal, alpha_reduce=self.alpha_reduce, primitives=self.primitives, channels_in=self.channels_in, channels_start=self.channels_start, stem_multiplier=self.stem_multiplier, num_classes=self.num_classes, num_cells=num_cells, writer=writer, test_mode=test_mode) if not test_mode and torch.cuda.is_available(): self.model = self.model.cuda()
from alpha import Alpha import torch import util alpha_norm = Alpha(1, {0: 7}, {0: 8}) alpha_reduce = Alpha(1, {0: 7}, {0: 8}) for edge in alpha_norm.parameters[0][0]: alpha_norm.parameters[0][0][edge].requires_grad = False for edge in alpha_reduce.parameters[0][0]: alpha_reduce.parameters[0][0][edge].requires_grad = False # Set to DARTS Alpha Normal alpha_norm.parameters[0][0][(0, 2)][2] = 1 alpha_norm.parameters[0][0][(0, 3)][2] = 1 alpha_norm.parameters[0][0][(0, 4)][2] = 1 alpha_norm.parameters[0][0][(1, 2)][2] = 1 alpha_norm.parameters[0][0][(1, 3)][2] = 1 alpha_norm.parameters[0][0][(1, 4)][8] = 1 alpha_norm.parameters[0][0][(1, 5)][8] = 1 alpha_norm.parameters[0][0][(2, 5)][5] = 1 # Set to DARTS Alpha Reduce alpha_reduce.parameters[0][0][(0, 2)][1] = 1 alpha_reduce.parameters[0][0][(0, 4)][1] = 1 alpha_reduce.parameters[0][0][(1, 2)][1] = 1 alpha_reduce.parameters[0][0][(1, 3)][1] = 1 alpha_reduce.parameters[0][0][(1, 5)][1] = 1 alpha_reduce.parameters[0][0][(2, 3)][8] = 1 alpha_reduce.parameters[0][0][(2, 4)][8] = 1 alpha_reduce.parameters[0][0][(2, 5)][8] = 1
config = { 'begin_date': '20140328', 'end_date': '20170801', 'data_path': data_path, 'result_path': result_path, 'intraday_path': intraday_path, 'h5_path_5min': h5_path_5min, 'h5_path_1min': h5_path_1min, 'data_source': ('volume_price', 'money_flow', 'style' ) # ('volume_price','inter_day','money_flow','financial') } print u'计算alpha因子' print config psx_alpha = Alpha(config) #psx_alpha.work_data(data) psx_alpha.load_data() time1 = time.time() print 'load data time = %s seconds' % (time1 - time0) psx_alpha.get_new(psx_alpha.alpha_worldquant101_60) #运行alpha_daily_000的因子逻辑 #psx_alpha.get_new(psx_alpha.alpha_recount_000) #psx_alpha.get_intraday_1min([alpha.fstcount_1min_000]) #运行日内因子,1min #psx_alpha.get_intraday_1min([alpha.fstcount_5min_000]) #运行日内因子计算,5min