import os
import random

os.environ["KERAS_BACKEND"] = "tensorflow"

with_gpu = True # not massive difference in speed, but faster with GPU
if not with_gpu:
    os.environ["CUDA_VISIBLE_DEVICES"] = ""

import numpy as np
import tensorflow as tf

from keras.callbacks import ModelCheckpoint, TensorBoard
from keras.models import Sequential, load_model
from keras.layers.core import Dense, Dropout, Flatten, Lambda
from keras.layers.convolutional import Conv2D, Cropping2D
from keras.layers.pooling import AveragePooling2D, MaxPooling2D

from sklearn.model_selection import train_test_split
from sklearn.utils import shuffle

import csv
import cv2

FLAGS = tf.app.flags.FLAGS

tf.app.flags.DEFINE_string(
    'data_dir',
    'data/',
    "directory containing driving_log.csv and IMG/*.jpg"
)

tf.app.flags.DEFINE_string(
    'model_file',
    'model.h5',
    "file where model should be saved"
)

tf.app.flags.DEFINE_float(
    'correction',
    0.025, # previously 0.015
    "correction to be applied to center-steering on left and right images"
) #TODO: calibrate further

tf.app.flags.DEFINE_bool(
    'include_sides',
    True,
    "determines whether the images taken from left/right sides are used"
)

tf.app.flags.DEFINE_bool(
    'augment_data',
    False,
    "determines whether data augmentation is used"
)

tf.app.flags.DEFINE_integer(
    'epochs',
    3,
    "number of training epochs"
)

tf.app.flags.DEFINE_integer(
    'raw_batch_size',
    64,
    "batches are constructed from this many base pictures (pre-augmentation)"
) # with flipping + side views, augmented b.s. is 6 * FLAGS.raw_batch_size

tf.app.flags.DEFINE_bool(
    'reset',
    True,
    "If False, previous model will be loaded at the start."
)

def get_data():
    print("Loading data in directory {}...".format(FLAGS.data_dir))
    data = []
    with open(os.path.join(FLAGS.data_dir, "driving_log.csv"), 'r') as f:
        reader = csv.reader(f)
        next(reader, None) # skip headers
        for row in reader:
            data.append(row)
    return data

def get_augmentation(images):
    hsvs = np.array([cv2.cvtColor(img, cv2.COLOR_RGB2HSV) for img in images])
    for i in range(hsvs.shape[0]):
        hsv = (np.random.randint(low=0.5, high=1.5) * hsvs[i,:,:,2]).round()
        hsv = np.minimum.reduce([hsv, 255*np.ones_like(hsv)]) # avoid overflow
        hsvs[i,:,:,2] = hsv.astype(np.uint8)
    return [cv2.cvtColor(hsv, cv2.COLOR_HSV2RGB) for hsv in hsvs]

def generator(data):
    while True:
        random.shuffle(data)
        for first in range(0, len(data), FLAGS.raw_batch_size):
            last = min(first + FLAGS.raw_batch_size, len(data))
            center_images = []
            left_images = []
            right_images = []
            steering_measurements = []
            for c, l, r, steering, throttle, brake, speed in data[first:last]:
                # strip is used to remove leading whitespace
                img_c = cv2.imread(os.path.join(FLAGS.data_dir, c.strip()))
                img_c = cv2.cvtColor(img_c, cv2.COLOR_BGR2RGB)
                img_l = cv2.imread(os.path.join(FLAGS.data_dir, l.strip()))
                img_l = cv2.cvtColor(img_l, cv2.COLOR_BGR2RGB)
                img_r = cv2.imread(os.path.join(FLAGS.data_dir, r.strip()))
                img_r = cv2.cvtColor(img_r, cv2.COLOR_BGR2RGB)
                center_images.append(img_c)
                left_images.append(img_l)
                right_images.append(img_r)
                steering = float(steering)
                steering_measurements.append(steering)
            images = np.array(center_images)
            steering_measurements = np.array(steering_measurements)
            if FLAGS.include_sides:
                images = np.concatenate((
                    images,
                    np.array(left_images),
                    np.array(right_images)
                ))
                steering_measurements = np.concatenate((
                    steering_measurements,
                    steering_measurements + FLAGS.correction,
                    steering_measurements - FLAGS.correction
                ))
            if FLAGS.augment_data:
                augmented = get_augmentation(images)
                images = np.concatenate((images, augmented))
                steering_measurements = np.concatenate((
                    steering_measurements,
                    steering_measurements
                ))
            X_batch = np.concatenate((images, np.fliplr(images)))
            y_batch = np.concatenate(
                (steering_measurements, -steering_measurements)
            )
            yield X_batch, y_batch

def commaai_net():
    model = Sequential()
    model.add(Cropping2D(
        cropping=((60, 30), (0, 0)),
        input_shape=(160, 320, 3)
    ))
    model.add(Lambda(lambda x: x/127.5 - 1.0))
    model.add(Conv2D(filters=4,kernel_size=3,strides=(1,2),activation='elu'))
    model.add(Conv2D(filters=4,kernel_size=3,strides=(1,2),activation='elu'))
    model.add(Conv2D(filters=4,kernel_size=5,strides=(2,3),activation='elu'))
    model.add(Flatten())
    model.add(Dropout(0.2))
    model.add(Dense(512, activation='elu'))
    model.add(Dropout(0.5))
    model.add(Dense(1))
    return model

def nvidia_net():
    # https://arxiv.org/pdf/1604.07316.pdf
    model = Sequential()
    model.add(Cropping2D(
        cropping=((60, 30), (0, 0)), # lectures suggest ((50,20),(0,0))
        input_shape=(160, 320, 3)
    )) # shape (70,320,3)
    model.add(Lambda(lambda img: tf.image.resize_images(img, (35, 160))))
    model.add(Lambda(lambda x: x/127.5 - 1.0))
    model.add(Conv2D(
        filters=24,
        kernel_size=5,
        strides=(1,2), # paper uses (2,2) but our input dims are different
        activation='relu',
        padding="valid"
    ))
    model.add(Conv2D(
        filters=36,
        kernel_size=5,
        strides=(2,2),
        activation='relu',
        padding="valid"
    ))
    model.add(Conv2D(
        filters=48,
        kernel_size=3,
        strides=(2,2),
        activation='relu',
        padding="valid"
    ))
    model.add(Conv2D(
        filters=64,
        kernel_size=3,
        strides=(2,2),
        activation='relu',
        padding="valid"
    ))
    model.add(Conv2D(
        filters=64,
        kernel_size=1,
        strides=(2,2),
        activation='relu',
        padding="valid"
    ))
    model.add(Flatten())
    model.add(Dense(1164, activation='relu'))
    model.add(Dense(100, activation='relu'))
    model.add(Dense(50, activation='relu'))
    model.add(Dense(10, activation='relu'))
    model.add(Dense(1))
    return model

def mod_lenet():
    model = Sequential()
    model.add(Cropping2D(
        cropping=((60, 30), (0, 0)),
        input_shape=(160, 320, 3)
    )) # shape (70,320,3)
    model.add(Lambda(lambda img: tf.image.resize_images(img, (35, 160))))
    model.add(Lambda(lambda x: x/127.5 - 1.0))
    model.add(Conv2D(
        filters=32,
        kernel_size=5,
        strides=(1,2),
        activation='relu',
        padding="valid"
    ))
    model.add(AveragePooling2D())
    model.add(Conv2D(
        filters=32,
        kernel_size=5,
        strides=(1,2),
        activation='relu',
        padding="same"
    ))
    model.add(AveragePooling2D())
    model.add(Dropout(0.2))
    model.add(Flatten())
    model.add(Dense(32, activation='relu'))
    model.add(Dense(32, activation='relu'))
    model.add(Dense(32, activation='relu'))
    model.add(Dense(1))
    return model

def encoder_net():
    model = Sequential()
    model.add(Cropping2D(
        cropping=((60, 30), (0, 0)),
        input_shape=(160, 320, 3)
    )) # shape (70,320,3)
    model.add(Lambda(lambda img: tf.image.resize_images(img, (35, 160))))
    model.add(Lambda(lambda x: x/127.5 - 1.0))
    model.add(Dropout(0.2))
    model.add(Conv2D(
        filters=32,
        kernel_size=5,
        strides=(1,2),
        activation='relu',
        padding="valid"
    ))
    model.add(AveragePooling2D())
    model.add(Conv2D(
        filters=32,
        kernel_size=5,
        strides=(1,2),
        activation='relu',
        padding="same"
    ))
    model.add(AveragePooling2D())
    model.add(Flatten())
    model.add(Dense(16, activation='relu'))
    model.add(Dense(4, activation='relu'))
    model.add(Dense(16, activation='relu'))
    model.add(Dense(16, activation='relu'))
    model.add(Dense(16, activation='relu'))
    model.add(Dense(1))
    return model

def get_model():
    #return commaai_net()
    #return nvidia_net()
    #return mod_lenet()
    return encoder_net()

def main(_):
    data = get_data()
    random.shuffle(data)
    training_data = data[:int(0.8*len(data))]
    validation_data = data[int(0.8*len(data)):]
    train_gen = generator(training_data)
    valid_gen = generator(validation_data)
    model = get_model()
    if not FLAGS.reset:
        model = load_model(FLAGS.model_file)
    print(model.summary())
    model.compile(loss='mse', optimizer='adam')
    model.fit_generator(
        generator=train_gen,
        steps_per_epoch=int(np.ceil(len(training_data)/FLAGS.raw_batch_size)),
        epochs=FLAGS.epochs,
        callbacks=[
            ModelCheckpoint(FLAGS.model_file),
            TensorBoard(
                write_images=True,
                batch_size=FLAGS.raw_batch_size
            )
        ],
        verbose=1,
        validation_data=valid_gen,
        validation_steps=int(np.ceil(len(validation_data)/FLAGS.raw_batch_size))
    )

if __name__ == '__main__':
    tf.app.run()


#二分法+双指针（从中间开始，反方向行走）

class Solution:

    def kClosestNumbers(self,A,target,k):
        if not A:
            return []
        index = self.findFirstIndex(A,target)
        left,right = index - 1,index
        result = []

        for i in range(k):
            if left < 0:
                result.append(A[right])
                right += 1
            elif right >= len(A):
                result.append(A[left])
                left -= 1
            else:
                if target-A[left] <= A[right]-target:
                    result.append(A[left])
                    left -= 1
                else:
                    result.append(A[right])
                    right += 1
        return result

    def findFirstIndex(self,A,target):
        #第一个大于等于的数的下标
        start,end = 0,len(A)-1
        while start+1 < end:
            mid = start+(end-start)//2
            if A[mid] >= target:
                end = mid
            else:
                start = mid
        if A[start] >= target:
            return start
        if A[end] >= target:
            return end
        return len(A)-1

if __name__ == '__main__':
    a_list = [1,2,3,4,5,6]
    tar = 3
    k =3
    obj = Solution()
    dup = obj.kClosestNumbers(a_list,tar,k)
    print(dup)

460_在排序数组中找最接近的K个数.py


# --------------------------------------------------------
# Fast R-CNN
# Copyright (c) 2015 Microsoft
# Licensed under The MIT License [see LICENSE for details]
# Written by Ross Girshick
# --------------------------------------------------------

"""The data layer used during training to train a Fast R-CNN network.

RoIDataLayer implements a Caffe Python layer.
"""

import caffe
from fast_rcnn.config import cfg
from vrd.wsd.mini_batch import get_minibatch
import numpy as np
import yaml
#from multiprocessing import Process, Queue
import pdb
import zl_config as C
import glog
class RoIDataLayerEbBinaryLogLoss(caffe.Layer):
    """Fast R-CNN data layer used for training."""

    def _shuffle_roidb_inds(self):
        """Randomly permute the training roidb."""
        if cfg.TRAIN.ASPECT_GROUPING:
            widths = np.array([r['width'] for r in self._roidb])
            heights = np.array([r['height'] for r in self._roidb])
            horz = (widths >= heights)
            vert = np.logical_not(horz)
            horz_inds = np.where(horz)[0]
            vert_inds = np.where(vert)[0]
            inds = np.hstack((
                np.random.permutation(horz_inds),
                np.random.permutation(vert_inds)))
            inds = np.reshape(inds, (-1, 2))
            row_perm = np.random.permutation(np.arange(inds.shape[0]))
            inds = np.reshape(inds[row_perm, :], (-1,))
            self._perm = inds
        else:
            self._perm = np.random.permutation(np.arange(len(self._roidb)))
        self._cur = 0

    def _get_next_minibatch_inds(self):
        """Return the roidb indices for the next minibatch."""
        if self._cur + cfg.TRAIN.IMS_PER_BATCH >= len(self._roidb):
            self._shuffle_roidb_inds()

        db_inds = self._perm[self._cur:self._cur + cfg.TRAIN.IMS_PER_BATCH]
        self._cur += cfg.TRAIN.IMS_PER_BATCH
        return db_inds

    def _get_next_minibatch(self):
        """Return the blobs to be used for the next minibatch.
        If cfg.TRAIN.USE_PREFETCH is True, then blobs will be computed in a
        separate process and made available through self._blob_queue.
        """
        if cfg.TRAIN.USE_PREFETCH:
            return self._blob_queue.get()
        else:
            db_inds = self._get_next_minibatch_inds()
            minibatch_db = [self._roidb[i] for i in db_inds]
            return get_minibatch(minibatch_db, self._num_classes)

    def set_roidb(self, roidb):
        """Set the roidb to be used by this layer during training."""
        self._roidb = roidb
        self._shuffle_roidb_inds()

    def setup(self, bottom, top):
        """Setup the RoIDataLayer."""

        # parse the layer parameter string, which must be valid YAML
        layer_params = yaml.load(self.param_str)

        self._num_classes = layer_params['num_classes']
        self._name_to_top_map = {}

        # data blob: holds a batch of N images, each with 3 channels
        idx = 0
        top[idx].reshape(cfg.TRAIN.IMS_PER_BATCH, 3,
                         max(cfg.TRAIN.SCALES), cfg.TRAIN.MAX_SIZE)
        self._name_to_top_map['data'] = idx
        idx += 1

        ##todo necessary?
        # top[idx].reshape(1, 3)
        # self._name_to_top_map['im_info'] = idx
        # idx += 1
        # rois blob: holds R regions of interest, each is a 5-tuple
        # (n, x1, y1, x2, y2) specifying an image batch index n and a
        # rectangle (x1, y1, x2, y2)
        top[idx].reshape(1, 5, 1, 1)
        self._name_to_top_map['gt_boxes'] = idx
        idx += 1

        # labels blob: R categorical labels in [0, ..., K] for K foreground
        # classes plus background
        top[idx].reshape(1, 1, 1, 1)
        self._name_to_top_map['labels'] = idx
        idx += 1

        top[idx].reshape(1, 20, 1, 1)
        self._name_to_top_map['labels_v'] = idx
        idx += 1
        #top[idx].reshape(1, 1000, 1, 1)
        #self._name_to_top_map['word_embeddings'] = idx
        #idx += 1

        #top[idx].reshape(1, 20, 1, 1)
        #self._name_to_top_map['regression_targets'] = idx
        #idx += 1

        #print 'RoiDataLayer: name_to_top:', self._name_to_top_map
        #assert len(top) == len(self._name_to_top_map)

    def forward(self, bottom, top):
        """Get blobs and copy them into this layer's top blob vector."""
        # todo: modify mini_batch
        blobs = self._get_next_minibatch()
        labels = blobs['labels']
        gt_boxes = blobs['gt_boxes']
        im_info = blobs['im_info']
        #gt_boxes = gt_boxes
        #blobs['labels']=blobs['gt_boxes']
        #blobs['word_embeddings'] = np.zeros((1,1,1,1),np.float32)
        #blobs['regression_targets'] = np.zeros((1,20,1,1),np.float32)

        #for blob_name, blob in blobs.iteritems():
        #    top_ind = self._name_to_top_map[blob_name]
        #    shape = blob.shape
        #    top[top_ind].reshape(*(blob.shape))
        #    # Copy data into net's input blobs
        #    top[top_ind].data[...] = blob.astype(np.float32, copy=False)

        top[0].reshape(*blobs['data'].shape)
        top[0].data[...] = blobs['data']

        gt_boxes = gt_boxes.reshape((gt_boxes.shape[0],gt_boxes.shape[1],1,1))
        zeros = np.zeros((gt_boxes.shape[0],1,1, 1), dtype=gt_boxes.dtype)
        all_rois = np.concatenate((zeros, gt_boxes),axis=1)

        top[1].reshape(*all_rois.shape)
        top[1].data[...] = all_rois
        #labels = np.concatenate((labels,np.array([0,0,0])))
        labels = np.unique(labels)
        labels_v = np.zeros((20))

        labels_v[...]=-1
        labels_v[labels.astype(np.int32)-1]=1
        #labels_v = np.tile(labels_v,labels.shape[0])

        labels_v = labels_v.reshape((1,20))
        #for i in xrange(labels.shape[0]):
        #    labels_v[i,labels[i]]=1
        labels = labels.reshape((labels.shape[0],1,1,1))
        #labels[-1,0,0,0]=0
        top[2].reshape(*labels.shape)
        top[2].data[...] = labels

        top[3].reshape(*labels_v.shape)
        top[3].data[...] =labels_v# labels

        #embeddings = np.zeros((gt_boxes.shape[0],1,1, 1), dtype=gt_boxes.dtype)
        #top[3].reshape(*embeddings.shape)
        #top[3].data[...] = embeddings

        #regression_targets = np.zeros((gt_boxes.shape[0],20,1, 1), dtype=gt_boxes.dtype)
        #top[4].reshape(*regression_targets.shape)
        #top[4].data[...] = regression_targets
        #glog.info('here')
        #pdb.set_trace()

    def backward(self, top, propagate_down, bottom):
        """This layer does not propagate gradients."""
        pass

    def reshape(self, bottom, top):
        """Reshaping happens during the call to forward."""
        pass


#!/usr/bin/env python2
# Import libraries
try:
    import pygame_sdl2
    pygame_sdl2.import_as_pygame()
    import pygame, sys, menu, math, random
    from pygame.locals import *
except:
    import pygame, sys, menu, math, random
    from pygame.locals import *

def play(fps):
    # Set the width and height of the window
    width, height = 640, 480
    # Create the window
    screen = pygame.display.set_mode((width, height), pygame.HWSURFACE | pygame.DOUBLEBUF)
    # Set pressed keys
    keys = [False, False, False, False]
    # Set player position
    playerpos=[100,100]
    # Make an list for the accuracy
    acc=[0,0]
    # Make an list for where the arrows are
    arrows=[]
    # Set the timer for spawning badgers
    badtimer=100
    badtimer1=0
    # Make an list for where the badgers are
    badguys=[[800,100]]
    # Set your health value
    healthvalue=194
    # Set the wait times
    waitforexit=0
    waitforarrows=0
    waitforballoons=0
    waitforballoons2=2
    # Set displaying balloon on/off
    balloon1display = False
    balloon2display = False
    # Set start ticks
    startticks = pygame.time.get_ticks()
    # Set title
    pygame.display.set_caption("Bunny and Badgers")

    # Load images
    # Load the players image
    player = pygame.image.load("resources/images/dude.png").convert_alpha()
    # Load the background image
    grass = pygame.image.load("resources/images/grass.png").convert()
    # Load the image of the castles
    castle = pygame.image.load("resources/images/castle.png").convert_alpha()
    # Load the image for the arrows
    arrow = pygame.image.load("resources/images/bullet.png").convert_alpha()
    # Load the image for the badgers
    badguyimg1 = pygame.image.load("resources/images/badguy.png").convert_alpha()
    badguyimg2 = pygame.image.load("resources/images/badguy2.png").convert_alpha()
    badguyimg3 = pygame.image.load("resources/images/badguy3.png").convert_alpha()
    badguyimg4 = pygame.image.load("resources/images/badguy4.png").convert_alpha()
    badguyimg = badguyimg1
    # Load the overlays
    greenoverlay = pygame.image.load("resources/images/greenoverlay.png").convert_alpha()
    redoverlay = pygame.image.load("resources/images/redoverlay.png").convert_alpha()
    # Load the healthbar images
    healthbar = pygame.image.load("resources/images/healthbar.png").convert_alpha()
    health = pygame.image.load("resources/images/health.png").convert_alpha()
    # Load the text balloons
    balloon1 = pygame.image.load("resources/images/balloon1.png").convert_alpha()
    balloon2 = pygame.image.load("resources/images/balloon2.png").convert_alpha()

    # Set positions
    castle1 = (0,height/16)
    castle2 = (0,height/3.5)
    castle3 = (0,height/2)
    castle4 = (0,height/1.4)

    # Create a clock
    clock = pygame.time.Clock()

    # Keep looping through
    running = 1
    exitcode = 0
    while running:
        badtimer-=1
        # Draw the background
        for x in range(width/grass.get_width()+1):
            for y in range(height/grass.get_height()+1):
                screen.blit(grass,(x*100,y*100))
        # Draw the castles
        screen.blit(castle, castle1)
        screen.blit(castle, castle2)
        screen.blit(castle, castle3)
        screen.blit(castle, castle4)
        # Set player position and rotation
        if True:
            position = pygame.mouse.get_pos()
            angle = math.atan2(position[1]-(playerpos[1]+32),position[0]-(playerpos[0]+26))
            playerrot = pygame.transform.rotate(player, 360-angle*57.29)
            playerpos1 = (playerpos[0]-playerrot.get_rect().width/2, playerpos[1]-playerrot.get_rect().height/2)
            screen.blit(playerrot, playerpos1)

        # Draw arrows
        for bullet in arrows:
            index=0
            velx=math.cos(bullet[0])*10
            vely=math.sin(bullet[0])*10
            bullet[1]+=velx
            bullet[2]+=vely
            if bullet[1]<-64 or bullet[1]>width or bullet[2]<-64 or bullet[2]>height:
                arrows.pop(index)
            index+=1
            for projectile in arrows:
                arrow1 = pygame.transform.rotate(arrow, 360-projectile[0]*57.29)
                screen.blit(arrow1, (projectile[1], projectile[2]))

        # Change the image of the badgers
        if badguyimg == badguyimg1:
            badguyimg = badguyimg2
        elif badguyimg == badguyimg2:
            badguyimg = badguyimg3
        elif badguyimg == badguyimg3:
            badguyimg = badguyimg4
        elif badguyimg == badguyimg4:
            badguyimg = badguyimg1

        # Draw badgers
        if badtimer==0:
            badheight1 = height/9.6
            badheight2 = height/1.1
            badheight = random.randint(int(badheight1), int(badheight2))
            badguys.append([width, badheight])
            badtimer=100-(badtimer1*2)
            if badtimer1>=35:
                badtimer1=35
            else:
                badtimer1+=5
        index=0
        for badguy in badguys:
            if badguy[0]<-64:
                badguys.pop(index)
            badguy[0]-=7
            # Attack castle
            badrect=pygame.Rect(badguyimg.get_rect())
            badrect.top=badguy[1]
            badrect.left=badguy[0]
            if badrect.left<64:
                healthvalue -= random.randint(5,20)
                badguys.pop(index)
            # Check for collisions
            index1=0
            for bullet in arrows:
                bullrect=pygame.Rect(arrow.get_rect())
                bullrect.left=bullet[1]
                bullrect.top=bullet[2]
                if badrect.colliderect(bullrect):
                    acc[0]+=1
                    badguys.pop(index)
                    arrows.pop(index1)
                index1+=1
            # Next bad guy
            index+=1
        # Draw badgers
        for badguy in badguys:
            screen.blit(badguyimg, badguy)
        # Draw clock
        font = pygame.font.Font("freesansbold.ttf", 24)
        survivedtext = font.render(str((90000-pygame.time.get_ticks()+startticks)/60000)+":"+str((90000-pygame.time.get_ticks()+startticks)/1000%60).zfill(2), True, (0,0,0))
        textRect = survivedtext.get_rect()
        textRect.topright=[width-5, 5]
        screen.blit(survivedtext, textRect)
        # Draw health bar
        screen.blit(healthbar, (5,5))
        for health1 in range(healthvalue):
            screen.blit(health, (health1+8,8))
        # Loop through the events
        for event in pygame.event.get():
            if event.type == pygame.KEYDOWN:
                # Move up
                if event.key in (K_w, K_UP):
                    keys[0]=True
                # Move left
                elif event.key in (K_a, K_LEFT):
                    keys[1]=True
                # Move down
                elif event.key in (K_s, K_DOWN):
                    keys[2]=True
                # Move right
                elif event.key in (K_d, K_RIGHT):
                    keys[3]=True
                # Quit by pressing back
                elif event.key==K_AC_BACK:
                    menu.launch()
            if event.type == pygame.KEYUP:
                # Move up
                if event.key in (K_w, K_UP):
                    keys[0]=False
                # Move left
                elif event.key in (K_a, K_LEFT):
                    keys[1]=False
                # Move down
                elif event.key in (K_s, K_DOWN):
                    keys[2]=False
                # Move right
                elif event.key in (K_d, K_RIGHT):
                    keys[3]=False
            # Check if you pressed a mouse button for shooting arrows
            if event.type==pygame.MOUSEBUTTONDOWN:
                if waitforarrows == 0:
                    position=pygame.mouse.get_pos()
                    acc[1]+=1
                    arrows.append([math.atan2(position[1]-(playerpos1[1]+32),position[0]-(playerpos1[0]+26)),playerpos1[0]+32,playerpos1[1]+32])
                    # Set wait time for arrows in frames
                    waitforarrows=15
                    if waitforballoons2:
                        waitforballoons2-=1
                    else:
                        # Choose balloon
                        balloonnr = random.randint(1, 2)
                        if balloonnr == 1:
                            balloon1display = True
                        elif balloonnr == 2:
                            balloon2display = True
                        waitforballoons2=2
        if waitforarrows:
            waitforarrows-=1
            waitforballoons = waitforarrows
        # Display balloon
        if waitforballoons:
            waitforballoons-=1
            if balloon1display:
                screen.blit(balloon1, (playerpos[0]+10, playerpos[1]-60))
            elif balloon2display:
                screen.blit(balloon2, (playerpos[0]+10, playerpos[1]-60))
        else:
            balloon1display = False
            balloon2display = False

        # Move player
        # Up
        if keys[0]:
            playerpos[1]-=5
        # Down
        elif keys[2]:
            playerpos[1]+=5
        # Left
        if keys[1]:
            playerpos[0]-=5
        # Right
        elif keys[3]:
            playerpos[0]+=5

        # Win/Lose check
        # Win
        if (pygame.time.get_ticks()-startticks)>=90000:
            running=0
            exitcode=1
        # Lose
        if healthvalue<=0:
            running=0
            exitcode=0
        if acc[1]!=0:
            accuracy=acc[0]*1.0/acc[1]*100
        else:
            accuracy=0

        # Flip the display
        pygame.display.flip()

        # Lock fps
        clock.tick(fps)

    # Win/lose display
    # Lose
    if exitcode==0:
        # Initialize the font
        pygame.font.init()
        # Set font
        font = pygame.font.Font("freesansbold.ttf", 24)
        bigfont = pygame.font.Font("freesansbold.ttf", 48)
        # Render text
        gameover = bigfont.render("Game over!", True, (255,0,0))
        gameoverRect = gameover.get_rect()
        gameoverRect.centerx = screen.get_rect().centerx
        gameoverRect.centery = screen.get_rect().centery-24
        text = font.render("Accuracy: "+str(accuracy)+"%", True, (255,0,0))
        textRect = text.get_rect()
        textRect.centerx = screen.get_rect().centerx
        textRect.centery = screen.get_rect().centery+24
        # Draw red overlay
        for x in range(width/redoverlay.get_width()+1):
            for y in range(height/redoverlay.get_height()+1):
                screen.blit(redoverlay,(x*100,y*100))
        # Draw text
        screen.blit(gameover, gameoverRect)
        screen.blit(text, textRect)
    # Win
    else:
        # Initialize the font
        pygame.font.init()
        # Set font
        font = pygame.font.Font("freesansbold.ttf", 24)
        bigfont = pygame.font.Font("freesansbold.ttf", 48)
        # Render text
        youwin = bigfont.render("You win!", True, (0,255,0))
        youwinRect = youwin.get_rect()
        youwinRect.centerx = screen.get_rect().centerx
        youwinRect.centery = screen.get_rect().centery-24
        text = font.render("Accuracy: "+str(accuracy)+"%", True, (0,255,0))
        textRect = text.get_rect()
        textRect.centerx = screen.get_rect().centerx
        textRect.centery = screen.get_rect().centery+24
        # Draw green overlay
        for x in range(width/greenoverlay.get_width()+1):
            for y in range(height/greenoverlay.get_height()+1):
                screen.blit(greenoverlay,(x*100,y*100))
        # Draw text
        screen.blit(youwin, youwinRect)
        screen.blit(text, textRect)
    # Exit automatic when the game is stopped
    while 1:
        waitforexit+=1
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
        if waitforexit == 1000:
            menu.launch()
        # Update the screen
        pygame.display.flip()


#! C:/Users/vlfgh/Desktop/memory/
# -*- coding: utf-8 -*-

import win32gui
import win32ui
import win32con
import pid_to_hwnd
import finPID_by_processName
import isThereLol
import time

import getUser_resolution
def screenshot(name):

    user_H_W =  getUser_resolution.getUserResolution()
    h = 1280
    w = 800

    d = ((user_H_W[0]-h)//2)
    dd = ((user_H_W[1]-w)//2)

    if isThereLol:
        hwnd = pid_to_hwnd.get_hwnds_for_pid(finPID_by_processName.find_Process_Name("LolClient.exe"))

        if len(hwnd)==0:
            time.sleep(10)
        hwnd = hwnd[0]
        win32gui.SetWindowPos(hwnd,win32con.HWND_NOTOPMOST,d,dd,1280,800,0)
        wDC = win32gui.GetWindowDC(hwnd)
        dcObj=win32ui.CreateDCFromHandle(wDC)
        cDC=dcObj.CreateCompatibleDC()
        dataBitMap = win32ui.CreateBitmap()
        dataBitMap.CreateCompatibleBitmap(dcObj, h,w)
        cDC.SelectObject(dataBitMap)
        cDC.BitBlt((0,0),(h, w) , dcObj, (0,0), win32con.SRCCOPY)
        dataBitMap.SaveBitmapFile(cDC, name+".png")
        # Free Resources
        dcObj.DeleteDC()
        #cDC.DeleteDC()
        win32gui.ReleaseDC(hwnd, wDC)
        win32gui.DeleteObject(dataBitMap.GetHandle())
    else:
        print('getScreenChot_by_wintitle is false')
        return False


# -*- coding: utf-8 -*-
import tensorflow as tf

def attention(inputs, attention_size, time_major=False, return_alphas=False):
    """
    Attention mechanism layer which reduces RNN/Bi-RNN outputs with Attention vector.
    The idea was proposed in the article by Z. Yang et al., "Hierarchical Attention Networks
     for Document Classification", 2016: http://www.aclweb.org/anthology/N16-1174.
    Variables notation is also inherited from the article

    Args:
        inputs: The Attention inputs.
            Matches outputs of RNN/Bi-RNN layer (not final state):
                In case of RNN, this must be RNN outputs `Tensor`:
                    If time_major == False (default), this must be a tensor of shape:
                        `[batch_size, max_time, cell.output_size]`.
                    If time_major == True, this must be a tensor of shape:
                        `[max_time, batch_size, cell.output_size]`.
                In case of Bidirectional RNN, this must be a tuple (outputs_fw, outputs_bw) containing the forward and
                the backward RNN outputs `Tensor`.
                    If time_major == False (default),
                        outputs_fw is a `Tensor` shaped:
                        `[batch_size, max_time, cell_fw.output_size]`
                        and outputs_bw is a `Tensor` shaped:
                        `[batch_size, max_time, cell_bw.output_size]`.
                    If time_major == True,
                        outputs_fw is a `Tensor` shaped:
                        `[max_time, batch_size, cell_fw.output_size]`
                        and outputs_bw is a `Tensor` shaped:
                        `[max_time, batch_size, cell_bw.output_size]`.
        attention_size: Linear size of the Attention weights.
        time_major: The shape format of the `inputs` Tensors.
            If true, these `Tensors` must be shaped `[max_time, batch_size, depth]`.
            If false, these `Tensors` must be shaped `[batch_size, max_time, depth]`.
            Using `time_major = True` is a bit more efficient because it avoids
            transposes at the beginning and end of the RNN calculation.  However,
            most TensorFlow data is batch-major, so by default this function
            accepts input and emits output in batch-major form.
        return_alphas: Whether to return attention coefficients variable along with layer's output.
            Used for visualization purpose.
    Returns:
        The Attention output `Tensor`.
        In case of RNN, this will be a `Tensor` shaped:
            `[batch_size, cell.output_size]`.
        In case of Bidirectional RNN, this will be a `Tensor` shaped:
            `[batch_size, cell_fw.output_size + cell_bw.output_size]`.
    """

    if isinstance(inputs, tuple):
        # In case of Bi-RNN, concatenate the forward and the backward RNN outputs.
        inputs = tf.concat(inputs, 2)

    if time_major:
        # (T,B,D) => (B,T,D)
        inputs = tf.array_ops.transpose(inputs, [1, 0, 2])

    hidden_size = inputs.shape[2].value  # D value - hidden size of the RNN layer

    # Trainable parameters
    w_omega = tf.Variable(tf.random_normal([hidden_size, attention_size], stddev=0.1))
    b_omega = tf.Variable(tf.random_normal([attention_size], stddev=0.1))
    u_omega = tf.Variable(tf.random_normal([attention_size], stddev=0.1))

    with tf.name_scope('v'):
        # Applying fully connected layer with non-linear activation to each of the B*T timestamps;
        #  the shape of `v` is (B,T,D)*(D,A)=(B,T,A), where A=attention_size
        v = tf.tanh(tf.tensordot(inputs, w_omega, axes=1) + b_omega)

    # For each of the timestamps its vector of size A from `v` is reduced with `u` vector
    vu = tf.tensordot(v, u_omega, axes=1, name='vu')  # (B,T) shape
    alphas = tf.nn.softmax(vu, name='alphas')         # (B,T) shape

    # Output of (Bi-)RNN is reduced with attention vector; the result has (B,D) shape
    output = tf.reduce_sum(inputs * tf.expand_dims(alphas, -1), 1)

    if not return_alphas:
        return output
    else:
        return output, alphas

'''
inputs是一个形如(batch_size, seq_len, word_size)的张量；
函数返回一个形如(batch_size, seq_len, position_size)的位置张量。
'''
def Position_Embedding(inputs, position_size):
    batch_size,seq_len = tf.shape(inputs)[0],tf.shape(inputs)[1]
    position_j = 1. / tf.pow(10000., \
                             2 * tf.range(position_size / 2, dtype=tf.float32 \
                            ) / position_size)
    position_j = tf.expand_dims(position_j, 0)
    position_i = tf.range(tf.cast(seq_len, tf.float32), dtype=tf.float32)
    position_i = tf.expand_dims(position_i, 1)
    position_ij = tf.matmul(position_i, position_j)
    position_ij = tf.concat([tf.cos(position_ij), tf.sin(position_ij)], 1)
    position_embedding = tf.expand_dims(position_ij, 0) \
                         + tf.zeros((batch_size, seq_len, position_size))
    return position_embedding

'''
inputs是一个二阶以上的张量，代表输入序列，比如形如(batch_size, seq_len, input_size)的张量；
seq_len是一个形如(batch_size,)的张量，代表每个序列的实际长度，多出部分都被忽略；
mode分为mul和add，mul是指把多出部分全部置零，一般用于全连接层之前；
add是指把多出部分全部减去一个大的常数，一般用于softmax之前。
'''
def Mask(inputs, seq_len, mode='mul'):
    if seq_len == None:
        return inputs
    else:
        mask = tf.cast(tf.sequence_mask(seq_len), tf.float32)
        for _ in range(len(inputs.shape)-2):
            mask = tf.expand_dims(mask, 2)
        if mode == 'mul':
            return inputs * mask
        if mode == 'add':
            return inputs - (1 - mask) * 1e12

'''
普通的全连接
inputs是一个二阶或二阶以上的张量，即形如(batch_size,...,input_size)。
只对最后一个维度做矩阵乘法，即输出一个形如(batch_size,...,ouput_size)的张量。
'''
def Dense(inputs, ouput_size, bias=True, seq_len=None):
    input_size = int(inputs.shape[-1])
    W = tf.Variable(tf.random_uniform([input_size, ouput_size], -0.05, 0.05))
    if bias:
        b = tf.Variable(tf.random_uniform([ouput_size], -0.05, 0.05))
    else:
        b = 0
    outputs = tf.matmul(tf.reshape(inputs, (-1, input_size)), W) + b
    outputs = tf.reshape(outputs, \
                         tf.concat([tf.shape(inputs)[:-1], [ouput_size]], 0)
                        )
    if seq_len != None:
        outputs = Mask(outputs, seq_len, 'mul')
    return outputs

'''
Multi-Head Attention的实现
'''
def Self_Attention(Q, K, V, nb_head, size_per_head, Q_len=None, V_len=None):
    #对Q、K、V分别作线性映射
    Q = Dense(Q, nb_head * size_per_head, False)
    Q = tf.reshape(Q, (-1, tf.shape(Q)[1], nb_head, size_per_head))
    Q = tf.transpose(Q, [0, 2, 1, 3])
    K = Dense(K, nb_head * size_per_head, False)
    K = tf.reshape(K, (-1, tf.shape(K)[1], nb_head, size_per_head))
    K = tf.transpose(K, [0, 2, 1, 3])
    V = Dense(V, nb_head * size_per_head, False)
    V = tf.reshape(V, (-1, tf.shape(V)[1], nb_head, size_per_head))
    V = tf.transpose(V, [0, 2, 1, 3])
    #计算内积，然后mask，然后softmax
    A = tf.matmul(Q, K, transpose_b=True) / tf.sqrt(float(size_per_head))
    A = tf.transpose(A, [0, 3, 2, 1])
    A = Mask(A, V_len, mode='add')
    A = tf.transpose(A, [0, 3, 2, 1])
    A = tf.nn.softmax(A)
    #输出并mask
    O = tf.matmul(A, V)
    O = tf.transpose(O, [0, 2, 1, 3])
    O = tf.reshape(O, (-1, tf.shape(O)[1], nb_head * size_per_head))
    O = Mask(O, Q_len, 'mul')
    return O


import requests
from sys import argv
from requests_futures.sessions import FuturesSession
from concurrent.futures import ThreadPoolExecutor

class Check():

    def __init__(self):
        self.session = FuturesSession(max_workers=30)
        # Quick and dirty
        self.fileIn = argv[1]
        self.fileOut = argv[2]
        self.bigURL = argv[3] if len(argv) >= 3 else "https://www.soundcloud.com/"

    def fetch(self,url):
        # Need to spoof
        header = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.75 Safari/537.36'}
        resp =  requests.get(url,headers=header,timeout=5.0,stream=False)
        # Not sure if we need to do this because of stream=False
        resp.close()

        return resp

    def run(self):
        #our list
        usernames = []

        with open(self.fileIn,"r") as f:
            for line in f:
                line = str(line).rstrip()
                if len(line) >= 3:
                    usernames.append(self.bigURL+line)
        f.close()

        # Cutting down any unnecessary requests
        usernames = list(set(usernames))

        responses = []
        out = []
        with ThreadPoolExecutor() as executor:
            for url in executor.map(self.fetch, usernames):
                responses.append(url)

        for re in responses:
            if re.status_code == 404:
                out.append(re.url)

        with open(self.fileOut,"w+r") as f:
            for line in out:
                f.write(line+"\n")

checking = Check()
checking.run()


from .hogwarts import Hogwarts
from potterworld.sub2.learn_spells import learn_spells
from potterworld.sub2.quidditch import quidditch
from potterworld.sub2.duel import duel_voldemort
from .sorting_hat_song import print_song
import random

class Wizard(Hogwarts):
    """
    This Wizard class initiates the wizard class object and assign class attributes. Uses Hogwarts superclass attributes.
    This class provides functions and information that will be used by wizard to perform different tasks.

    Attributes
    ----------
    name (str)
    wand_movement_pattern (str)
    house_cup_points (int)
    health_points_for_duel (int)
    house_quidditch_rating (float)
    wand_wood_type (str)
    wand_core (str)
    wand_length (int)
    wand_movement_pattern (str)

    Methods
    -------
    gringotts : Converts canadian dollar number to wizarding currency
    get_wand : Uses the superclass to assign a wand to the wizard and print the wand details
    sorting_hat : Sorts the wizard into a Hogwarts house (Gryffindor, Hufflepuff, Ravenclaw, Slytherin)
    learn_spells : Allows wizard to learn several spells, and also allows them to create their own special spell.
    night_crawl : Takes the wizard through a narrative simulating a late night exploration of hogwarts castle.
    play_quidditch : Different houses at Hogwarts compete with each other by playing Quidditch using this function.
    house_cup_winner : Outputs house cup points score and the winner of the house cup.
    fight_voldemort : The wizard fights Lord Voldemort to death in this duel.
    display : Displays the current class object attributes and gives the user instructions for the next step.
    """

    def __init__(self, name):
        '''
        Initiate the wizard class object and assign class attributes. Uses superclass attributes.

        Arguments:
        name -- (str) the name of the wizard
        '''
        Hogwarts.__init__(self, name)
        self.health_points_for_duel = 0
        self.best_friend = "yourself for now, call sorting_hat() to get assigned to a house and make friends"
        self.house_value = "not assigned yet"
        self.house_mascot = "not assigned yet"
        self.house_colors = "not assigned yet"
        self.house_ghost = "not assigned yet"
        self.house_founders_relic = "not assigned yet"
        self.spells_dict = dict()
        self.house_cup_points = 0
        self.house_quidditch_rating = 0
        self.learn_spells_flag = 0
        self.wand_wood_type = None
        self.wand_core = None
        self.wand_length = None
        self.wand_movement_pattern = None

    def __str__(self):
        '''
        Prints output of when str() is called on the class object.

        Arguments:
        None
        '''
        message = f'Welcome to the magic world, {self.name}.\nRun my.display() for further current status.\n'
        print(message)

    def gringotts(self, currency):
        '''
        Converts canadian dollar number to wizarding currency

        Arguments:
        currency -- (int, float) the amount of canadian currency
        '''
        Hogwarts.gringotts(self, currency)

    def get_wand(self):
        '''
        Uses the superclass to assign a wand to the wizard and print the wand details

        Arguments:
        None
        '''
        if self.wand_wood_type == None:
            (self.wand_wood_type, self.wand_core, self.wand_length) = Hogwarts.get_wand(self)
            message = f'You now have a magical wand!\n'  \
                    f"It's wood type is {self.wand_wood_type}, core is made of {self.wand_core}, and it's length is {self.wand_length} inches."
            print(message)
            print(' ')
            print("Now it's time to get yourself into one of the four prestigious Houses at Hogwarts using 'sorting hat'")
            print('Example: my.sorting_hat()')
            print(' ')
        else:
            print(f"You already have a wand. It's wood type is {self.wand_wood_type}, core is made of {self.wand_core}, and it's length is {self.wand_length} inches.")
            print("Now it's time to get yourself into one of the four prestigious Houses at Hogwarts using 'sorting hat'")
            print('Example: my.sorting_hat()')
            print(' ')

    def sorting_hat(self):
        '''
        Sorts the wizard into a Hogwarts house (Gryffindor, Hufflepuff, Ravenclaw, Slytherin)

        Arguments:
        None
        '''
        if self.House == 'no':

            print('you will now be sorted into a hogwarts house. The sorting hat will now sing its song!')
            print('.........')
            print('......')
            print('...')
            print_song()
            print('...')
            print('......')
            print('.........')
            print(' ')
            print('Do you wish to choose the House yourself (or) are you okay with random house assignment ?\n')
            user_input = input("Enter y to choose your house, or enter any other character if not interested : ")
            if user_input.lower() == 'y':
                print(' ')
                house_input = input(f'Enter one of these characters ( g r h s ) which represent {Hogwarts.hogwarts_houses_list} : ')
                if house_input.lower() == 'g':
                    self.House = Hogwarts.hogwarts_houses_list[0]
                    self.best_friend = random.choice(Hogwarts.friends_list[self.House])
                elif house_input.lower() == 'r':
                    self.House = Hogwarts.hogwarts_houses_list[1]
                    self.best_friend = random.choice(Hogwarts.friends_list[self.House])
                elif house_input.lower() == 'h':
                    self.House = Hogwarts.hogwarts_houses_list[2]
                    self.best_friend = random.choice(Hogwarts.friends_list[self.House])
                elif house_input.lower() == 's':
                    self.House = Hogwarts.hogwarts_houses_list[3]
                    self.best_friend = random.choice(Hogwarts.friends_list[self.House])
                else:
                    print(f"\nYour input '{house_input}' did not match the given options ( g r h s ), you will now be randomly assigned to a House.\n")
                    self.House = random.choice(Hogwarts.hogwarts_houses_list)
                    self.best_friend = random.choice(Hogwarts.friends_list[self.House])
                print(' ')
            else:
                print(f'\nYou will now be randomly assigned to a House.\n')
                self.House = random.choice(Hogwarts.hogwarts_houses_list)
                self.best_friend = random.choice(Hogwarts.friends_list[self.House])
            self.house_value = Hogwarts.value_list[self.House]
            self.house_mascot = Hogwarts.mascot_list[self.House]
            self.house_colors = Hogwarts.colors_list[self.House]
            self.house_ghost = Hogwarts.ghost_list[self.House]
            self.house_founders_relic = Hogwarts.relic_list[self.House]
            self.house_cup_points = 50
            self.health_points_for_duel = 100
            self.house_quidditch_rating = Hogwarts.default_quidditch_rating[self.House]
            self.display()
        else:
            print(f'You are already assigned to {self.House} house, and your best friend is {self.best_friend}.')

    def learn_spells(self):
        """
        This function first displays all the spells learned by the wizard that they can use with their magic wand.

        If the wizard is interested in making their own wand movement for a special spell,
        this funciton creates that wand movement pattern based on inputs from the wizard.
        This special spell will help the wizard when they fight Lord Voldemort.

        Arguments:
        None
        """
        self.learn_spells_flag = 1
        obj = learn_spells()
        self.spells_dict = obj.get_all_spells()
        print('Great! These are the spells you learned.\n')
        for key, value in self.spells_dict.items():
            print(f'{key} : {value}')
        self.wand_movement_pattern = obj.get_wand_movement_pattern()
        if self.wand_movement_pattern == 'random':
            print('\nYou did not learn any special spell with magic wand movement. A random spell will be used when you fight.')
        else:
            print(f'\nGreat! The special spell you learned is {self.wand_movement_pattern}')
            print('Remember to use this when you fight Lord Voldemort')
        print("\nNow it's time for some fun!\n")
        print('Go for a night crawl (or) Play Quidditch (or) fight Lord Voldemort')
        print('Examples:')
        print('my.night_crawl()')
        print('my.play_quidditch()')
        print('my.fight_voldemort()')
        print(' ')
        print("You can call the 'my.display()' method at any time to see your current attribute information.")

    def night_crawl(self):
        '''
        Takes the wizard through a narrative simulating a late night exploration of hogwarts castle.
        The user makes a few decisions that affect the course of the exploration.
        End result is a gain or loss of house points.

        Arguments:
        None
        '''
        print('''You have decided to sneak out of your dormitory and explore the castle after hours.
Throughout this adventure, you will have to make some decisions. Hopefully you don't get caught by any professors.\n
Your first decision: Would you like to explore the "Great Hall" or the "Astronomy Tower"?\n''')
        while True:
            location = input('For "Great Hall" enter "g", For "Astronomy Tower" enter "a":')
            if location == 'a':
                dest = 'Astronomy Tower'
                print('\nOff to the Astronomy Tower!\n')
                break
            elif location == 'g':
                dest = 'Great Hall'
                print('\nOff to the Great Hall!\n')
                break
            else:
                print('please enter "g" or "a"')
                continue

        input("Press 'Enter' to continue")

        print(f'''\nOn your way to the {dest}. You hear in the distance the janitor, August Filch, calling for his cat, Mrs. Norris.
You continue walking and come accross a scroll on the floor.\n''')
        while True:
            map = input('If you would like to pick up the scroll, input "y". If you would like to leave it, input "n":')
            if map == 'y':
                print('''\nYou picked up the Marauder's Map. It helps you to see where other people are located in the castle.
What a great find!\n''')
                break
            elif map == "n":
                print('''\nYou decided not to pick up the Maurauder's Map. It helps the owner to see where other people in the castle are located.
Hopefully you will not regret that decision later... You continue onward.\n''')
                break
            else:
                print('please enter "y" or "n"')
        input("Press 'Enter' to continue")

        print(f'''You have arrived at the {dest}. In the northeast corner of the {dest}, you hear a 'meow' noise coming from the cabinets.
You decide to investigate.''')
        input("Press 'Enter' to continue")

        print('\nThere are 5 cabinets labelled 1, 2, 3, 4, 5.\n')
        cat_loc =  random.randint(1,5)
        while True:
            try:
                cab = int(input('Which cabinet would you like to unlock? Input a single integer 1, 2, 3, 4, or 5:'))
                if cab == cat_loc:
                    print('\nCongratulations! You found Mrs. Norris. Now you should find Mr. Filch to give him the good news.')
                    break
                else:
                    print('\nNothing in here. Try another cabinet.\n')
                    continue
            except ValueError:
                print('\nYou must enter a valid integer. Please try again.\n')
                continue

        print(f'''As you search the castle for Mr. Filch, you hear footsteps coming from the other end of the corridor.
If only you had a way of finding out who it is...''')

        input("Press 'Enter' to continue")

        if map == 'y':
            print('''\nYou remembered that you have the Marauder's Map.
You pull it out and see that the person approaching you is Professor McGonagall.
You hide behind a suit of armour until she is out of sight.\n''')
            input("Press 'Enter' to continue")
            print('You then use the map and locate Mr. Filch. He is in the potions classroom. You head over there and return his cat to him.\n')
            input("Press 'Enter' to continue")
            print(f'''When you hand him the cat, he happily says,
'Thank you {self.name}, I'm so glad you found Mrs. Norris. I'm awarding 20 house points to your house. What is your house again?'\n
You respond 'It's {self.House} Sir'.
Filch then says, 'Well then, I'm awarding 25 house points to {self.House}. Now run along to bed'\n''')
            self.house_cup_points = self.house_cup_points + 25
            input("Press 'Enter' to continue")
            print(f'''You make it back to your dormitory with a smile on your face.
You can't wait for the morning so you can tell {self.best_friend} and the rest of the house the good news.''')

        if map == 'n':
            print('''\nIf only you had the Marauder's Map. You would then be able to tell who it is.
You decide to jump behind a suit of armour to hide.\n''')
            input("Press 'Enter' to continue")
            print(f'''As the footsteps draw closer, you peek your head out to see who it is.
To your dismay, Professor McGonagall sees your head and shouts,
"{self.name}! What are you doing out of bed. I'm removing 25 house points from {self.House} and assigning you detention for one week.
Now get off to bed. I hope you have learned your lesson."\n''')
            self.house_cup_points = self.house_cup_points - 25
            input("Press 'Enter' to continue")
            print(f'''You make it back to your dormitory with a glum look on your face.
You are dreading the morning when you will have to tell {self.best_friend} and the rest of the house the bad news.''')

    def play_quidditch(self):
        """
        Different houses at Hogwarts compete with each other by playing Quidditch using this function.

        In the first game, winner is selected based on some randomness and also based on house rating of the wizard.
        And in the second game, this function gets inputs from the wizard for catching a snitch.
        House points are awarded or deducted from the house in each game.

        Arguments:
        None
        """
        try:
            self.house_cup_points = quidditch(self.house_quidditch_rating, self.house_cup_points, self.House)
        except:
            print('\nUnexpected error while playing Quidditch, please try again.\n')
        print(' ')
        print('Go for a night crawl (or) Play Quidditch again (or) fight Lord Voldemort')
        print('Examples:')
        print('my.night_crawl()')
        print('my.play_quidditch()')
        print('my.fight_voldemort()')
        print('\nIf you would like to find out who won the house cup at the end of the school year, call the method my.house_cup_winner()')
        print(' ')

    def house_cup_winner(self):
        '''
        Outputs house cup points score and the winner of the house cup.

        Arguments:
        None
        '''
        print(f'{self.House} house now has a total of {self.house_cup_points} house cup points.')
        if self.house_cup_points > 100:
            print(f'\nCongratulations! {self.House} won the house cup!')
        else:
            temp_dict = list(Hogwarts.hogwarts_houses_list)
            winner_house = random.choice(temp_dict.remove(self.House))
            print('\nYou did not win the house cup. This years winner is {winner_house}.')

    def fight_voldemort(self):
        """
        The wizard fights Lord Voldemort to death in this duel.

        In alternating order, user casts a spell, then voldemort casts a spell.
        Spells used by the wizard and Lord Voldemort are displayed, and they lose some energy each time a spell is cast.
        Wizard can improve their chances of winning by using the wand movement that they learned earlier.

        Arguments:
        None
        """
        self.health_points_for_duel = 100
        if self.learn_spells_flag == 0:
            print("You haven't learned any spells yet.")
            print("You need to go to charms class with Professor Flitwick to learn some magic spells.")
            print('Example: my.learn_spells()')
        else:
            game_over = 0
            while game_over == 0:
                try:
                    (result, your_spell, opponent_spell) = duel_voldemort(self.wand_movement_pattern)
                except:
                    print('\nUnexpected error while fighting Lord Voldemort, please try again.\n')
                self.health_points_for_duel -= 20
                print(f"\nLord Voldemort's spell was {opponent_spell} : {self.spells_dict[opponent_spell]}.")
                print(f"\nYour spell was {your_spell} : {self.spells_dict[your_spell]}.")
                if result == 0:
                    print(f'you have {self.health_points_for_duel} % energy to cast spells.\n')
                if result == 0 and self.health_points_for_duel > 0:
                    print(f'Keep fighting...')
                elif result == 1:
                    print("*******************************")
                    print("You win! Lord Voldemort is dead.")
                    print("*******************************")
                    game_over = 1
                else:
                    print("You lost the duel, better luck next time.")
                    game_over = 1
            print(' ')
            print('Go for a night crawl (or) Play Quidditch again (or) fight Lord Voldemort')
            print('Examples:')
            print('my.night_crawl()')
            print('my.play_quidditch()')
            print('my.fight_voldemort()')
            print(' ')

    def display(self):
        '''
        Displays the current class object attributes and gives the user instructions for the next step.

        Arguments:
        None
        '''
        message = f'Hello wizard {self.name}.\n'  \
                  f'You are currently assigned to {self.House} house in Hogwarts magic school.\n'  \
                  f'Your best friend is {self.best_friend}.\n'  \
                  f"Your magical wand wood type is {self.wand_wood_type}, core is made of {self.wand_core}, and it's length is {self.wand_length}.\n"   \
                  f'You currently have {self.Golden_Galleons} Golden Galleons, {self.Silver_Sickles} Silver Sickles, {self.Bronze_Knuts} Bronze Knuts.\n'  \
                  f'Your house value is {self.house_value}, '  \
                  f'house mascot is {self.house_mascot}, '  \
                  f'house colors are {self.house_colors}, '  \
                  f'house ghost is {self.house_ghost}, '  \
                  f'house founders relic is {self.house_founders_relic}.\n'  \
                  f'You have {self.house_cup_points} house cup points.\n'  \
                  f'Your house quidditch rating is {self.house_quidditch_rating}.\n'  \
                  f'You have {self.health_points_for_duel} health points for a fight with Lord Voldemort.'
        print(message)
        if self.learn_spells_flag == 1:
            print(' ')
            print("Now it's time for some fun!")
            print('Go for a night crawl (or) Play Quidditch (or) fight Lord Voldemort')
            print('Examples:')
            print('my.night_crawl()')
            print('my.play_quidditch()')
            print('my.fight_voldemort()')
            print(' ')
        else:
            print(' ')
            print("You have not learned any spells yet. It's time to go to charms class with Professor Flitwick to learn some magic spells!")
            print('Example: my.learn_spells()')
            print(' ')


from sys import exit
from random import randint
from textwrap import dedent

class Scene(object):

    def enter(self):
        print("This scene is not yet configured.")
        print("Subclass it and implement enter().")
        exit(1)

class Death(Scene):

    quips = [
        "You died, You kinda suck it this.",
        "Your Mom would be proud.....if she were smarter."
        "Such a luser.",
        "I've a small puppy that's better at this.",
        "You're worse them your Dad's jokes."
    ]

    def enter(self):
        print(Death.quips[randint(0, len(self.quips)-1)])
        exit(1)

class MagicForest(Scene):

    def enter(self):
        print(dedent("""
            You enter the magic forest. There is an amazing light around you.
            You feel a warm, calming feeling.
            In the distance a light is shown.
            What are you going to do?
        """))

        action = input("> ")

        if action == "walk":
            print(dedent("""
            You walk towards the light.
            """))
            return 'Light_fountain'

        elif action == "stay":
            pass


'''
info:解析gml文件中各个节点中的label转换为A.B.C...的形式
'''
from collections import defaultdict

def parse_label(line): #parse label to api
    str = ""
    nclass = ""
    nfunction = ""
    if(line.startswith("L")):
        str = line[1:]
    else:
        str = line
    #print str
    str = str.rpartition("->")
    nclass = str[0]
    nfunction = str[2]
    #print nfunction
    nclass = nclass[0:-1]
    nclass = nclass.replace('/','.')
    nfunction = nfunction.rpartition(")")
    type = nfunction[2].rpartition('[')[0]
    nfunction = type + nfunction[0] + nfunction[1]
    api = "<" + nclass + ": " +nfunction + ">"

    return api

if __name__ == '__main__':
    line = "Lcom/unipay/account/UnipayAccountPlatform;->refreshPoint(Landroid/os/Handler;)V [access_flags=public] @ 0x18e4cc"
    line2 = "Ljava/util/concurrent/locks/ReentrantLock;-><init>()V"
    line3 = "Lu/aly/dg;->l()Lu/aly/db; [access_flags=public abstract] @ 0x0"

    parse_label(line2)
    parse_label(line3)


import os

from flask import url_for

from werkzeug.utils import secure_filename

from utils import allowed_file

class UploadFileLogic(object):
    """Logic class for taking an HTML uploaded from a flask form, and placing it in a folder"""
    def __init__(self, config):
        self._config = config

    def upload(self, request):
        # check if the post request has the file part
        if 'file' not in request.files:
            print(f"No file part. Redirecting to {request.url}")
            return request.url

        file = request.files['file']
        # if user does not select file, browser also
        # submit an empty part without filename
        if file.filename == '':
            print(f"No file selected. Redirecting to {request.url}")
            return request.url

        if file and allowed_file(file.filename):
            filename_secured = secure_filename(file.filename)

            if not os.path.isdir(os.path.join(self._config['UPLOAD_FOLDER'], request.form['ticker'])):
                os.mkdir(os.path.join(self._config['UPLOAD_FOLDER'], request.form['ticker']))

            file.save(os.path.join(
                f"{self._config['UPLOAD_FOLDER']}/{request.form['ticker']}",
                filename_secured
            ))
            return url_for('process_file', ticker=request.form['ticker'], filename=filename_secured)


'''
https://github.com/jeremite/channel-attribution-model/blob/master/process_data.py

'''

import numpy as np
import random
import tensorflow as tf
import tf_slim as slim
# from keras.utils import to_categorical
from keras.utils import np_utils
from keras.preprocessing.text import Tokenizer
from sklearn.preprocessing import StandardScaler, MinMaxScaler
from pandas.api.types import is_string_dtype, is_numeric_dtype
import warnings
import pandas as pd

warnings.filterwarnings("ignore")

def cat(data, field, leng, s=','):
    data[field] = data[field].map(lambda x: x.split(s))
    data[leng] = data[field].map(lambda x: len(x))

def most_fre(lt):
    return max(lt, key=lt.count)

# most_fre('north')

def sca(col):
    col = [int(v) for v in col]
    return [(v - min(col)) / (max(col) - min(col)) for v in col]

def train_cats(df):
    """Change any columns of strings in a panda's dataframe to a column of
    categorical values. This applies the changes inplace.
    """
    for n, c in df.items():
        if is_string_dtype(c):
            df[n] = c.astype('category').cat.as_ordered()

def process_data(data, seq_length=20):
    # data_a = data_att.loc[:,['path_id','path','total_conversions','last_time_lapse','null_conversion']]
    data.dropna(axis=0, how='any',inplace=True)  # 删除了所有na的,axis=0删除包含缺失值的行，how='any'只要有缺失值出现就删除该行，inplace=True在原数据上进行操作
    # merge target variables

    # str -> list
    cat(data, 'path', "leng_path", s='>')
    cat(data, 'marketing_area', "leng_area", s=',')
    cat(data, 'tier', "leng_tier", s=',')
    cat(data, 'customer_type', "leng_type", s=',')
    cat(data, 'age', "leng_type", s=',')
    cat(data, 'gender', "leng_type", s=',')
    cat(data, 'country', "leng_type", s=',')

    # remove those path with channels less than 3

    data_new = data[(data.leng_path >= 3)]
    # 可以还原索引，重新变为默认的整型索引
    data_new = data_new.reset_index()
    # leave with the most common value
    data_new.marketing_area = data_new.marketing_area.map(lambda x: most_fre(x))
    data_new.tier = data_new.tier.map(lambda x: most_fre(x))
    data_new.customer_type = data_new.customer_type.map(lambda x: most_fre(x))
    data_new.age = data_new.age.map(lambda x: most_fre(x))
    data_new.gender = data_new.gender.map(lambda x: most_fre(x))
    data_new.country = data_new.country.map(lambda x: most_fre(x))
    data_new.replace('', 'NA', inplace=True)  # ？？？？？？？？前面不是删掉了缺失行吗

    # 输出12：所有y
    y = data_new.total_conversions

    # 抽取训练集、测试集
    # got train and test data indices
    idx = [x for x in range(data_new.shape[0])]  # 输出矩阵的行数,以[0, 1, 2, 3]形式输出,共17行，0~16
    np.random.seed(111)
    random.shuffle(idx)  # 打乱
    # print(idx)
    tr_idx = idx[0:int(0.8 * len(idx))]
    te_idx = idx[int(0.8 * len(idx)):]
    # print(te_idx)

    # got data for time decay
    cat(data_new, 'last_time_lapse', "leng_time_lapse", s=',')
    data_new.last_time_lapse = data_new.last_time_lapse.map(lambda x: sca(x))
    pad_sequence = tf.keras.preprocessing.sequence.pad_sequences
    time_decay = pad_sequence(data_new.last_time_lapse, maxlen=seq_length, padding='pre', truncating='pre',
                              dtype='float32')  # 17*20
    # 输出10：所有时间
    time_decay = time_decay.reshape(-1, 20, 1)  # 17*20*1
    # 输出1：训练集时间
    time_decay_tr = time_decay[tr_idx]  # 占0.8
    # 输出2：测试集时间
    time_decay_te = time_decay[te_idx]  # 占0.2

    # got data for attribution
    text = data_new.path  # (17,) object
    # encoding
    t = Tokenizer()  # Tokenizer实际上只是生成了一个字典，并且统计了词频等信息，并没有把文本转成需要的向量表示。
    # 首先用Tokenizer的 fit_on_texts 方法学习出文本的字典，然后word_index 就是对应的单词和数字的映射关系dict，
    # 通过这个dict可以将每个string的每个词转成数字，可以用texts_to_sequences，这是我们需要的，然后通过padding的方法补成同样长度，
    # 在用keras中自带的embedding层进行一个向量化，并输入到LSTM中。
    t.fit_on_texts(text)
    # vocabulary size
    vocab_size = len(t.word_index) + 3  # 8
    print(vocab_size)
    # integer encode the documents
    encoded_docs = t.texts_to_sequences(text)
    # padding and truncating path data
    # 输出11：newlines.shape,17*20,pad_sequence
    newlines = pad_sequence(encoded_docs, maxlen=seq_length, padding='pre', truncating='post')  # 17*20
    X_train = newlines[tr_idx]
    # 输出7
    Y_train = y[tr_idx]
    X_test = newlines[te_idx]
    # 输出8
    Y_test = y[te_idx]
    # 输出9：所有x
    all_X = np.array(list(map(lambda x: np_utils.to_categorical(x, num_classes=vocab_size), newlines)), ndmin=3)
    # 输出3：用户路径模块-训练集X
    X_tr = np.array(list(map(lambda x: np_utils.to_categorical(x, num_classes=vocab_size), X_train)), ndmin=3)
    # 输出4：用户路径模块-测试集X
    X_te = np.array(list(map(lambda x: np_utils.to_categorical(x, num_classes=vocab_size), X_test)), ndmin=3)
    # 输出14：所有路径
    paths = text[tr_idx].reset_index().path

    # got customer data (control data)
    data_lr = data_new.loc[:, ['marketing_area', 'tier', 'customer_type', 'age', 'gender', 'country']]
    train_cats(data_lr)
    data_lr['c_type' + '_na'] = [1 if v == 'NA' else 0 for v in data_lr['customer_type']]
    for col in data_lr.columns:
        if not is_numeric_dtype(data_lr[col]):
            data_lr[col] = data_lr[col].cat.codes + 1
    # 输出5：用户属性模块：训练集X
    X_tr_lr = data_lr.iloc[tr_idx, :]
    # print(X_tr_lr)
    # 输出6：用户属性模块：测试集X
    X_te_lr = data_lr.iloc[te_idx, :]  # iloc根据行号选
    # 利用 MinMaxScaler 放缩数据
    scaler = MinMaxScaler()
    scaler.fit(X_tr_lr)
    X_tr_lr[['marketing_area', 'tier', 'customer_type', 'age', 'gender', 'country']] = scaler.fit_transform(
        X_tr_lr[['marketing_area', 'tier', 'customer_type', 'age', 'gender', 'country']])
    X_te_lr[['marketing_area', 'tier', 'customer_type', 'age', 'gender', 'country']] = scaler.transform(
        X_te_lr[['marketing_area', 'tier', 'customer_type', 'age', 'gender', 'country']])
    # 输出13：用户属性模块的哪些字段
    categorical_vars = data_lr.columns[0:6]
    # print(categorical_vars)

    return [time_decay_tr, time_decay_te, X_tr, X_te, X_tr_lr, X_te_lr, Y_train, Y_test,
            all_X, time_decay, newlines, y, categorical_vars, paths]

# data = pd.read_excel('fake_data.xlsx')
# data['last_time_lapse'] = data['last_time_lapse'].map(eval)
if __name__ == '__main__':
    data = pd.read_excel('fake_data2.xlsx')
    seq_length = 20
    # 预处理后的数据
    data_all = process_data(data, seq_length=seq_length)
    # print('data_all[-1]:')
    # print(data_all[-1])
    channels = list(set([_p for p in data_all[-1] for _p in p]))
    # channels.sort()
    print(channels)


import numpy as np
import multiprocessing as mp

from . import utility
from . import constants
from .agents import SparringAgent

class DataGenerator:
    def __init__(self, num_workers=constants.NUM_WORKERS):
        self.num_workers = num_workers

    def agent_simulation(self, worker_id, num_games, sparrer_type):
        # NOTE: may need more imports for network agents
        np.random.seed()

        raw_training_examples = []
        for _ in range(num_games):
            agent = SparringAgent(sparrer_type=sparrer_type, agent_id = worker_id % constants.MAX_PLAYERS)
            raw_training_examples += agent.simulate()

        print('Worker {} Generated {} examples'.format(worker_id, len(raw_training_examples)))
        return raw_training_examples

    def generate_agent_data(self, num_games, sparrer_type):
        process_pool = mp.Pool(processes=self.num_workers)
        workers_results = []

        for i in range(self.num_workers):
            data_async = process_pool.apply_async(self.agent_simulation, args=(i, num_games, sparrer_type))
            workers_results.append(data_async)

        try:
            training_data_X = []
            training_data_y = []
            for res in workers_results:
                worker_data = res.get()
                for X, y, agent_id in worker_data:
                    X = utility.convert_to_model_input(agent_id, X)
                    training_data_X.append(X)
                    training_data_y.append(y)

            process_pool.close()
            process_pool.join()

            # training_data_X = np.array(training_data_X)
            # training_data_y = np.array(training_data_y)

            # # Augment training data
            # X_augment = []
            # y_augment = []
            # for i in range(len(training_data_X)):
            #     X, y = utility.augment_data(training_data_X[i], training_data_y[i])
            #     X_augment.append(X)
            #     y_augment.append(y)

            # X_augment = np.concatenate(X_augment)
            # y_augment = np.concatenate(y_augment)

            return training_data_X, training_data_y

        except KeyboardInterrupt:
            print('SIGINT caught, exiting')
            process_pool.terminate()
            process_pool.join()
            exit()

    def generate_simple_agent_data(self, num_games):
        return self.generate_agent_data(num_games, sparrer_type=constants.SIMPLE_SPARRER)

if __name__ == '__main__':
    generator = DataGenerator(num_workers=constants.NUM_WORKERS)
    training_data_X, training_data_y = generator.generate_simple_agent_data(1)


# print("Hello! Please complete this survey for me.")
# print("What is your name?")

# new_dict = {"name": "trang", 13: "tiger", "may":5}

# print(new_dict["name"])

# for each in new_dict:
#     print(new_dict[each])

# dict = {"name": 23}

import time
import json

ls = []

print("Hello please complete this survey.")
dict = {}

def questions(dict):
    answer = input("what's your name?")
    dict["name"] = answer
    time.sleep(0.5)
    answer = input("when is your birthday? (MM/DD/YYYY)")
    dict["bday"] = answer
    time.sleep(0.5)
    answer = input("what is your favorite boba place?")
    dict["boba"] = answer
    time.sleep(0.5)
    answer = input("Who is your celebrity crush?")
    dict["celeb crush"] = answer
    time.sleep(0.5)
    answer = input("What is your biggest fear?")
    dict["fear"] = answer
    time.sleep(0.5)

for i in range(2):
    dict = {}
    questions(dict)
    ls.append(dict)
tojson = json.dumps(ls)

print(tojson)
print(ls)

with open('dictionary.json', 'w') as outfile:
        json.dump(ls, outfile)


#!/usr/bin/env python3
import functools
import os
from multiprocessing import Process
from baselines import logger
from mpi4py import MPI
import mpi_util
import tf_util
from cmd_util import make_atari_env, arg_parser
from policies.cnn_gru_policy_dynamics import CnnGruPolicy
from policies.cnn_policy_param_matched import CnnPolicy
from ppo_agent import PpoAgent
from utils import set_global_seeds
from vec_env import VecFrameStack
from copy import deepcopy
import datetime
from baselines.common.vec_env import VecNormalize
from baselines.common.cmd_util import make_vec_env

def make_mujoco_env(env_id, seed, reward_scale=1.0):
    env = make_vec_env(env_id, "mujoco", num_env=1, seed=seed, reward_scale=reward_scale,
                       flatten_dict_observations=True)
    env = VecNormalize(env)
    return env

def train(*, origin_paper, env_id, num_env, hps, num_timesteps, seed):
    # venv = VecFrameStack(
    #     make_atari_env(env_id, num_env, seed, wrapper_kwargs=dict(),
    #                    start_index=num_env * MPI.COMM_WORLD.Get_rank(),
    #                    max_episode_steps=hps.pop('max_episode_steps')),
    #     hps.pop('frame_stack'))
    # # venv.score_multiple = {'Mario': 500,
    # #                        'MontezumaRevengeNoFrameskip-v4': 100,
    # #                        'GravitarNoFrameskip-v4': 250,
    # #                        'PrivateEyeNoFrameskip-v4': 500,
    # #                        'SolarisNoFrameskip-v4': None,
    # #                        'VentureNoFrameskip-v4': 200,
    # #                        'PitfallNoFrameskip-v4': 100,
    # #                        }[env_id]
    # venv.score_multiple = 1
    # venv.record_obs = True if env_id == 'SolarisNoFrameskip-v4' else False
    venv = make_mujoco_env(env_id, seed, reward_scale=1.0)
    setattr(venv, "score_multiple", 1.0)
    setattr(venv, "record_obs", False)
    ob_space = venv.observation_space
    ac_space = venv.action_space
    gamma = hps.pop('gamma')
    policy = {'rnn': CnnGruPolicy,
              'cnn': CnnPolicy}[hps.pop('policy')]

    agent = PpoAgent(
        scope='ppo',
        ob_space=ob_space,
        ac_space=ac_space,
        stochpol_fn=functools.partial(
                policy,
                scope='pol',
                ob_space=ob_space,
                ac_space=ac_space,
                update_ob_stats_independently_per_gpu=hps.pop('update_ob_stats_independently_per_gpu'),
                proportion_of_exp_used_for_predictor_update=hps.pop('proportion_of_exp_used_for_predictor_update'),
                dynamics_bonus = hps.pop("dynamics_bonus")
            ),
        gamma=gamma,
        gamma_ext=hps.pop('gamma_ext'),
        lam=hps.pop('lam'),
        nepochs=hps.pop('nepochs'),
        nminibatches=hps.pop('nminibatches'),
        lr=hps.pop('lr'),
        cliprange=hps.pop('cliprange'),
        nsteps=hps.pop('nsteps'),
        ent_coef=hps.pop('ent_coef'),
        max_grad_norm=hps.pop('max_grad_norm'),
        use_news=hps.pop("use_news"),
        comm=MPI.COMM_WORLD if MPI.COMM_WORLD.Get_size() > 1 else None,
        update_ob_stats_every_step=hps.pop('update_ob_stats_every_step'),
        int_coeff=hps.pop('int_coeff'),
        ext_coeff=hps.pop('ext_coeff'),
        origin_paper=origin_paper,
    )
    agent.start_interaction([venv])
    if hps.pop('update_ob_stats_from_random_agent'):
        agent.collect_random_statistics(num_timesteps=128*10)
    if len(hps) != 0:
        logger.info("warning!Unused hyperparameters: %s" % list(hps.keys()))

    counter = 0
    while True:
        info = agent.step()
        if info['update']:
            logger.logkvs(info['update'])
            logger.dumpkvs()
            counter += 1
        if agent.I.stats['tcount'] > num_timesteps:
            break

    agent.stop_interaction()

def add_env_params(parser):
    parser.add_argument('--env', help='environment ID', default='HalfCheetah-v2')
    parser.add_argument('--seed', help='RNG seed', type=int, default=0)
    parser.add_argument('--max_episode_steps', type=int, default=4500)

def main(args):
    server = args.server
    origin_paper = args.origin_paper
    if not origin_paper:
        args.nminibatches = 32
        args.nsteps = 2048
        args.max_grad_norm = 0.5
        args.lr = 3e-4
        args.ent_coef = 0
        args.cliprange = 0.2

    if not server:
        path = "logs"
    else:
        path = os.path.join("/home", "lizn", "openai", "rnd",
                            datetime.datetime.now().strftime("ppo-rnd-{}-%Y-%m-%d-%H-%M-%S-%f".format(args.env)))
    logger.configure(dir=path, format_strs=['stdout', 'log', 'csv'] if MPI.COMM_WORLD.Get_rank() == 0 else [])
    if MPI.COMM_WORLD.Get_rank() == 0:
        with open(os.path.join(logger.get_dir(), 'experiment_tag.txt'), 'w') as f:
            f.write(args.tag)

    if server:
        mpi_util.setup_mpi_gpus()

    seed = 10000 * args.seed + MPI.COMM_WORLD.Get_rank()
    set_global_seeds(seed)

    hps = dict(
        frame_stack=4,
        ent_coef=args.ent_coef,
        nminibatches=args.nminibatches,
        nepochs=4 if origin_paper else 10,
        lr=args.lr,
        max_grad_norm=args.max_grad_norm,
        use_news=args.use_news,
        gamma=args.gamma,
        gamma_ext=args.gamma_ext,
        max_episode_steps=args.max_episode_steps,
        lam=args.lam,
        update_ob_stats_every_step=args.update_ob_stats_every_step,
        update_ob_stats_independently_per_gpu=args.update_ob_stats_independently_per_gpu,
        update_ob_stats_from_random_agent=args.update_ob_stats_from_random_agent,
        proportion_of_exp_used_for_predictor_update=args.proportion_of_exp_used_for_predictor_update,
        policy=args.policy,
        int_coeff=args.int_coeff,
        ext_coeff=args.ext_coeff,
        dynamics_bonus=args.dynamics_bonus,
        cliprange=args.cliprange,
        nsteps=args.nsteps

    )
    logger.info("training parameters:")
    logger.info(str(hps))
    tf_util.make_session(make_default=True)
    train(origin_paper=origin_paper, env_id=args.env, num_env=args.num_env, seed=seed,
        num_timesteps=args.num_timesteps, hps=hps)

if __name__ == '__main__':
    from baselines.common.misc_util import boolean_flag

    parser = arg_parser()
    add_env_params(parser)
    parser.add_argument('--num-timesteps', type=int, default=int(1e6))
    parser.add_argument('--num_env', type=int, default=1)
    parser.add_argument('--use_news', type=int, default=0)
    parser.add_argument('--gamma', type=float, default=0.99)
    parser.add_argument('--gamma_ext', type=float, default=0.99)
    parser.add_argument('--lam', type=float, default=0.95)
    parser.add_argument('--update_ob_stats_every_step', type=int, default=0)
    parser.add_argument('--update_ob_stats_independently_per_gpu', type=int, default=0)
    parser.add_argument('--update_ob_stats_from_random_agent', type=int, default=1)
    parser.add_argument('--proportion_of_exp_used_for_predictor_update', type=float, default=1.)
    parser.add_argument('--tag', type=str, default='')
    parser.add_argument('--policy', type=str, default='cnn', choices=['cnn', 'rnn'])
    parser.add_argument('--int_coeff', type=float, default=1.)
    parser.add_argument('--ext_coeff', type=float, default=2.)
    parser.add_argument('--dynamics_bonus', type=int, default=0)
    parser.add_argument('--nminibatches', type=int, default=1)
    parser.add_argument('--nsteps', type=int, default=128)
    parser.add_argument('--max_grad_norm', type=float, default=0)
    parser.add_argument('--lr', type=float, default=0.0001)
    parser.add_argument('--ent_coef', type=float, default=0.001)
    parser.add_argument('--cliprange', type=float, default=0.1)
    boolean_flag(parser, 'origin_paper', default=False)
    boolean_flag(parser, 'server', default=False)
    boolean_flag(parser, 'all_env', default=False)
    # todo: change the following two parameters when you run your local machine or server

    args = parser.parse_args()

    list_p = []
    list_env = ["HalfCheetah-v2", "Hopper-v2", "Swimmer-v2", "InvertedDoublePendulum-v2", "Reacher-v2",
                "Walker2d-v2", "Pusher-v2"] if args.all_env else [args.env]
    for env in list_env:
        _args = deepcopy(args)
        _args.env = env
        p = Process(target=main, args=(_args, ))
        list_p.append((env, p))
        p.start()
        print("============================================")
        print("{} start!".format(env))
        print("============================================")
    for (env, p) in list_p:
        p.join()
        print("============================================")
        print("{} end!".format(env))
        print("============================================")


# Triangle containment
# Problem 102
#
# Three distinct points are plotted at random on a Cartesian plane, for which -1000 ≤ x, y ≤ 1000,
# such that a triangle is formed.
#
# Consider the following two triangles:
#
#     A(-340,495), B(-153,-910), C(835,-947)
#     X(-175,41), Y(-421,-714), Z(574,-645)
#
# It can be verified that triangle ABC contains the origin, whereas triangle XYZ does not.
#
# Using triangles.txt (right click and 'Save Link/Target As...'), a 27K text file containing the
# co-ordinates of one thousand "random" triangles, find the number of triangles for which the
# interior contains the origin.
#
# NOTE: The first two examples in the file represent the triangles in the example given above.

def dist_squared(point1, point2):

    x1 = point1[0]
    y1 = point1[1]
    x2 = point2[0]
    y2 = point2[1]

    return (y2 - y1) ** 2 + (x2 - x1) ** 2
def std_form_eq(point1, point2):
    x1 = point1[0]
    y1 = point1[1]
    x2 = point2[0]
    y2 = point2[1]

    a = -(y2 - y1)
    b = x2 - x1
    c = (x2 - x1) * y1 - (y2 - y1) * x1

    return a, b, c

def intersection(abc1, abc2):
    a1 = abc1[0]
    b1 = abc1[1]
    c1 = abc1[2]

    a2 = abc2[0]
    b2 = abc2[1]
    c2 = abc2[2]

    d = a1 * b2 - b1 * a2
    d_x = c1 * b2 - b1 * c2
    d_y = a1 * c2 - c1 * a2

    return d_x / d, d_y / d

data_file = "data/p102_triangles.txt"

with open(data_file, 'r') as f:
    lines = f.read().splitlines()

triangles = []
for line in lines:
    points = line.split(',')
    X = (int(points[0]), int(points[1]))
    Y = (int(points[2]), int(points[3]))
    Z = (int(points[4]), int(points[5]))

    triangles.append([X, Y, Z])

count = 0
origin = (0, 0)

for t in triangles:

    a = t[0]
    b = t[1]
    c = t[2]

    ao_eq = std_form_eq(a, origin)
    bo_eq = std_form_eq(b, origin)
    co_eq = std_form_eq(c, origin)

    ab_eq = std_form_eq(a, b)
    bc_eq = std_form_eq(b, c)
    ac_eq = std_form_eq(a, c)

    ao_int_bc = intersection(ao_eq, bc_eq)
    bo_int_ac = intersection(bo_eq, ac_eq)
    co_int_ab = intersection(co_eq, ab_eq)

    ao_dist = dist_squared(a, origin)
    bo_dist = dist_squared(b, origin)
    co_dist = dist_squared(c, origin)

    a_bc_dist = dist_squared(a, ao_int_bc)
    b_ac_dist = dist_squared(b, bo_int_ac)
    c_ab_dist = dist_squared(c, co_int_ab)

    check1 = ao_dist < a_bc_dist
    check2 = bo_dist < b_ac_dist
    check3 = co_dist < c_ab_dist

    if check1 and check2 and check3:
        count += 1

print(count)

# Answer: 228


'''Uma determinada empresa armazena para cada funcionário uma ficha contendo o código, o número de
horas trabalhadas e o seu no de dependentes. Considerando que a empresa paga R$ 12,00 por hora e R$
40,00 por dependentes e que sobre o salário são feitos descontos de 8,5% para o INSS e 5% para IR.
Escreva um algoritmo que leia o código, número de horas trabalhadas e número de dependentes de N
funcionários. Após a leitura de cada ficha, escreva os valores descontados para cada imposto e o salário
líquido do funcionário.'''

# ENTRADA
def main():
    f = int(input('Quantidade de fichas de funcionários: '))

    fichas(f)

# PROCESSAMENTO
def fichas(f):
    contador = 1
    valor_hora = 12
    valor_por_dependente = 40

    while contador <= f:
        print('-'*100)
        c = int(input('Código do funcionário: '))
        h = int(input('Quantidade de horas trabalhadas: '))
        d = int(input('Número de dependentes: '))

        contador = contador + 1

        salario_bruto = (valor_hora * h) + (valor_por_dependente * d)

        print('-'*100)
        print(f'Código do funcionário: {c}')
        print(f'Desconto do INSS: R$ {desconto_inss(salario_bruto):.2f}')
        print(f'Desconto do IR: R$ {desconto_ir(salario_bruto):.2f}')
        print(f'Salário Líquido: R$ {salario_liquido(salario_bruto, desconto_inss(salario_bruto), desconto_ir(salario_bruto)):.2f}')
        print('-'*100)

def salario_liquido(sb, d_inss, d_ir):
    salario = sb - d_inss - d_ir
    return salario

def desconto_inss(sb):
    inss = (sb * 8.5) / 100
    return inss

def desconto_ir(sb):
    ir = (sb * 5) / 100
    return ir

# SAÍDA
main()

capitulo_7_iteracao/Fabio03 - WHILE cont/fabio03_q23_empresa.py


import os
import tensorflow as tf
import tensorflow.contrib.eager as tfe

tf.enable_eager_execution()

train_dataset_url = "http://download.tensorflow.org/data/iris_training.csv"

train_dataset_fp = tf.keras.utils.get_file(fname=os.path.basename(train_dataset_url), origin=train_dataset_url)
print("Local copy of the dataset file: {}".format(train_dataset_fp))

def parse_csv(line):
	example_defaults = [[0.], [0.], [0.], [0.], [0.]]
	parsed_line = tf.decode_csv(line, example_defaults)
	# first four fields are features combine into single tensor
	features = tf.reshape(parsed_line[:-1], shape=(4,))
	# last field is the label
	label = tf.reshape(parsed_line[-1], shape=())
	return features, label

train_dataset = tf.data.TextLineDataset(train_dataset_fp)
train_dataset = train_dataset.skip(1)
train_dataset = train_dataset.map(parse_csv)
train_dataset = train_dataset.shuffle(buffer_size=1000)
train_dataset = train_dataset.batch(32)

# view a single example entry from a batch
#features, label = tfe.Iterator(train_dataset).next()
#print("example features", features[0])
#print("example label:", label[0])

model = tf.keras.Sequential([
	tf.keras.layers.Dense(10, activation="relu", input_shape=(4,)),
	tf.keras.layers.Dense(10, activation="relu"),
	tf.keras.layers.Dense(3)])

def loss(model, x, y):
	y_ = model(x)
	return tf.losses.sparse_softmax_cross_entropy(labels=y, logits=y_)

def grad(model, inputs, targets):
	with tfe.GradientTape() as tape:
		loss_value = loss(model, inputs, targets)
	return tape.gradient(loss_value, model.variables)

optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.01)

# keep results for plotting
train_loss_results = []
train_accuracy_results = []

num_epochs = 201
for epoch in range(num_epochs):
	epoch_loss_avg = tfe.metrics.Mean()
	epoch_accuracy = tfe.metrics.Accuracy()

	# training loop - using batches of 32
	for x, y in tfe.Iterator(train_dataset):
		# optimize the model
		grads = grad(model, x, y)
		optimizer.apply_gradients(zip(grads, model.variables),
			global_step=tf.train.get_or_create_global_step())

		# track progress
		epoch_loss_avg(loss(model, x, y))
		epoch_accuracy(tf.argmax(model(x), axis=1, output_type=tf.int32), y)

	# end epoch
	train_loss_results.append(epoch_loss_avg.result())
	train_accuracy_results.append(epoch_accuracy.result())

	if epoch % 50 == 0:
		print("Epoch {:03d}: Loss: {:.3f}, Accuracy: {:.3%}".format(epoch,
			epoch_loss_avg.result(),
			epoch_accuracy.result()))


def tally_sort(num_list):
    minimum = 999999
    for n in num_list:
        if n < minimum:
            minimum = n

    new_arr = [0] * len(num_list)

    for n in num_list:
        new_arr[n - minimum] += 1

    ret_arr = []
    for i in range(len(new_arr)):
        for _ in range(new_arr[i]):
            ret_arr.append(i)

    for i in range(len(ret_arr)):
        ret_arr[i] += minimum

    return ret_arr

num_list, sorted = [2, 5, 2, 3, 8, 6, 3 ], [2, 2, 3, 3, 5, 6, 8]
assert( tally_sort(num_list) == sorted )


from math import pi

def Pi(n):
    nk = 0
    with open("punkty.txt") as f:
        for line in f.readlines()[:n]:
            x, y = list(map(int, line.split()))

            if (x-200)**2 + (y-200)**2 <= 40000:
                nk += 1

    return (nk*4)/n

with open("epsilons1.txt", "w") as ff:
    for i in range(1, 1701):
        ff.write("n="+ str(i) + ";"+ str(abs(pi - Pi(i)))+"\n")

with open("epsilons2.txt", "w") as ff:
    for i in range(1000, 1701):
        ff.write("n="+ str(i) + "; "+ str(round(abs(pi - Pi(i)), 4))+"\n")

2016/before matura exam attempt/zad4/4-3.py


import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl
import math
import os
from preprocessing import *

class Data :

    def __init__(self, X=None, y=None, Xnames=None, yname=None, weights=None) :
        """
        Data class.

        Attributes
        --------------------
            X       -- numpy array of shape (n,d), features
            y       -- numpy array of shape (n,), targets
        """

        # n = number of examples, d = dimensionality
        self.X = X
        self.y = y
        self.Xnames = Xnames
        self.yname = yname
        self.weights = weights

    def load(self, filenameX, filenamey, header=0) :
        """
        Load csv file into X array of features and y array of labels.

        Parameters
        --------------------
            filename   -- string, filename
        """

        # determine filename
        dir = os.path.dirname(__file__)
        fX = os.path.join(dir, 'Dataset', filenameX)
        fy = os.path.join(dir, 'Dataset', filenamey)

        # load data
        with open(fX, 'r') as fid :
            dataX = np.loadtxt(fid, delimiter=",", skiprows=header)

        with open(fy, 'r') as fid :
            datay = np.loadtxt(fid, delimiter=",", skiprows=header)

        # separate features and labels
        self.X = dataX
        self.y = datay

        # load feature and label names
        if header != 0:
            with open(fX, 'r') as fid :
                headerX = fid.readline().rstrip().split(",")

            with open(fy, 'r') as fid :
                headery = fid.readline().rstrip().split(",")

            self.Xnames = headerX
            self.yname = headery

        else:
            self.Xnames = None
            self.yname = None

    def plot(self) :
        """Plot features and labels."""
        pos = np.nonzero(self.y > 0)  # matlab: find(y > 0)
        neg = np.nonzero(self.y < 0)  # matlab: find(y < 0)
        plt.plot(self.X[pos,0], self.X[pos,1], 'b+', markersize=5)
        plt.plot(self.X[neg,0], self.X[neg,1], 'ro', markersize=5)
        plt.show()

    def plot_histogram(self) :
        """
        Plots histogram of values in X grouped by y.

        Parameters
        --------------------
            X     -- numpy array of shape (n,), feature values
            y     -- numpy array of shape (n,), target classes
            Xnames -- string, name of feature
            yname -- string, name of target
        """
        for i in xrange(len(self.Xnames)):
            # set up data for plotting
            targets = sorted(set(self.y))
            data = []; labels = []
            for target in targets :
                features = [self.X[:,i][j] for j in xrange(len(self.y)) if self.y[j] == target]
                data.append(features)
                labels.append('%s = %s' % (self.yname[0], target))

            # set up histogram bins
            features = set(self.X[:,i])
            nfeatures = len(features)
            test_range = range(int(math.floor(min(features))), int(math.ceil(max(features)))+1)
            if nfeatures < 33 and sorted(features) == test_range:
                bins = test_range + [test_range[-1] + 1] # add last bin
                align = 'left'
            else :
                bins = 33
                align = 'mid'

            # plot
            plt.figure()
            n, bins, patches = plt.hist(data, bins=bins, align=align, alpha=0.5, label=labels, log=True, density=False)
            # n, bins, patches = plt.hist(data, density=True)
            plt.xlabel(self.Xnames[i])
            plt.ylabel('Frequency')
            plt.legend()
            plt.legend(loc='upper left')
            plt.show()

def save_data(filename, df):
    df.to_csv(filename)

# helper functions
def load_data(filenameX, filenamey, header=0) :
    """Load csv file into Data class."""
    data = Data()
    data.load(filenameX, filenamey, header=header)
    return data

def update_data():
    npX, npy, Xnames, X, y = main()
    X.to_csv('Dataset/X.csv', index=False)
    y.to_csv('Dataset/y.csv', index=False, header=True)


# 类方法
# @classmethod 将一个方法绑定到类上, 而非类的实例
# 类方法的第一个参数将传入类本身, 通常将参数名命名为cls
# 类方法内部无法访问任何实例属性
# 先从自己的__dict__中查找，如果没有，再通过__class__找到自己的类，再去类的__dict__中查找

# https://docs.python.org/3/library/functions.html#classmethod

# A class method receives the class as implicit first argument, just like an instance method receives the instance.
# It can be called either on the class or on an instance.
# If a class method is called for a derived class, the derived class object is passed as the implied first argument.

class Student:

    sum = 0

    def __init__(self,name,age):
        self.name = name
        self.age = age
        # self.__class__.sum += 1
        # print('当前班级学生总数为: ' + str(self.__class__.sum))

    # 定义一个类方法
    # cls跟self一样可以任意改变，这里表示类本身
    @classmethod
    def plus_sum(cls):
        cls.sum += 1
        print('当前班级学生总数为: ' + str(cls.sum))

# 每实例化一个对象就调用一次类方法，使总数加一
# 通过类访问类方法
student1 = Student('石敢当',17)
Student.plus_sum()
student2 = Student('喜小乐',18)
Student.plus_sum()

# 另外，实例也可以直接调用类方法
student3 = Student('小诺',19)
student3.plus_sum()
# print(student3.__dict__)
# {'name': '小诺', 'age': 19}


##############################################################################
# Institute for the Design of Advanced Energy Systems Process Systems
# Engineering Framework (IDAES PSE Framework) Copyright (c) 2018-2019, by the
# software owners: The Regents of the University of California, through
# Lawrence Berkeley National Laboratory,  National Technology & Engineering
# Solutions of Sandia, LLC, Carnegie Mellon University, West Virginia
# University Research Corporation, et al. All rights reserved.
#
# This software is distributed under the 3-clause BSD License.
##############################################################################
"""
A simple GUI viewer/editor for Pyomo models.
"""
from __future__ import division, print_function, absolute_import

__author__ = "John Eslick"

import os
import logging

_log = logging.getLogger(__name__)

from pyomo.contrib.viewer.qt import *
from pyomo.contrib.viewer.report import value_no_exception, get_residual
import pyomo.environ as pyo

mypath = os.path.dirname(__file__)
try:
    _ResidualTableUI, _ResidualTable = \
        uic.loadUiType(os.path.join(mypath, "residual_table.ui"))
except:
    class _ResidualTableUI(object):
        pass
    class _ResidualTable(object):
        pass

class ResidualTable(_ResidualTable, _ResidualTableUI):
    def __init__(self, ui_data, parent=None):
        super(ResidualTable, self).__init__(parent=parent)
        self.setupUi(self)
        self.ui_data = ui_data
        datmodel = ResidualDataModel(parent=self, ui_data=ui_data)
        self.datmodel = datmodel
        self.tableView.setModel(datmodel)
        self.ui_data.updated.connect(self.refresh)
        self.sortButton.clicked.connect(self.sort)
        self.calculateButton.clicked.connect(self.calculate)

    def sort(self):
        self.datmodel.sort()

    def refresh(self):
        self.datmodel.update_model()
        self.datmodel.sort()
        self.datmodel.layoutChanged.emit()

    def calculate(self):
        self.ui_data.calculate_constraints()
        self.refresh()

class ResidualDataModel(QAbstractTableModel):
    def __init__(self, parent, ui_data):
        super(ResidualDataModel, self).__init__(parent)
        self.column = ["name", "residual", "value", "ub", "lb", "active"]
        self.ui_data = ui_data
        self.include_inactive = True
        self.update_model()
        self.sort()

    def update_model(self):
        if self.include_inactive:
            ac = None
        else:
            ac = True
        self._items = list(self.ui_data.model.component_data_objects(
            pyo.Constraint, active=ac))

    def sort(self):
        self._items.sort(key=
            lambda o: (o is None, get_residual(self.ui_data, o)
                                  if get_residual(self.ui_data, o) is not None
                                  else -float("inf")), reverse=True)

    def rowCount(self, parent=QtCore.QModelIndex()):
        return len(self._items)

    def columnCount(self, parent=QtCore.QModelIndex()):
        return len(self.column)

    def data(self, index=QtCore.QModelIndex(), role=QtCore.Qt.DisplayRole):
        row = index.row()
        col = self.column[index.column()]
        if role == QtCore.Qt.DisplayRole:
            o = self._items[row]
            if col=="name":
                return str(o)
            elif col=="residual":
                return get_residual(self.ui_data, o)
            elif col=="active":
                return o.active
            elif col=="ub":
                return value_no_exception(o.upper)
            elif col=="lb":
                return value_no_exception(o.lower)
            elif col=="value":
                try:
                    return self.ui_data.value_cache[o]
                except KeyError:
                    return None
        else:
            return None

    def headerData(self, i, orientation, role=QtCore.Qt.DisplayRole):
        '''
            Return the column headings for the horizontal header and
            index numbers for the vertical header.
        '''
        if orientation == QtCore.Qt.Horizontal and role == QtCore.Qt.DisplayRole:
            return self.column[i]
        return None