Exemple #1
0
def check_events(ai_settings, screen, stats, sb, play_button, ship, aliens,
                 bullets):
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            fd.write_data(stats.high_score)

            sys.exit()
        elif event.type == pygame.KEYDOWN:
            check_keydown_events(event, ai_settings, screen, ship, bullets)

        elif event.type == pygame.KEYUP:
            check_keyup_events(event, ship)
        elif event.type == pygame.MOUSEBUTTONDOWN:
            mouse_x, mouse_y = pygame.mouse.get_pos()
            check_play_button(ai_settings, screen, stats, sb, play_button,
                              ship, aliens, bullets, mouse_x,
                              mouse_y)  #又想抽死自己 play
def delete_employee(id):

    #0 load in the database
    database = data.read_data()

    #1 find the index of the employee that we want to delete
    employee_to_delete = data.find_employee_by_id(id)

    #2 remove that index from the database list
    for index,e in enumerate(database):
        if e['id'] == employee_to_delete['id']:
            # update the entry in the databse
            database[index] = employee_to_delete
            break # break out of the loop because each id is unique

    del database[index]

    #3 write the whole database back to the file
    data.write_data(database)

    return redirect(url_for('display_employees'))
def process_edit_employee(id):
    
    #1 - read in the database
    database = data.read_data()

    #2 - update the entry in the database
    employee_to_update = data.find_employee_by_id(id)
    employee_to_update['first_name'] = request.form.get('firstname')
    employee_to_update['last_name'] = request.form.get('lastname')
    employee_to_update['salary'] = request.form.get('salary')
    employee_to_update['title'] = request.form.get('title')
 
    #find the index to replace
    for index,e in enumerate(database):
        if e['id'] == employee_to_update['id']:
            # update the entry in the databse
            database[index] = employee_to_update
            break # break out of the loop because each id is unique

    #3 - overwrite the file with our current version of database
    data.write_data(database)
    return redirect(url_for('display_employees'))
Exemple #4
0
def write(data, olution):
    name = file_name + '_' + str(evaluate(data, solution)) + output_extension
    path = join(output_solve_folder, name)
    write_data(path, solution)
Exemple #5
0
# init bodies
data.read_data(bodies)

# init gui
slider = gui.Slider(buttons)
start_stop = gui.StartStop(buttons)

clock = pygame.time.Clock()
running = True
while running:
    for event in pygame.event.get():
        buttons.update(event)
        if event.type == pygame.QUIT:
            running = False
            data.write_data()

    screen.fill(BG_COLOR)

    # determining how fast is the system drawn based on the position of the slider.
    dt = slider.calc_value()
    if start_stop.on and bodies.size > 0:
        # first we calculate new positions on all bodies
        bodies.new_state(dt)
        # then we move them
        bodies.update(WINDOW_SIZE)

    buttons.draw(screen)
    bodies.draw(screen)

    pygame.display.flip()
Exemple #6
0
def load(args, client):
    data = load_data(args["input"])
    return write_data(client, data)
Exemple #7
0
def evolutionary_algorithm():
    best_timetable = None
    data = dt.load_data(input_file)
    neighbour = mutation.neighbour
    for i in range(num_runs):
        chromosome = dt.generate_chromosome(data)

        for j in range(max_generations):
            new_chromosome = neighbour(deepcopy(chromosome))
            ft = cost_function(chromosome)
            if ft == 0:
                break
            ftn = cost_function(new_chromosome)
            if ftn <= ft:
                chromosome = new_chromosome
            if j % 200 == 0:
                print('Iteration', j, 'cost', cost_function(chromosome))

        print('Run', i + 1, 'cost', cost_function(chromosome), 'chromosome', chromosome)

        if best_timetable is None or cost_function2(chromosome) <= cost_function2(best_timetable):
            best_timetable = deepcopy(chromosome)

    chromosome = best_timetable

    neighbour2 = mutation.neighbour2

    for j in range(3 * max_generations):
        new_chromosome = neighbour2(deepcopy(chromosome))
        ft = cost_function2(chromosome)
        ftn = cost_function2(new_chromosome)
        if ftn <= ft:
            chromosome = new_chromosome
        if j % 200 == 0:
            print('Iteration', j, 'cost', cost_function2(chromosome))

    print('Run', 'cost', cost_function2(chromosome), 'chromosome', chromosome)

    dt.write_data(chromosome[0], output_file)

    professor_hard = True
    classroom_hard = True
    group_hard = True
    allowed_classrooms = True

    # Check hard constraints
    for single_class in chromosome[0]:
        if single_class['Assigned_classroom'] not in single_class['Classroom']:
            allowed_classrooms = False
    for profesor in chromosome[1]:
        for i in range(len(chromosome[1][profesor])):
            if chromosome[1][profesor][i] > 1:
                professor_hard = False
    for Classroom in chromosome[2]:
        for i in range(len(chromosome[2][Classroom])):
            if chromosome[2][Classroom][i] > 1:
                classroom_hard = False
    for group in chromosome[3]:
        for i in range(len(chromosome[3][group])):
            if chromosome[3][group][i] > 1:
                group_hard = False

    print('Are hard restrictions for professors satisfied:', professor_hard)
    print('Are hard restrictions for classrooms satisfied:', classroom_hard)
    print('Are hard restrictions for groups satisfied:', group_hard)
    print('Are hard restrictions for allowed classrooms satisfied:', allowed_classrooms)

    # Check preferred order statistics
    subjects_cost = 0
    for single_class in chromosome[4]:
        subject_cost = 0
        for lab in chromosome[4][single_class]['L']:
            for practice in chromosome[4][single_class]['V']:
                for group in lab[1]:
                    if group in practice[1] and lab[0] < practice[0]:
                        subject_cost += 1
            for lecture in chromosome[4][single_class]['P']:
                for group in lab[1]:
                    if group in lecture[1] and lab[0] < lecture[0]:
                        subject_cost += 1
        for practice in chromosome[4][single_class]['V']:
            for lecture in chromosome[4][single_class]['P']:
                for group in practice[1]:
                    if group in lecture[1] and practice[0] < lecture[0]:
                        subject_cost += 1
        subjects_cost += subject_cost
        print('Subject cost for subject', single_class, 'is:', subject_cost)
    print('Total subject cost:', subjects_cost)

    # Check group statistics
    total_group_cost = 0
    total_group_load = 0
    max_group_cost = 0
    for group in chromosome[3]:
        group_cost = 0
        group_load = 0
        for day in range(5):
            last_seen = 0
            found = False
            current_load = 0
            for hour in range(12):
                time = day * 12 + hour
                if chromosome[3][group][time] >= 1:
                    current_load += 1
                    if not found:
                        found = True
                    else:
                        group_cost += (time - last_seen - 1)
                    last_seen = time
            if current_load > 6:
                group_load += 1
        print('Group cost for group', group, 'is:', group_cost, ', number of hard days:', group_load)
        if max_group_cost < group_cost:
            max_group_cost = group_cost
        total_group_cost += group_cost
        total_group_load += group_load
    print('Maximum group cost is:', max_group_cost)
    print('Average group cost is:', total_group_cost / len(chromosome[3]))
    print('Total group load is:', total_group_load)

    # Check professor statistics
    total_prof_cost = 0
    total_prof_load = 0
    free_hour = True
    max_prof_cost = 0
    for prof in chromosome[1]:
        prof_cost = 0
        prof_load = 0
        for day in range(5):
            last_seen = 0
            found = False
            current_load = 0
            for hour in range(12):
                time = day * 12 + hour
                if chromosome[1][prof][time] >= 1:
                    if time == 59:
                        free_hour = False
                    current_load += 1
                    if not found:
                        found = True
                    else:
                        prof_cost += (time - last_seen - 1)
                    last_seen = time
            if current_load > 6:
                prof_load += 1
        print('Prof cost for prof', prof, 'is:', prof_cost, ', number of hard days:', prof_load)
        if max_prof_cost < prof_cost:
            max_prof_cost = prof_cost
        total_prof_cost += prof_cost
        total_prof_load += prof_load
    print('Max prof cost is:', max_prof_cost)
    print('Average prof cost is:', total_prof_cost / len(chromosome[1]))
    print('Total prof load is:', total_prof_load)
    print('Free hour:', free_hour, ', 59')