def main(plot=False): warnings.filterwarnings('error', category=RuntimeWarning) days, fuel_mass, uglovi, snaga = pop_init(gen=1) print(days.shape) print(fuel_mass.shape) print(uglovi.shape) print(snaga.shape) pop_fit = np.empty(broj_jedinki) koeficijenti = np.empty((broj_jedinki, podaci.chebdeg + 1)) fitness = np.loadtxt('gen_99.txt', dtype=float, usecols=364) ind = np.argmin(fitness) pop_elita = None print('Fitness: ', fitness[ind]) for i in range(broj_gen): for j in range(30, 40): print(j) print('File fitness: ', fitness[j]) print('Launch offset:', days[j]) print('Fuel mass:', fuel_mass[j] / 255 * podaci.max_fuel_mass) #print('Uglovi:', (uglovi[j]%(2*pi))*360/(2*pi)) _r, _, _, min_dist_dest = simulacija_pogon.simulacija( days[j], fuel_mass[j], uglovi[j], snaga[j], y_max) # if pop_fit[j] == -1: pop_fit[j] = fitnes(min_dist_dest) koeficijenti[j] = chebfit(x_osa(broj_segmenata), uglovi[j], podaci.chebdeg) if plot: x, y = crtanje_planeta(plot=False) plt.plot(np.array(x[2]) / au, np.array(y[2]) / au, 'b--', np.array(x[3]) / au, np.array(y[3]) / au, 'r-.', _r[:, 0] / au, _r[:, 1] / au, 'g:', linewidth=0.9) plt.plot(0.0, 0.0, 'k*', markersize=7) plt.axis('scaled') plt.xlabel('x-osa [astronomska jedinica]') plt.ylabel('y-osa [astronomska jedinica]') plt.title('Flyby Marsa') plt.show() # print(podaci.trajanje) # pop_bit = np.array([[0, 1, 1, 0, 1, 0, 1, 0, 0, 1, 15.1], # [1, 1, 0, 1, 0, 0, 1, 0, 1, 0, 5.6], # [0, 0, 0, 1, 0, 1, 0, 0, 1, 1, 10.8], # [1, 0, 1, 0, 1, 0, 1, 1, 0, 1, 2.6], # [0, 1, 1, 0, 1, 0, 0, 0, 1, 0, 4.9]]) # pop_elita = np.array([[0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 1.5]]) pop_bit = float_to_bit(days[:, np.newaxis], fuel_mass, koeficijenti, snaga) # print(pop_bit) pop_bit_new, pop_elita = genetski_algoritam.genetski_algoritam( pop_bit, pop_elita, podaci.p_elit, podaci.p_mut) # print(pop_bit_new) # print(pop_elita) # date_time, fuel_mass, uglovi, snaga = bit_to_float(pop_bit_new) print(np.min(pop_fit))
def main(): warnings.filterwarnings("error", category=RuntimeWarning) comm = MPI.COMM_WORLD rank = comm.Get_rank() n = comm.Get_size() pop_elita = None if rank == 0: days, fuel_mass, uglovi, snaga = pop_init() pop_fit = np.empty(broj_jedinki) koeficijenti = np.empty((broj_jedinki, podaci.chebdeg + 1)) for i in range(broj_gen): for j in range(broj_jedinki): proc_id = comm.recv(source=MPI.ANY_SOURCE, tag=1) comm.send( (j, days[j], fuel_mass[j], uglovi[j], snaga[j], y_max), dest=proc_id) waiting = n while waiting > 0: data = comm.recv(source=MPI.ANY_SOURCE, tag=2) (j, r_, _, _, min_dist_dest) = data pop_fit[j] = fitnes(min_dist_dest) koeficijenti[j] = chebfit(x_osa(broj_segmenata), uglovi[j], podaci.chebdeg) waiting = waiting - 1 pop_bit = float_to_bit(days[:, np.newaxis], fuel_mass, koeficijenti, snaga, pop_fit) # print(pop_bit) pop_bit_new, pop_elita = genetski_algoritam.genetski_algoritam( pop_bit, pop_elita, podaci.p_elit, podaci.p_mut) days, fuel_mass, uglovi, snaga = bit_to_float(pop_bit_new) for it in range(broj_jedinki): days[it] = days[it] % max_time_span if i % 10 == 0: np.savetxt('gen_' + str(i) + '.txt', pop_bit_new, fmt='%d') for i in range(1, n): proc_id = comm.recv(source=MPI.ANY_SOURCE) comm.send(-1, dest=proc_id) else: while True: comm.send(comm.Get_rank(), dest=0, tag=1) data = comm.recv(source=0) if data != -1: _r, _v, _step, min_dist_dest = simulacija_pogon.simulacija( data[1], data[2], data[3], data[4], data[5]) comm.send((data[0], _r, _v, _step, min_dist_dest), dest=0, tag=2) else: print('Proc ' + str(comm.Get_rank()) + ' logs out') break
def main(plot=False): days, fuel_mass, uglovi, snaga = pop_init() pop_fit = np.empty(broj_jedinki) koeficijenti = np.empty((broj_jedinki, podaci.chebdeg + 1)) for i in range(broj_gen): start = tm.process_time() for j in range(broj_jedinki): # print(j) _r, _, _, min_dist_dest = simulacija_pogon.simulacija( days[j], fuel_mass[j], uglovi[j], snaga[j], y_max) # if pop_fit[j] == -1: pop_fit[j] = fitnes(min_dist_dest) koeficijenti[j] = chebfit(x_osa(broj_segmenata), uglovi[j], podaci.chebdeg) if plot: x, y = crtanje_planeta(plot=False) plt.plot(x[0], y[0], 'g', x[1], y[1], 'y', x[2], y[2], 'b--', x[3], y[3], 'r', _r[:, 0], _r[:, 1], linewidth=0.8) plt.plot(0.0, 0.0, 'k*', markersize=7) plt.axis('scaled') plt.show() print(tm.process_time() - start) print(podaci.trajanje) pop_bit = float_to_bit(days[:, np.newaxis], fuel_mass, koeficijenti, snaga, pop_fit) # print(pop_bit) pop_bit_new = genetski_algoritam.genetski_algoritam( pop_bit, podaci.p_elit, podaci.p_mut) date_time, fuel_mass, uglovi, snaga, pop_fit = bit_to_float( pop_bit_new) print(np.min(pop_fit))
def main(): comm = MPI.COMM_WORLD rank = comm.Get_rank() if rank==0: n = comm.Get_size() days, brod, uglovi, snaga = pop_init() pop_fit = np.empty(broj_jedinki) koeficijenti = np.empty((broj_jedinki, podaci.chebdeg + 1)) for i in range(broj_gen): for j in range(broj_jedinki): ID = comm.recv(source=MPI.ANY_SOURCE,tag = 1) comm.send ((j,days[j], brod[j], uglovi[j], snaga[j], y_max), dest = ID) waiting = n while waiting>0: data = comm.recv(source=MPI.ANY_SOURCE,tag = 2); (j,r_,_,_,min_dist_dest) = data pop_fit[j] = fitnes(min_dist_dest) koeficijenti[j] = chebfit(x_osa(broj_segmenata), uglovi[j], podaci.chebdeg) waiting = waiting-1 pop_bit = float_to_bit(days[:, np.newaxis], brod, koeficijenti, snaga, pop_fit) # print(pop_bit) pop_bit_new = genetski_algoritam.genetski_algoritam(pop_bit, podaci.p_elit, podaci.p_mut) days, brod, uglovi, snaga, pop_fit = bit_to_float(pop_bit_new) for i in range (1,n): ID = comm.recv(source=MPI.ANY_SOURCE); comm.send(-1, dest = ID); else: while True: comm.send (comm.Get_rank(),dest=0,tag = 1); data = comm.recv(source=0); if (data!=-1): _r, _v, _step, min_dist_dest = simulacija_pogon.simulacija(data[1], data[2], data[3], data[4], data[5]) comm.send(j,_r,_v,_step,min_dist_dest,dest = 0,tag =2) else: print('Proc ' + str(comm.Get_rank()) + ' logs out'); break;
def main(): warnings.filterwarnings("error", category=RuntimeWarning) comm = MPI.COMM_WORLD rank = comm.Get_rank() n = comm.Get_size() pop_elita = None if rank == 0: days, fuel_mass, uglovi, snaga = pop_init() print('DAYS: ', days.shape) print('FUEL MASS: ', fuel_mass.shape) print('UGLOVI: ', uglovi.shape) print('SNAGA: ', snaga.shape) pop_fit = np.empty(broj_jedinki) koeficijenti = np.empty((broj_jedinki, podaci.chebdeg + 1)) for i in range(broj_gen): for j in range(broj_jedinki): print('gen/jed ', (i, j)) proc_id = comm.recv(source=MPI.ANY_SOURCE, tag=1) comm.send( (j, days[j], fuel_mass[j], uglovi[j], snaga[j], y_max), dest=proc_id) waiting = broj_jedinki while waiting > 0: data = comm.recv(source=MPI.ANY_SOURCE, tag=2) (j, r_, _, _, min_dist_dest) = data pop_fit[j] = fitnes(min_dist_dest) if math.isnan(pop_fit[j]): print('min_dist_dest: ', min_dist_dest) raise ValueError('Fitness je NaN') koeficijenti[j] = chebfit(x_osa(broj_segmenata), uglovi[j], podaci.chebdeg) waiting = waiting - 1 pop_fit1 = pop_fit pop_bit = float_to_bit(days, fuel_mass, koeficijenti, snaga, pop_fit) if i % 10 == 9: np.savetxt('gen_' + str(i) + '.txt', pop_bit, fmt='%d') pop_bit_new, pop_elita = genetski_algoritam.genetski_algoritam( pop_bit, pop_elita, podaci.p_elit, podaci.p_mut) days, fuel_mass, uglovi, snaga = bit_to_float(pop_bit_new) print('fitness: ', np.all((pop_fit1 - pop_fit) == np.zeros(broj_jedinki))) for it in range(broj_jedinki): days[it] = days[it] % max_time_span f = open('fitness.txt', 'a') f.write( str(np.amin(pop_fit)) + ' ' + str(np.median(pop_fit)) + '\n') f.close() for i in range(1, n): comm.send((-1, -1), dest=i) else: while True: comm.send(comm.Get_rank(), dest=0, tag=1) data = comm.recv(source=0) if data != (-1, -1): _r, _v, _step, min_dist_dest = simulacija_pogon.simulacija( data[1], data[2], data[3], data[4], data[5]) comm.send((data[0], _r, _v, _step, min_dist_dest), dest=0, tag=2) else: print('Proc ' + str(comm.Get_rank()) + ' logs out') break