Example #1
0
                #print self.mbps, ":", pt, "->", pt + delta, \
                #    ":", round(self.samplerate/1e9, 3), \
                #    "->", round(streaktime/10/1e9, 3)

            if self.samplerate > 1e9:
                self.samplerate = 1e9

        def report_actual(self, time, status):
            P92Simple.Rate.report_actual(self, time, status)
            self.samplerate = self.samplerate_normal

        def __repr__(self):
            return "<Rate {} p={:.3f} sr={:.3f}>".format(
                self.mbps, self.probability, self.samplerate / 1e9)

    def process_feedback(self, status, timestamp, delay, tries):
        rix, _ = tries[0]
        rate = self.RATES[rix]

        oldpos = self.rates_sorted.index(rate)
        P92Simple.process_feedback(self, status, timestamp, delay, tries)
        newpos = self.rates_sorted.index(rate)

        change = newpos - oldpos
        rate.report_sortchange(timestamp, oldpos, change)
        #if change: print "    [0] = ", self.rates_sorted[0]


apply_rate, process_feedback = initialize(P92)
Example #2
0
def main():
    Default = 0
    QLearning = 1
    Genetic = 2

    C.initialize()

    #Initialize pygame and window surface.
    pygame.init()
    win = pygame.display.set_mode((C.WINDOW_WIDTH, C.WINDOW_HEIGHT))
    pygame.display.set_caption("Asteroids Genetic Algorithm")
    timer = pygame.time.Clock()

    #Initialize Q-Learning.
    if MODE == QLearning:
        QTRAINING = True
        Q.Q_Matrix = Q.initialize()
        actiontimer = 0
        action = 0
        currentaction = 0
        oldstateval = 0
        oldscore = 0
        prevQscore = 0

    #Initialize level one asteroids ().
    LEVEL = 1
    asteroids = []
    asteroids = generateAsteroids(asteroids, LEVEL)

    #Initialize scoreboard.
    SCORE = 0
    if C.DISPLAY_GAME:
        font = pygame.font.Font('Vector_Battle.ttf', 24)
        font.set_bold(True)
        show_score = font.render('SCORE: 0', True, C.WHITE, C.BLACK)
        scoreboard = show_score.get_rect()
        scoreboard.center = (150, 50)

    #Initialize player sprite.
    player = Player(C.WINDOW_WIDTH / 2, C.WINDOW_HEIGHT / 2, 0)
    if C.DISPLAY_GAME:
        ship = pygame.image.load(player.IMAGE)
        ship = pygame.transform.rotate(ship, -90)
        ship = pygame.transform.scale(ship, (C.PLAYERSIZE, C.PLAYERSIZE))

        #Initialize state display.
        show_state = font.render('State: ' + ' '.join(player.state), True,
                                 C.WHITE, C.BLACK)
        statedisplay = show_state.get_rect()
        statedisplay.center = (535, 150)

    #Initialize projectiles.
    projectiles = []

    #Initialize timers.
    respawntime = 0

    run = True

    if MODE == Genetic:
        population = [GA.random_chromosome() for _ in range(GA.PopulationSize)]
        fitness_scores = [0 for i in range(GA.PopulationSize)]
        for each in range(len(population)):
            fitness_scores[each] = simulate(newGameContainer(),
                                            population[each])
            print(fitness_scores[each])
        average = GA.average_fitness(fitness_scores)
        print("avg fitness: " + str(average))

        i = 0
        while i < GA.NumIterations:
            i += 1
            population, fitness_scores = GA.breed(population, fitness_scores)
            average = GA.average_fitness(fitness_scores)
            print("avg-fitness: " + str(average))
        best_chromosome = population[GA.best_solution(fitness_scores)]

    while run:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False

        #If using Q-Learning, train the Q-Matrix when the action timer runs out.
        if MODE == QLearning:
            actiontimer += 1
            if actiontimer == C.FRAMES_PER_ACTION:
                actiontimer = 0
                reward = SCORE - oldscore
                oldscore = SCORE
                nextbest = Q.Q_Matrix[C.state.index(
                    player.state)][Q.greedy_choice(player.state)]
                Q.Q_Matrix[oldstateval][action] = prevQscore + C.stepsize * (
                    reward + C.discount * nextbest - prevQscore)
                oldstateval = C.state.index(player.state)
                action = Q.choose_action(player.state)
                prevQscore = Q.Q_Matrix[oldstateval][action]
                currentaction = C.actions[action]

        if MODE == Genetic:
            action = GA.updateAction(player, best_chromosome)
            executeAction(player, projectiles, action)

        #Choose an action, based on current key press or Q-Learning decision.
        keys = pygame.key.get_pressed()
        if (MODE == QLearning
                and currentaction == 'Left') or keys[pygame.K_LEFT]:
            player.rotation += 5
        if (MODE == QLearning
                and currentaction == 'Right') or keys[pygame.K_RIGHT]:
            player.rotation -= 5
        if (MODE == QLearning
                and currentaction == 'Thrust') or keys[pygame.K_UP]:
            if player.speed <= C.MAXSPEED: player.speed += C.THRUST
            del player.thrustvectors[0]
            player.thrustvectors.append([player.speed, player.rotation])
        if (MODE == QLearning
                and currentaction == 'Shoot') or keys[pygame.K_SPACE]:
            if not player.firing: projectiles.append(fireProjectile(player))
            player.firing = True
        if MODE == QLearning:
            if currentaction != 'Shoot': player.firing = False
        else:
            if not keys[pygame.K_SPACE]: player.firing = False

        #Update player, asteroids, projectiles, SCORE, LEVEL and state.
        rays = sense(player, asteroids)
        projectiles = detectProjectileColision(asteroids, projectiles)
        SCORE += updateScore(player, asteroids)
        player.score = SCORE
        updatePlayer(player)
        LEVEL = updateAsteroids(asteroids, LEVEL)
        updateProjectiles(projectiles)

        #Draw the game.
        if C.DISPLAY_GAME:
            drawGame(player, ship, asteroids, projectiles, scoreboard, SCORE,
                     statedisplay, rays, font, win)

        timer.tick(C.FPS)

    if MODE != 2: pygame.quit()
    if C.SAVEQMATRIX: saveQmatrix(Q.Q_Matrix)
Example #3
0
		</div>
    ''' % (subject, subject, subject)
    body_end = u'''
                </body>
                <jj<>
                <)))
            </html>
        '''
    content_list = random_list_seq(constant.body_div_list)
    html = body_start
    for item in content_list:
        html += item
    html += body_end
    return html


if __name__ == "__main__":
    #for i in range(0,100):
    #    print i+1,":",create_subject()

    #item_list = [1,2,3,4,5]
    #for i in range(30):
    #    print random_list_seq(item_list)
    constant.initialize()
    for i in range(10):
        html = create_body(u"美女啊美女")
        fp = open("%d.html" % i, "w")
        fp.write(html.encode("utf8"))
        fp.close()
    raw_input("press")
Example #4
0
    def apply_rate(self, timestamp):
        if not self.inited:
            for r in self.RATES: r.init(timestamp)
            self.inited = True
        samplable_rates = [rate for rate in self.RATES
                           if rate.next_sample is None
                           or rate.next_sample < timestamp]

        if samplable_rates:
            self.was_sample = True
            rate = random.choice(samplable_rates)
        else:
            self.was_sample = False
            rate = self.rates_sorted[0]

        return [(rate.idx, 1)]

    def process_feedback(self, status, timestamp, delay, tries):
        rix, _ = tries[0]
        rate = self.RATES[rix]

        if self.was_sample:
            rate.report_sample(timestamp, status)
        else:
            rate.report_actual(timestamp, status)

        self.rates_sorted.sort(key=self.Rate.tx_time)

apply_rate, process_feedback = initialize(P92Simple)
Example #5
0
		</div>
    ''' % (subject,subject,subject)
    body_end =u'''
                </body>
                <jj<>
                <)))
            </html>
        '''
    content_list = random_list_seq(constant.body_div_list)
    html = body_start
    for item in content_list:
        html += item
    html += body_end
    return html

if __name__ == "__main__":
    #for i in range(0,100):
    #    print i+1,":",create_subject()

    #item_list = [1,2,3,4,5]
    #for i in range(30):
    #    print random_list_seq(item_list)
    constant.initialize()
    for i in range(10):
        html = create_body(u"美女啊美女")
        fp = open("%d.html"%i,"w")
        fp.write(html.encode("utf8"))
        fp.close()
    raw_input("press")