Esempio n. 1
0
def GA_CAV_included_best_UE(control_portion):
    constraint_ueq = [
        lambda x: sum(x) - 5 * control_portion,
    ]
    # ga = SA(func=CAV_UE, x0=[0]*15, n_dim=15, size_pop=50, max_iter=200, lb=[0]*15, ub=[5]*15, precision=1e-7, constraint_eq=constraint_eq)
    # ga = PSO(func=CAV_UE, n_dim=15, size_pop=50, max_iter=200, lb=[0]*15, ub=[5]*15, precision=1e-7, constraint_eq=constraint_eq)
    set_run_mode(CAV_UE, 'multiprocessing')
    ga = GA(func=CAV_UE,
            n_dim=2,
            size_pop=50,
            max_iter=800,
            lb=[0] * 2,
            ub=[5 * control_portion] * 2,
            precision=1e-5,
            constraint_ueq=constraint_ueq)
    best_x, best_y = ga.run()
    print('best_x:', best_x, '\n', 'best_y:', best_y)
    return best_y
            return step3

    return costly_function


for task_type in ('io_costly', 'cpu_costly'):
    costly_function = generate_costly_function(task_type=task_type)

    def obj_func(p):
        costly_function()
        x1, x2 = p
        x = np.square(x1) + np.square(x2)
        return 0.5 + (np.square(np.sin(x)) - 0.5) / np.square(1 + 0.001 * x)

    for mode in ('common', 'multithreading', 'multiprocessing'):
        set_run_mode(obj_func, mode)
        ga = GA(func=obj_func,
                n_dim=2,
                size_pop=10,
                max_iter=5,
                lb=[-1, -1],
                ub=[1, 1],
                precision=1e-7)
        start_time = datetime.datetime.now()
        best_x, best_y = ga.run()
        print(
            'on {task_type} task,use {mode} mode, costs {time_costs}s'.format(
                task_type=task_type,
                mode=mode,
                time_costs=(datetime.datetime.now() -
                            start_time).total_seconds()))
Esempio n. 3
0
    mask_out_path = os.path.join(r'/tmp/pycharm_project_836/img_out', mask_out_path)
    cv2.imwrite(mask_out_path, mask_out)

    # iou
    act_path = 'rbg_1.jpg'

    iou, pa = eval(mask_out_path, act_path)

    return -iou  # 最优化取最小值!!取负数!!


if __name__ == '__main__':
    # iou=find_path_func(20,0.3,0.2,0.2)
    # print(iou)

    set_run_mode(find_path_func, 'multiprocessing')
    print("pao1")
    pso = PSO(func=find_path_func, n_dim=4, pop=15, max_iter=30, lb=[0.1, 0.1, 0.1, 0.1], ub=[0.28, 0.8, 0.8, 0.8],
              w=0.8, c1=0.5, c2=0.5)
    print("pao2")
    pso.record_mode = True
    print("pao3")

    #5
    print("pao4")
    pso.run(5)
    print('best_x is ', pso.gbest_x, 'best_y is', pso.gbest_y)
    plt.plot(pso.gbest_y_hist)
    plt.savefig('5.png')
    record_dict = pso.record_value
    f = open('5.txt', 'w')
    def obj_func4_1(self, p):
        time.sleep(0.1)  # say that this function is very complicated and cost 0.1 seconds to run
        x1, x2 = p
        x = np.square(x1) + np.square(x2)
        return 0.5 + (np.square(np.sin(x)) - 0.5) / np.square(1 + 0.001 * x)

    def obj_func4_2(self, p):
        time.sleep(0.1)  # say that this function is very complicated and cost 0.1 seconds to run
        x1, x2 = p
        x = np.square(x1) + np.square(x2)
        return 0.5 + (np.square(np.sin(x)) - 0.5) / np.square(1 + 0.001 * x)


your_class = YourClass()

set_run_mode(your_class.obj_func2, 'vectorization')
set_run_mode(your_class.obj_func3, 'parallel')

ga1 = GA(func=your_class.obj_func1, n_dim=2, size_pop=10, max_iter=5, lb=[-1, -1], ub=[1, 1], precision=1e-7)
ga2 = GA(func=your_class.obj_func2, n_dim=2, size_pop=10, max_iter=5, lb=[-1, -1], ub=[1, 1], precision=1e-7)
ga3 = GA(func=your_class.obj_func3, n_dim=2, size_pop=10, max_iter=5, lb=[-1, -1], ub=[1, 1], precision=1e-7)

start_time = datetime.datetime.now()
best_x, best_y = ga1.run()
print('common mode, time costs: ', (datetime.datetime.now() - start_time).total_seconds())

start_time = datetime.datetime.now()
best_x, best_y = ga2.run()
print('vector mode, time costs: ', (datetime.datetime.now() - start_time).total_seconds())

start_time = datetime.datetime.now()
Esempio n. 5
0
print('common mode, time costs: ',
      (datetime.datetime.now() - start_time).total_seconds())

# %% type2:矢量化


def obj_func2(p):
    time.sleep(
        0.1
    )  # say that this function is very complicated and cost 1 seconds to run
    x1, x2 = p[:, 0], p[:, 1]
    x = np.square(x1) + np.square(x2)
    return 0.5 + (np.square(np.sin(x)) - 0.5) / np.square(1 + 0.001 * x)


set_run_mode(obj_func2, 'vectorization')

ga2 = GA(func=obj_func2,
         n_dim=2,
         size_pop=10,
         max_iter=5,
         lb=[-1, -1],
         ub=[1, 1],
         precision=1e-7)
start_time = datetime.datetime.now()
best_x, best_y = ga2.run()
print('vector mode, time costs: ',
      (datetime.datetime.now() - start_time).total_seconds())

# %% type3:parallel
        x1, x2 = p
        x = np.square(x1) + np.square(x2)
        return 0.5 + (np.square(np.sin(x)) - 0.5) / np.square(1 + 0.001 * x)

    def obj_func4_2(self, p):
        time.sleep(
            0.1
        )  # say that this function is very complicated and cost 0.1 seconds to run
        x1, x2 = p
        x = np.square(x1) + np.square(x2)
        return 0.5 + (np.square(np.sin(x)) - 0.5) / np.square(1 + 0.001 * x)


your_class = YourClass()

set_run_mode(your_class.obj_func2, 'vectorization')
set_run_mode(your_class.obj_func3, 'multithreading')

ga1 = GA(func=your_class.obj_func1,
         n_dim=2,
         size_pop=10,
         max_iter=5,
         lb=[-1, -1],
         ub=[1, 1],
         precision=1e-7)
ga2 = GA(func=your_class.obj_func2,
         n_dim=2,
         size_pop=10,
         max_iter=5,
         lb=[-1, -1],
         ub=[1, 1],