Ejemplo n.º 1
0
    def endstep(self, game, state, frametime):
        
        player = self.get_player(game, state)
        # check if we are on the ground before moving (for walking over 1 unit walls)
        onground = True

        # first we move, ignoring walls
        self.x += self.hspeed * frametime
        # if we are in a wall now, we must move back

        if game.map.collision_mask.overlap(self.collision_mask, (int(self.x), int(self.y))):
            # but if we just walked onto a one-unit wall it's ok
            # but we had to be on the ground
            if onground and not game.map.collision_mask.overlap(self.collision_mask, (int(self.x), int(self.y - 6))):
                while game.map.collision_mask.overlap(self.collision_mask, (int(self.x), int(self.y))):
                    self.y -= 1
            # but sometimes we are so fast we will need to take two stairs at the same time
            elif onground and not game.map.collision_mask.overlap(self.collision_mask, (int(self.x), int(self.y - 12))) and game.map.collision_mask.overlap(self.collision_mask, (int(self.x - 6 * function.sign(self.hspeed)), int(self.y))):
                while game.map.collision_mask.overlap(self.collision_mask, (int(self.x), int(self.y))):
                    self.y -= 1
            else:
                self.x = math.floor(self.x) # move back to a whole pixel - TODO math.floor/math.ceil depending on direction

                # and if one pixel wasn't enough
                while game.map.collision_mask.overlap(self.collision_mask, (int(self.x), int(self.y))):
                    self.x -= function.sign(self.hspeed)

                self.hspeed = 0
            
            
        #downward stairscript with hspeed checks
        if onground and not game.map.collision_mask.overlap(self.collision_mask, (int(self.x), int(self.y + 6))):
            if game.map.collision_mask.overlap(self.collision_mask, (int(self.x), int(self.y + 7))):
                self.y += 6
            elif game.map.collision_mask.overlap(self.collision_mask, (int(self.x), int(self.y + 13))):
                if (self.hspeed !=0):
                    if game.map.collision_mask.overlap(self.collision_mask, (int(self.x + function.sign(self.hspeed)*-6), int(self.y + 7))) and not game.map.collision_mask.overlap(self.collision_mask, (int(self.x + 6), int(self.y + 1))):
                        self.y += 12

        # same stuff, but now vertically
        self.y += self.vspeed * frametime

        if game.map.collision_mask.overlap(self.collision_mask, (int(self.x), int(self.y))):
            self.y = float(int(self.y))

            while game.map.collision_mask.overlap(self.collision_mask, (int(self.x), int(self.y))):
                self.y -= function.sign(self.vspeed)

            self.vspeed = 0

        player.last_left = player.left;
        player.last_right = player.right;
Ejemplo n.º 2
0
    def step(self, game, state, frametime):
        # FIXME: MAKE THIS WORK FOR NEGATIVE frametime!
        # GMK-GG2 tried to emulate basic acceleration and air resistance with two simple instructions:
        #   [execute 30 times per second]
        #   speed += 1
        #   speed *= 0.92
        # Underneath is the same thing converted to work with frametime.
        # PS: If you ever have the chance, please bash whoever had the idea of a non-standard exponential function for a rocket in an 8-bit game on the head. Thank you.
        self.speed /= 30
        n = 30 * frametime
        self.speed = (0.92**n) * self.speed + 0.92*((1 - (0.92**n))/(1 - 0.92))
        self.speed *= 30

        self.hspeed = math.cos(math.radians(self.direction)) * self.speed * function.sign(frametime)
        self.vspeed = math.sin(math.radians(self.direction)) * (-self.speed) * function.sign(frametime)
Ejemplo n.º 3
0
    def step(self, game, state, frametime):
        player = self.get_player(game, state)

        # this is quite important, if hspeed / 20 drops below 1 self.animoffset will rapidly change and cause very fast moving legs (while we are moving very slow)
        if abs(self.hspeed) > 20:
            self.animoffset += frametime * abs(self.hspeed) / 20
            self.animoffset %= 2
        if abs(self.hspeed) == 0:
            self.animoffset =0

        self.flip = not (player.aimdirection < 90 or player.aimdirection > 270)

        # if we are holding down movement keys, move
        if player.left: self.hspeed -= self.acceleration * frametime
        if player.right: self.hspeed += self.acceleration * frametime

        # Friction:
        if abs(self.hspeed) < 10: self.hspeed = 0
        else: self.hspeed -= function.sign(self.hspeed) * min(abs(self.hspeed), 600 * frametime)

        if player.up:
            self.jump(game, state)

        # gravitational force
        self.vspeed += 300 * frametime

        # TODO: air resistance, not hard limit
        self.vspeed = min(800, self.vspeed)

        self.hspeed = min(self.max_speed, max(-self.max_speed, self.hspeed))
Ejemplo n.º 4
0
    def step(self, game, state, frametime):
        # FIXME: MAKE THIS WORK FOR NEGATIVE frametime!
        # GMK-GG2 tried to emulate basic acceleration and air resistance with two simple instructions:
        #   [execute 30 times per second]
        #   speed += 1
        #   speed *= 0.92
        # Underneath is the same thing converted to work with frametime.
        # PS: If you ever have the chance, please bash whoever had the idea of a non-standard exponential function for a rocket in an 8-bit game on the head. Thank you.
        self.speed /= 30
        n = 30 * frametime
        self.speed = (0.92**n) * self.speed + 0.92 * ((1 - (0.92**n)) /
                                                      (1 - 0.92))
        self.speed *= 30

        self.hspeed = math.cos(math.radians(
            self.direction)) * self.speed * function.sign(frametime)
        self.vspeed = math.sin(math.radians(
            self.direction)) * (-self.speed) * function.sign(frametime)
Ejemplo n.º 5
0
    def endstep(self, game, state, frametime):

        player = self.get_player(state)
        # check if we are on the ground before moving (for walking over 1 unit walls)
        onground = True

        # first we move, ignoring walls
        self.x += self.hspeed * frametime
        # if we are in a wall now, we must move back

        if state.map.collision_mask.overlap(self.collision_mask, (int(round(self.x)), int(round(self.y)))):
            # but if we just walked onto a one-unit wall it's ok
            # but we had to be on the ground
            if onground and not state.map.collision_mask.overlap(self.collision_mask, (int(round(self.x)), int(round(self.y - 6)))):
                while state.map.collision_mask.overlap(self.collision_mask, (int(round(self.x)), int(round(self.y)))):
                    self.y -= 1
            # but sometimes we are so fast we will need to take two stairs at the same time
            elif onground and not state.map.collision_mask.overlap(self.collision_mask, (int(round(self.x)), int(round(self.y - 12)))) and state.map.collision_mask.overlap(self.collision_mask, (int(round(self.x - 6 * function.sign(self.hspeed))), int(round(self.y)))):
                while state.map.collision_mask.overlap(self.collision_mask, (int(round(self.x)), int(round(self.y)))):
                    self.y -= 1
            else:
                if self.hspeed < 0:
                    self.x = math.floor(self.x) # move back to a whole pixel
                else:
                    self.x = math.ceil(self.x)
                # and if one pixel wasn't enough
                while state.map.collision_mask.overlap(self.collision_mask, (int(round(self.x)), int(round(self.y)))):
                    self.x -= function.sign(self.hspeed) * function.sign(frametime)

                self.hspeed = 0

        # same stuff, but now vertically
        self.y += self.vspeed * frametime

        if state.map.collision_mask.overlap(self.collision_mask, (int(round(self.x)), int(round(self.y)))):
            self.y = float(round(self.y))

            while state.map.collision_mask.overlap(self.collision_mask, (int(round(self.x)), int(round(self.y)))):
                self.y -= function.sign(self.vspeed) * function.sign(frametime)

            self.vspeed = 0
        
        player.last_left = player.left
        player.last_right = player.right
Ejemplo n.º 6
0
    def endstep(self, game, state, frametime):

        player = self.get_player(state)
        # check if we are on the ground before moving (for walking over 1 unit walls)
        onground = True

        # first we move, ignoring walls
        self.x += self.hspeed * frametime
        # if we are in a wall now, we must move back

        if game.map.collision_mask.overlap(self.collision_mask, (int(round(self.x)), int(round(self.y)))):
            # but if we just walked onto a one-unit wall it's ok
            # but we had to be on the ground
            if onground and not game.map.collision_mask.overlap(self.collision_mask, (int(round(self.x)), int(round(self.y - 6)))):
                while game.map.collision_mask.overlap(self.collision_mask, (int(round(self.x)), int(round(self.y)))):
                    self.y -= 1
            # but sometimes we are so fast we will need to take two stairs at the same time
            elif onground and not game.map.collision_mask.overlap(self.collision_mask, (int(round(self.x)), int(round(self.y - 12)))) and game.map.collision_mask.overlap(self.collision_mask, (int(round(self.x - 6 * function.sign(self.hspeed))), int(round(self.y)))):
                while game.map.collision_mask.overlap(self.collision_mask, (int(round(self.x)), int(round(self.y)))):
                    self.y -= 1
            else:
                if self.hspeed < 0:
                    self.x = math.floor(self.x) # move back to a whole pixel
                else:
                    self.x = math.ceil(self.x)
                # and if one pixel wasn't enough
                while game.map.collision_mask.overlap(self.collision_mask, (int(round(self.x)), int(round(self.y)))):
                    self.x -= function.sign(self.hspeed) * function.sign(frametime)

                self.hspeed = 0

        # same stuff, but now vertically
        self.y += self.vspeed * frametime

        if game.map.collision_mask.overlap(self.collision_mask, (int(round(self.x)), int(round(self.y)))):
            self.y = float(round(self.y))

            while game.map.collision_mask.overlap(self.collision_mask, (int(round(self.x)), int(round(self.y)))):
                self.y -= function.sign(self.vspeed) * function.sign(frametime)

            self.vspeed = 0
        
        player.last_left = player.left
        player.last_right = player.right
Ejemplo n.º 7
0
    def destroy(self, game, state, frametime):
        if not self.max_flight_time - self.flight_time < self.fade_time:
            for obj in state.entities.values():
                if (
                    isinstance(obj, character.Character)
                    and math.hypot(self.x - obj.x, self.y - obj.y) < self.blastradius
                ):

                    # w and h are the width and height of the collision mask of the character
                    w, h = obj.collision_mask.get_size()

                    # x and y are here a vector from the rocket to the character
                    x = self.x - obj.x
                    y = self.y - obj.y

                    # we try to find out the crosspoint of that vector with the collision rectangle
                    f = w / (2 * x)
                    if abs(f * y) < h / 2:
                        # the vector crosses the rectangle at the sides
                        x = function.sign(x) * w / 2
                        y *= f
                    else:
                        # the vector crosses the rectangle at the bottom or top
                        f = h / (2 * y)
                        x *= f
                        y = function.sign(y) * h / 2

                    # x and y are now the positions of the point on the edge of the collision rectangle nearest to the rocket

                    # now get the vector from the rocket to that point, and store it in x and y
                    x = (obj.x + x) - self.x
                    y = (obj.y + y) - self.y

                    length = math.hypot(x, y)
                    force = (1 - (length / self.blastradius)) * self.knockback
                    obj.hspeed += force * (x / length)
                    obj.vspeed += force * (y / length)

        super(Rocket, self).destroy(state)
Ejemplo n.º 8
0
def update_hidden(s,w,n):
    l,n2=np.shape(s)
    
    h0 = np.zeros(n2) # no external local fields
    
    h1 = np.empty(n2); p11 = np.empty(n2); p12 =np.empty(n2)
    h2=np.empty((n2, n2))
       
    # t = 0: ---------------------------    
    t = 0
    for i in range(n,n2):
        s[t,i] = 1.
        h2[:,i] = h0[i]+ np.sum(w[:,0:n2]*s[t,0:n2],axis=1)
        p1 = 1/np.prod(1+np.exp(-2*s[t+1,:]*h2[:,i]))
        p2 = 1/np.prod(1+np.exp(-2*s[t+1,:]*(h2[:,i]-2*w[:,i])))
        s[t,i] = ft.sign(p1/(p1+p2)-np.random.rand())

    # update s_hidden(t): t = 1 --> l-2:
    for t in range(1,l-1):
        # P(S_hidden(t)):
        h1[n:n2] = h0[n:n2]+np.sum(w[n:n2, :]*s[t-1, :], axis=1)
        p11[n:n2] = 1/(1+np.exp(-2*h1[n:n2])) # p(s =+1)
        p12[n:n2] = 1-p11[n:n2]                # p(s=-1)

        # P(S(t+1)):
        for i in range(n,n2):
            s[t,i] = 1.
            h2[:,i] = h0[:]+np.sum(w[:,0:n2]*s[t,0:n2],axis=1)
            p1 = p11[i]/np.prod(1+np.exp(-2*s[t+1,:]*h2[:,i]))
            p2 = p12[i]/np.prod(1+np.exp(-2*s[t+1,:]*(h2[:,i]-2*w[:,i])))
            s[t,i] = ft.sign(p1/(p1+p2)-np.random.rand())
                          
    # update s_hidden(t): t = l-1:
    h1[n:n2] = h0[n:n2]+np.sum(w[n:n2, :]*s[l-2, :], axis=1)
    p11[n:n2] = 1/(1+np.exp(-2*h1[n:n2]))     
    s[l-1,n:n2] = ft.sign_vec(p11[n:n2]-np.random.rand(n2-n))
        
    return s
Ejemplo n.º 9
0
    def destroy(self, game, state, frametime):
        if not self.max_flight_time - self.flight_time < self.fade_time:
            for obj in state.entities.values():
                if isinstance(obj, character.Character) and math.hypot(
                        self.x - obj.x, self.y - obj.y) < self.blastradius:

                    # w and h are the width and height of the collision mask of the character
                    w, h = obj.collision_mask.get_size()

                    # x and y are here a vector from the rocket to the character
                    x = self.x - obj.x
                    y = self.y - obj.y

                    # we try to find out the crosspoint of that vector with the collision rectangle
                    f = w / (2 * x)
                    if abs(f * y) < h / 2:
                        # the vector crosses the rectangle at the sides
                        x = function.sign(x) * w / 2
                        y *= f
                    else:
                        # the vector crosses the rectangle at the bottom or top
                        f = h / (2 * y)
                        x *= f
                        y = function.sign(y) * h / 2

                    # x and y are now the positions of the point on the edge of the collision rectangle nearest to the rocket

                    # now get the vector from the rocket to that point, and store it in x and y
                    x = (obj.x + x) - self.x
                    y = (obj.y + y) - self.y

                    length = math.hypot(x, y)
                    force = (1 - (length / self.blastradius)) * self.knockback
                    obj.hspeed += force * (x / length)
                    obj.vspeed += force * (y / length)

        super(Rocket, self).destroy(state)
Ejemplo n.º 10
0
Archivo: ftrl.py Proyecto: iAcheson/MLR
    def do_ftrl(self,data, test_data):
        self.saveParameters()
        if self.intercept:
            data = list(map(self.addBias, data))
            test_data = list(map(self.addBias, test_data))

        #储存每一代的ACC和LOSS
        ACC = []
        LOSS = []
        TEST_ACC = []
        TEST_LOSS = []
        AUC = []
        TEST_AUC = []

        ZW = [{} for _ in range(self.classNum)]
        ZU = [{} for _ in range(self.classNum)]
        NW = [{} for _ in range(self.classNum)]
        NU = [{} for _ in range(self.classNum)]
        WW = [{} for _ in range(self.classNum)]
        for w in WW:
            for i in range(self.feaNum):
                w[i] = np.random.normal(0,self.w_stdev)
        WU = [{} for _ in range(self.classNum)]
        for u in WU:
            for i in range(self.feaNum):
                u[i] = np.random.normal(0,self.u_stdev)
        if self.load:
            (WW, WU) = pickle.load(open("save/weight"+self.load_stamp+".kpl" ,"rb"))

        it = 0
        acc = []

        pos_data = []
        neg_data = []
        for item in data:
            if item[0]<=0:
                neg_data.append(item)
            else:
                pos_data.append(item)
        M = len(pos_data)/self.Mrate

        while it < self.it:
            start_time = time.time()


            shuffle_index = list(range(len(neg_data)))
            random.shuffle(shuffle_index)

            shuffle_neg_data = []
            for sii in range(len(pos_data)):
                shuffle_neg_data.append(neg_data[shuffle_index[sii]])

            data = pos_data + shuffle_neg_data

            for label, feaDic in data:
                # label, feaDic = data[si]

                #计算梯度:
                GW, GU = fc.cal_derivative(WW, WU, (label, feaDic))

                #更新Z,N
                for i in range(self.classNum):
                    for index in feaDic:
                        zw = ZW[i].get(index, 0)
                        nw = NW[i].get(index, 0)
                        zu = ZU[i].get(index, 0)
                        nu = NU[i].get(index, 0)
                        gw = GW[i].get(index, 0)/M
                        gu = GU[i].get(index, 0)/M
                        ww = WW[i].get(index, 0)
                        wu = WU[i].get(index, 0)

                        sw = ((nw + gw ** 2) ** 0.5 - nw ** 0.5) / self.w_alpha
                        su = ((nu + gu ** 2) ** 0.5 - nu ** 0.5) / self.u_alpha

                        ZW[i][index] = zw + gw - sw * ww
                        ZU[i][index] = zu + gu - su * wu

                        NW[i][index] = nw + gw ** 2
                        NU[i][index] = nu + gu ** 2

                #更新w,u
                for i in range(self.classNum):
                    for index in feaDic:
                        #计算W
                        z = ZW[i].get(index, 0)
                        n = NW[i].get(index, 0)
                        if abs(z) < self.norm1:
                            WW[i][index] = 0
                        else:
                            WW[i][index] = -(z - fc.sign(z) * self.norm1) / \
                                           (self.norm21 + (self.w_beta + n ** 0.5) / self.w_alpha)
                        #计算U

                        z = ZU[i].get(index, 0)
                        n = NU[i].get(index, 0)
                        if abs(z) < self.norm1:
                            WU[i][index] = 0
                        else:
                            WU[i][index] = -(z - fc.sign(z) * self.norm1) / \
                                           (self.norm21 + (self.u_beta + n ** 0.5) / self.u_alpha)
            print("iteration: ", it)
            it += 1
            if self.save :
                pickle.dump((WW,WU), open("save/weight"+self.stamp+".kpl", "wb"))

            # 计算train 相关参数:
            loss = fc.calLoss(data, WW, WU, self.norm21, self.norm1, self.feaNum)

            count = 0.0
            labels = []
            scores = []
            _tp, _tn, _p, _n = 0, 0, 0, 0
            for i in range(len(data)):
                d = data[i]
                result = fc.mlr(WW,WU, d)
                labels.append(d[0])
                scores.append(result)
                if d[0] == 1:
                    _p += 1
                else:
                    _n += 1

                if abs(result - (1 + d[0]) / 2) < 0.5:
                    count += 1
                    if result > 0.5:
                        _tp += 1
                    else:
                        _tn += 1
            acc = count / len(data)
            print ("train:")
            print("tp = ", _tp, " p = ", _p, " tn = ", _tn, " n = ", _n, " tp/p = ", _tp / _p, " tn/n = ", _tn / _n)
            ACC.append(str(acc))
            LOSS.append(str(loss))
            # 计算AUC
            roc_auc = roc_auc_score(labels, scores)
            AUC.append(str(roc_auc))

            if (it) % 10 == 0:
                #计算test相关量
                count = 0.0
                labels = []
                scores = []
                _tp, _tn, _p, _n = 0, 0, 0, 0
                for i in range(len(test_data)):
                    d = test_data[i]
                    result = fc.mlr(WW,WU, d)
                    labels.append(d[0])
                    scores.append(result)
                    if d[0] == 1:
                        _p += 1
                    else:
                        _n += 1

                    if abs(result - (1 + d[0]) / 2) < 0.5:
                        count += 1
                        if result > 0.5:
                            _tp += 1
                        else:
                            _tn += 1
                test_loss = fc.calLoss(test_data, WW, WU, self.norm21, self.norm1, self.feaNum)
                test_acc = count / len(test_data)
                print ("test:")
                print("tp = ", _tp, " p = ", _p, " tn = ", _tn, " n = ", _n, " tp/p = ", _tp / _p, " tn/n = ", _tn / _n,)

                TEST_ACC.append(str(test_acc))
                TEST_LOSS.append(str(test_loss))
                # 计算AUC
                # fpr, tpr, thresholds = roc_curve(np.array(labels), np.array(scores), pos_label=1)
                test_roc_auc = roc_auc_score(labels, scores)
                TEST_AUC.append(str(test_roc_auc))
                print("loss ", loss, " acc ", acc," auc ", roc_auc, " test loss ", test_loss, " test acc ", test_acc, " test auc ", test_roc_auc)
            print("use time: ", time.time() - start_time)
            print ("------------------------------------------------------\n")

        with open("save/result"+self.stamp,"a") as fw:
            fw.write("train_acc:" + " ".join(ACC)+"\n")
            fw.write("train_loss:" + " ".join(LOSS) + "\n")
            fw.write("train_auc:" + " ".join(AUC) + "\n")
            fw.write("test_acc:" + " ".join(TEST_ACC) + "\n")
            fw.write("test_loss:" + " ".join(TEST_LOSS) + "\n")
            fw.write("test_auc:" + " ".join(TEST_AUC) + "\n")

            # acc.append(count / len(data))
        #     if it%30 == 0:
        #
        #         _data = np.transpose(np.array([(y[0], y[1], x) for x, y in data]))
        #         plt.scatter(_data[0], _data[1], s=(_data[2] + 2) * 2, c=(_data[2] + 1) / 2, linewidths=0, alpha=0.7)
        #         plt.xlim(-3, 3)
        #         plt.ylim(-3, 3)
        #         for w in WW:
        #             plt.plot([-2, 0, 2], calculateY([-2, 0, 2], w))
        #         plt.savefig('%s'%it)
        #         # plt.show()
        #         plt.close(0)
        # print (acc)
        return WW, WU