def action(self, opponent, world): global actions, wait, direction, model, observations, targets, pid_aim, pid_move, pid_turn a, s = angle_between(self, opponent) aim = tools.sign(-self.pid_aim.pid(a*s)) g = self.compute_error(opponent, world) if g.x == 0 and g.y == 0: return # [x, y] = self.location[:] # [x0, y0] = [x+10*g[0], y+10*g[1]] # pygame.draw.line(main.screen, pygame.Color(0, 128, 0), (x,y), (x0,y0), 5) a = Vector2D(-math.sin(self.angle),-math.cos(self.angle)) # [x1, y1] = [x+100*a[0], y+100*a[1]] # pygame.draw.line(main.screen, pygame.Color(0, 0, 128), (x,y), (x1,y1), 5) r = angle(a, g) s = site(a, g) turn = tools.sign(self.pid_turn.pid(min(r, math.fabs(math.pi-r))*s)) move = tools.sign(math.pi/2 - r) shoot = r < math.pi/16 self.perform_action(move, turn, aim, shoot) print self.location
def easy_add_transaction(tx_orig, privkey='default'): tx = copy.deepcopy(tx_orig) if privkey in ['default', 'Default']: if tools.db_existence('privkey'): privkey = tools.db_get('privkey') else: return 'No private key is known, so the tx cannot be signed. ' pubkey = tools.privtopub(privkey) address = tools.make_address([pubkey], 1) if 'count' not in tx: try: tx['count'] = tools.count(address, {}) except: tx['count'] = 1 # add our pubkey if 'pubkeys' not in tx: tx['pubkeys'] = [pubkey] # this is IMPORTANT # when adding new transaction which is signed by us, # this procedure is applied. tx without signatures is signed with our privkey. if 'signatures' not in tx: tx['signatures'] = [tools.sign(tools.det_hash(tx), privkey)] return blockchain.add_tx(tx)
def insert_block(pubkey, rewarded_pubkey): length = tools.db_get('length') if length == -1: # this is the first ever block candidate_block = genesis(pubkey) else: # get the last block prev_block = tools.db_get(length) candidate_block = make_block(prev_block, tools.db_get('txs'), pubkey) txs = copy.deepcopy(candidate_block['txs']) flag = True for tx in txs: if tx['type'] == 'mint': # no need to add reward flag = False if flag: txs = txs + [make_mint(rewarded_pubkey)] candidate_block['txs'] = txs if tools.db_existence('privkey'): privkey = tools.db_get('privkey') else: return 'no private key is known, so the tx cannot be signed. Here is the tx: \n' + str( tools.package(txs).encode('base64').replace('\n', '')) candidate_block['auth_sign'] = tools.sign(tools.det_hash(candidate_block), privkey) candidate_block['auth_pubkey'] = pubkey if candidate_block is None: return else: custom.queues['suggested_blocks'].put(candidate_block)
def sign(tx, privkey): pubkey = tools.privtopub(privkey) address = tools.make_address([pubkey], 1) if 'pubkeys' not in tx: tx['pubkeys'] = [pubkey] if 'signatures' not in tx: tx['signatures'] = [tools.sign(tools.det_hash(tx), privkey)] return tx
def sign(tx, privkey): pubkey=tools.privtopub(privkey) address=tools.make_address([pubkey], 1) if 'pubkeys' not in tx: tx['pubkeys']=[pubkey] if 'signatures' not in tx: tx['signatures']=[tools.sign(tools.det_hash(tx), privkey)] return tx
def easy_add_transaction(tx_orig, privkey, DB): tx=copy.deepcopy(tx_orig) pubkey=tools.privtopub(privkey) try: tx['count']=blockchain.count(pubkey, DB) except: tx['count']=1 tx['signature']=tools.sign(tools.det_hash(tx), privkey) blockchain.add_tx(tx, DB)
def __sign(self, obj): if(isinstance(obj, Transaction)): try: transaction_content = get_hash(obj) signature = sign(self.__private_key, transaction_content) obj.add_signing(signature) obj.add_hash(transaction_content) return True except: return False else: try: block_content = get_hash(obj) signature = sign(self.__private_key, block_content) obj.add_signing(signature) return True except: return False
def sign(tx, privkey): pubkey = tools.privtopub(privkey) address = tools.make_address([pubkey], 1) tx = add_recent_hash(tx) if "pubkeys" not in tx: tx["pubkeys"] = [pubkey] if "signatures" not in tx: tx["signatures"] = [tools.sign(tools.det_hash(tx), privkey)] return tx
def __sign(self, transaction): try: transaction_content = get_hash(transaction) signature = sign(self.__private_key, transaction_content) transaction.add_signing(signature) transaction.add_hash(transaction_content) return True except: return False
def easy_add_transaction(tx_orig, privkey, DB): tx=copy.deepcopy(tx_orig) pubkey=tools.privtopub(privkey) address=tools.make_address([pubkey], 1) try: tx['count']=blockchain.count(address, DB) except: tx['count']=1 tx['signatures']=[tools.sign(tools.det_hash(tx), privkey)] blockchain.add_tx(tx, DB)
def easy_add_transaction(tx_orig, privkey, DB): tx = copy.deepcopy(tx_orig) pubkey = tools.privtopub(privkey) address = tools.make_address([pubkey], 1) try: tx['count'] = blockchain.count(address, DB) except: tx['count'] = 1 tx['signatures'] = [tools.sign(tools.det_hash(tx), privkey)] blockchain.add_tx(tx, DB)
def easy_add_transaction(tx_orig, DB): tx = copy.deepcopy(tx_orig) if 'pubkeys' not in tx: tx['pubkeys']=[DB['pubkey']] try: tx['count'] = tools.count(DB['address'], DB) except: tx['count'] = 1 tx['signatures'] = [tools.sign(tools.det_hash(tx), DB['privkey'])] return(blockchain.add_tx(tx, DB))
def easy_add_transaction(tx_orig, DB): tx = copy.deepcopy(tx_orig) if 'pubkeys' not in tx: tx['pubkeys'] = [DB['pubkey']] try: tx['count'] = tools.count(DB['address'], DB) except: tx['count'] = 1 tx['signatures'] = [tools.sign(tools.det_hash(tx), DB['privkey'])] return (blockchain.add_tx(tx, DB))
def _send_msg_to_background(url, post_data): time_stamp = str(time.time()) s = sign(post_data, time_stamp) header = {'timeStamp': time_stamp, 'sign': s} fail_count = 3 while fail_count: try: resp = requests.post(url, post_data, headers=header) resp = resp.json() if 'SUCCESS' in resp.get('status'): break except Exception as e: fail_count -= 1
def bres_like_parabola(parabola): x1, y1 = parabola.pixels[0] x2, y2 = parabola.pixels[1] p = abs(x1 - x2) increment = tools.sign(x1 - x2) x = x1 - increment * p / 2 y0 = y1 y = 0 x_max = parabola.params['scene_size'].width di = 0 yield x, y0 if not p: return while 0 < x < x_max: dh = di + 2 * p dv = di - 1 - 2 * y dd = di + 2 * (p - y) - 1 if dd < 0: delta = abs(dh) - abs(dd) if delta <= 0: x += increment di += 2 * p else: x += increment y += 1 di += 2 * (p - y) - 1 elif dd > 0: delta = abs(dd) - abs(dv) if delta <= 0: x += increment y += 1 di += 2 * (p - y) - 1 else: y += 1 di += -1 - 2 * y else: x += increment y += 1 di += 2 * (p - y) - 1 yield x, y0 + y yield x, y0 - y
def easy_add_transaction(tx_orig, DB, privkey='default'): tx = copy.deepcopy(tx_orig) try: tx['count'] = tools.count(DB['address'], DB) except: tx['count'] = 1 if privkey=='default': if 'privkey' in DB: privkey=DB['privkey'] else: return('no private key is known, so the tx cannot be signed. Here is the tx: \n'+str(tools.package(tx_orig).encode('base64').replace('\n', ''))) if 'pubkeys' not in tx: tx['pubkeys']=[tools.privtopub(privkey)] tx['signatures'] = [tools.sign(tools.det_hash(tx), privkey)] tools.log('collect winnings 3') return(blockchain.add_tx(tx, DB))
def trade_shares(DB, args): #args = [ PM_id, buy ] privkey = tools.db_get('privkey') pubkey = tools.privtopub(privkey) address = tools.make_address([pubkey], 1) tx = { 'type': 'buy_shares', 'PM_id': args[0], 'buy': csv2vec(args[1]), 'pubkeys': [pubkey], 'count': tools.count(address, {}) } cost = txs_tools.cost_to_buy_shares(tx) tx['price_limit'] = int(cost * 1.01) + 1 tx = tools.unpackage(tools.package(tx)) tx = tools.POW(tx) tx['signatures'] = [tools.sign(tools.det_hash(tx), privkey)] return easy_add_transaction(tx, DB, privkey)
def common_buy_shares(tx, num_states, brainwallet): privkey = tools.det_hash(brainwallet) pubkey = tools.privtopub(privkey) address = tools.make_address([pubkey], 1) tx['pubkeys'] = [pubkey] tx['count'] = tools.count(address, {}) cost = txs_tools.cost_to_buy_shares(tx) tx['price_limit'] = int(cost * 1.01) + 1 print( 'now for a little proof of work. This may take several minutes. The purpose of this pow is to make it more difficult for a front runner to steal your trade.' ) tx = tools.unpackage(tools.package(tx)) tx = tools.POW(tx) tx['signatures'] = [tools.sign(tools.det_hash(tx), privkey)] print('tx for copy/pasting into pushtx: ' + tools.package(tx).encode('base64')) return tx
def easy_add_transaction(tx_orig, DB, privkey='default'): tx = copy.deepcopy(tx_orig) if privkey in ['default', 'Default']: if tools.db_existence('privkey'): privkey=tools.db_get('privkey') else: return('no private key is known, so the tx cannot be signed. Here is the tx: \n'+str(tools.package(tx_orig).encode('base64').replace('\n', ''))) pubkey=tools.privtopub(privkey) address=tools.make_address([pubkey], 1) if 'count' not in tx: try: tx['count'] = tools.count(address, {}) except: tx['count'] = 1 if 'pubkeys' not in tx: tx['pubkeys']=[pubkey] if 'signatures' not in tx: tx['signatures'] = [tools.sign(tools.det_hash(tx), privkey)] return(blockchain.add_tx(tx, DB))
def build_buy_shares(): tx={'type':'buy_shares', 'PM_id':str(raw_input('What is the unique name for this prediction market?\n>'))} num_states=int(raw_input('how many states does this pm have?\n>')) tx['buy']=[] for i in range(num_states): tx['buy'].append(int(raw_input('how many shares do you want to buy of state '+str(i)+'? To sell states, use negative numbers.\n>'))) brainwallet=str(raw_input('What is your brainwallet\n>')) privkey=tools.det_hash(brainwallet) pubkey=tools.privtopub(privkey) address=tools.make_address([pubkey], 1) tx['pubkeys']=[pubkey] tx['count'] = tools.count(address, {}) cost=txs_tools.cost_to_buy_shares(tx) tx['price_limit']=int(cost*1.01) print('now for a little proof of work. This may take several minutes. The purpose of this pow is to make it more difficult for a front runner to steal your trade.') tx=tools.unpackage(tools.package(tx)) tx=tools.POW(tx) tx['signatures']=[tools.sign(tools.det_hash(tx), privkey)] print('tx for copy/pasting into pushtx: '+tools.package(tx).encode('base64')) return tx
def easy_add_transaction(tx_orig, DB, privkey='default'): tx = copy.deepcopy(tx_orig) if privkey in ['default', 'Default']: if tools.db_existence('privkey'): privkey = tools.db_get('privkey') else: return ('no private key is known, so the tx cannot be signed. Here is the tx: \n' + str( tools.package(tx_orig).encode('base64').replace('\n', ''))) pubkey = tools.privtopub(privkey) address = tools.make_address([pubkey], 1) if 'count' not in tx: try: tx['count'] = tools.count(address, {}) except: tx['count'] = 1 if 'pubkeys' not in tx: tx['pubkeys'] = [pubkey] if 'signatures' not in tx: tx['signatures'] = [tools.sign(tools.det_hash(tx), privkey)] return (blockchain.add_tx(tx, DB))
def evaluate_diff_f_g(f,w): 'Returns the average of difference between f and g (g is equivalent as vector w )' count = 0 limit = 10000 diff = 0 # generate random point as out of sample data # check result and count if there is a difference # between target function f and hypothesis function g while count < limit: count = count + 1 x = uniform(-1,1) y = uniform(-1,1) vector = [1,x,y] sign_f = sign(f(x),y) sign_g = h(w,vector) if sign_f != sign_g: diff = diff + 1 return diff/(count*1.0)
def h(w,x): 'Hypothesis function returns w0 x0 + w1 x1 ... + wn xn' res = 0 for i in range(len(x)): res = res + w[i]*x[i] return sign(res)
def f(x): return sign(x[2] - x[1] + 1 / 4 * np.sin(np.pi * x[1]))
def site(a, b): return tools.sign(b[0]*a[1] - b[1]*a[0])
def site(a, b): return tools.sign(b[0] * a[1] - b[1] * a[0])
def f(x): return sign(x[2] -x[1] + 1/4 * np.sin(np.pi * x[1]))
def site(a, b): return tools.sign(a.cross(b))
def perform_action(self, move=0, turn=0, aim=0, shoot=0): self.acceleration = tools.sign(move) self.turn_acceleration = tools.sign(turn) self.aim_acceleration = tools.sign(aim) if shoot: self.shoot()