def CostFunction(X): # original_initial_orientation = np.array([[-1.0,-0.000194820380622269,0.0],[0.0,1.0,0.0]]) original_initial_orientation = np.array([[1.0, 0.0, 0.0], [0.0, 1.0, 0.0]]) current_initial_orientation = create_initial_orientation( X, original_initial_orientation) # print('current initial orientation: ',current_initial_orientation ) variables = [ main_tra.t, main_tra.acceleration, main_tra.rotvel, main_tra.initial_position, current_initial_orientation, main_tra.initial_velocity ] current_trajectory = Trajectory(variables) current_trajectory.solver() tra_cur = current_trajectory.getdisp() np.savetxt('tra_cur.csv', tra_cur, fmt='%.5f', delimiter=',') #----------------------------------------------------------------------------------------------------------------------- # Compute Error between Reference and current using the Selected method in ErrorType and generate MinErrorList #----------------------------------------------------------------------------------------------------------------------- ErrorTotal = ComputeError(tra_ref, tra_cur) # MinErrorList will be written in DE_Log.txt to keep track of the evolution of the computed Minimum Error. # Last entry of MinErrorList corresponds to minimum error found up that point in the Optimization run. global best_IO global MinErrorList if MinErrorList == []: MinErrorList.extend([ErrorTotal]) best_IO.extend([current_initial_orientation]) elif ErrorTotal < MinErrorList[-1]: MinErrorList.extend([ErrorTotal]) best_IO.extend([current_initial_orientation]) else: MinErrorList.extend([MinErrorList[-1]]) best_IO.extend([best_IO[-1]]) #------------------------------------------------------------------------------------------------------------------- # Create a log File to keep track evolution #------------------------------------------------------------------------------------------------------------------- Log_file = 'DE_Log.txt' variables = ['alpha1', 'alpha2', 'phi'] FileDelimiter = ',' OptParameterNames = [variables[i] for i in range(len(X))] if not os.path.isfile(Log_file): with open(Log_file, 'w+') as Log: for names in OptParameterNames: print(names) Log.write(names + FileDelimiter) Log.write('Error' + FileDelimiter) Log.write('Min_Error' + '\n') OptParameterValues = [X[i] for i in range(len(X))] with open(Log_file, 'a') as Log_1: for Vals in OptParameterValues: Log_1.write(str(Vals) + FileDelimiter) Log_1.write(str(ErrorTotal) + FileDelimiter) Log_1.write(str(MinErrorList[-1]) + '\n') print('MinErTotal', MinErrorList[-1], 'CIO', best_IO[-1][0, :], best_IO[-1][1, :]) # print('MinErTotal',MinErrorList[-1]) return ErrorTotal
bounds = [[0, 360]] #------------------------------------------------------------------------------------------------------- #Define global reference: #ref_trajectory = read_frame(main_video.fin,main_video.input_videos_folder,main_video.trans)#Reference_displacements #Numpy array ntsx3 #tra_ref = ref_trajectory.reading_frame() variables_ref = [ main_tra.t, main_tra.acceleration, main_tra.rotvel, main_tra.initial_position, main_tra.initial_orientation, main_tra.initial_velocity ] ref_trajectory = Trajectory(variables_ref) ref_trajectory.solver() tra_ref = ref_trajectory.getdisp() np.savetxt('tra_ref.csv', tra_ref, fmt='%f', delimiter=',') #Optimization = CostFunction([45.,45.,180.]) #print('TotalError: ',Optimization) #------------------------------------------------------------------------------------------------------------------- # Optimization #------------------------------------------------------------------------------------------------------------------- Opt_SimplifiedModel = differential_evolution(CostFunction, bounds, strategy=Strategy, maxiter=MaxGenerations, popsize=Np, tol=0.01, mutation=F,