예제 #1
0
(terrain_x, terrain_y,
 terrain_z) = np.load('simulationdata/visualize/subterrain.npy')
(road_xcords, road_ycords) = np.load('simulationdata/visualize/road_cords.npy')
(road_x, road_y) = (terrain_x[road_ycords,
                              road_xcords], terrain_y[road_ycords,
                                                      road_xcords])
r = terrain.road((road_xcords, road_ycords), (terrain_x, terrain_y, terrain_z))
print(r.maxdistance)
# r.plotroad()

sensor_loc = [[39, 82], [148, 158], [214, 57]]
sensor_real_loc = np.array(
    [[r.x[ele[0], ele[1]], r.y[ele[0], ele[1]], r.z[ele[0], ele[1]]]
     for ele in sensor_loc])

target_dw = [600, 3]
target_real_loc = r.dwtoxyz(target_dw[0], target_dw[1])
ar = arena.arena(3, 3, sensor_real_loc, target_real_loc)

ssb = np.array([[10, -3], [r.maxdistance - 10, 3]])

pslr = psos.PSO(scorefunc, 2, searchspaceboundaries=ssb)
pslr.solve_iters(10, verbose=True, out=True)

sol = []
for i in range(10):
    sol.append(pslr.solve_convergence(0.001, checkiters=20, out=True))

sorted_sols = sorted(sol, key=lambda solution: solution['extremum value'])
def solveforlocation():
    sensorloc_array = create_sensorlocarray(ndim, no_sensors)
    target_loc = create_target(ndim)
    curr_arena = ar.arena(no_sensors, ndim, sensorloc_array, target_loc)

    # curr_arena = localize(no_sensors,np.load('psosolsens.npy'));
    # curr_arena = localize(no_sensors,np.load('gdssolsens.npy'));
    # curr_arena = localize(no_sensors,np.load('fr.npy'));

    # indx = 0;
    # lst_score = float("inf");
    # pos_tracker = np.empty(np.shape(pso.current_pos));
    # spread = 0;
    # centroid = np.empty(pso.no_dim);

    ExampleSolSpacePoint = np.random.random((1, ndim))
    iterindx = 1
    gdsolver = gds.GDS(curr_arena.get_score, curr_arena.gradient_score,
                       ExampleSolSpacePoint)
    psosolver = psos.PSO(curr_arena.get_score, 10, ndim, 1, 1.5, 1,
                         ExampleSolSpacePoint,
                         [-10 * np.ones(ndim), 10 * np.ones(ndim)])

    psodata = stlog.statelogger(
        'localize_psodata', 'localize_psolog',
        np.vstack(
            (psosolver.current_pos, curr_arena.sensor_loc,
             curr_arena.target_loc)), psosolver.centroid, psosolver.spread,
        psosolver.globalmin)  # Plotter is  just expecting these.

    # gddata = stlog.statelogger('gddata2', 'gdslog',np.vstack((gdsolver.new_point, curr_arena.sensor_loc, curr_arena.target_loc)),
    #                            gdsolver.new_point, 0,gdsolver.curr_arena.get_score(gdsolver.new_point)) #Combined position data of moving point sensors and sensor target, i.e the object to be localized, in the first vstack array.
    # Dummy gdsolver.new_point to replace centroid that the plotter is expecting in the second term.
    indx = 0
    # while np.abs(np.amin(pso.curr_score)-lst_score)>.001:
    point = ExampleSolSpacePoint

    while indx < 300:
        psosolver.update_pos()
        psosolver.update_currscores()
        psosolver.update_selfmin()
        psosolver.update_globalmin()
        psosolver.update_velocities()
        (centroid, spread) = psosolver.calc_swarm_props()
        print('\n \n \n \n \n')
        print('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \n')
        print("The running index is " + str(indx) + "\n")
        print(str(psosolver.globalminlocation))
        indx += 1
        if psosolver.no_particles < 20:
            psosolver.report()

        print('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \n')
        curr_score = curr_arena.get_score(point)
        point = gdsolver.get_descented_point(point)
        descented_score = curr_arena.get_score(point)
        deltascore = descented_score - curr_score
        curr_score = descented_score
        gdsolver.report()
        psodata.add_state(
            np.vstack((psosolver.current_pos, curr_arena.sensor_loc,
                       curr_arena.target_loc)), psosolver.centroid,
            psosolver.spread, psosolver.globalmin)
    i = 0
    gradient = 0
    for ele in avgspace_some:
        curr_obj = ops.optsenpmt(ele,no_sensors,dim,alpha,sigma)
        gradient+=curr_obj.gradient_score(sensorloc)
        i += 1
    return 0-gradient/i


# def score_glb(sensorloc):
#     return currscript_score(sensorloc,avgspace,no_sensors,dim,alpha,sigma)
#
# def gradient_glb(sensorloc):
#     return
#
psol = pssol.PSO(currscript_score, 20, no_sensors*dim,.5,.5,1,-50+np.random.random(no_sensors*dim)*100,[-10*np.ones(no_sensors*dim),10*np.ones(no_sensors*dim)])

point = np.random.random(no_sensors*dim)
gdssol = gdslr.GDS(currscript_score,currscript_gradient,point)

rec_obj_pso = stlg.statelogger('optms_pso','optmspsolog',psol.curr_score,psol.globalmin,psol.current_pos)

rec_obj_gds = stlg.statelogger('optms_gds','optmsgdslog',gdssol.score_func(point))


for i in range(100):
    psol.update_pos()
    psol.update_currscores()
    psol.update_selfmin()
    psol.update_globalmin()
    psol.update_velocities()