def store2movie( self, movie=None, mkwargs=dict(), pkwargs=dict(), fkwargs=dict(), sel=slice(None), mp=True, ): xt = np.ndarray((2, 3)) use = resource.getrusage(resource.RUSAGE_SELF) usc = resource.getrusage(resource.RUSAGE_CHILDREN) xt[:, :] = np.array(( time.time(), use.ru_utime + usc.ru_utime, use.ru_stime + usc.ru_stime, ))[np.newaxis, :] fkw = dict(size=(800, 400)) fkw = dict(fkw, **fkwargs) mkw = dict(delay=0.1, mp=mp) mkw = dict(mkw, **mkwargs) pkw = dict(q=('eps', 'T'), plot='plot2') pkw = dict(pkw, **pkwargs) plot = pkw.pop('plot') q = pkw.pop('q') plotter = getattr(self.copy_plot_data(), plot) make_movie( movie, generator=plotter, canvas=MPLCanvas, ckwargs=fkw, gargs=(q, ), gkwargs=pkw, values=self.store[sel], data='kwargs', **mkw, ) use = resource.getrusage(resource.RUSAGE_SELF) usc = resource.getrusage(resource.RUSAGE_CHILDREN) xt[1, :] = np.array(( time.time(), use.ru_utime + usc.ru_utime, use.ru_stime + usc.ru_stime, ))[np.newaxis, :] xm = xt[1] - xt[0] x = np.hstack((xm, np.sum(xm[1:]))) print( f'[EVO] movie: time: {time2human(x[0]):>8s} real, {time2human(x[1]):>8s} user, {time2human(x[2]):>8s} sys, {time2human(x[3]):>8s} cpu total.' )
def bend_movie(filename='bend.webm', fps=60, res=5, steps=600, mp=False, **kwargs): rmax = 1e6 rmin = 200 rr = rmax / (np.arange(steps) / (steps - 1) * (rmax / rmin - 1) + 1) h = 150 w = np.pi * rmin fkwargs = dict( xhot=20, thot=1e8, mbase='F', ) fkwargs.update(kwargs) def generator(r, fig): d = Diffuse( geo='s', nz=round(h / res), nx=round(w * (h + r) / (r * res)), method='solve4', r=r, lx=w, lz=h, **fkwargs, ) d.plot( vmax=1e8, vmin=3e6, xlim=[-w * 1.1, w * 1.1], ylim=[-(2 * rmin + h) * 1.1, h * 1.1], fig=fig, pos=[0.1, 0.3, 0.875, 0.65], cbpos=[0.1, 0.1, 0.875, 0.1], ) make_movie( filename, generator=generator, canvas=MPLCanvas, data='r', values=rr, fps=fps, mp=mp, )
# Run the given simulation for up to 100k time steps. num_balanced = 0 for s in range(10 ** 5): inputs = sim.get_scaled_state() action = net.serial_activate(inputs) # Apply action to the simulated cart-pole force = discrete_actuator_force(action) sim.step(force) # Stop if the network fails to keep the cart within the position or angle limits. # The per-run fitness is the number of time steps the network can balance the pole # without exceeding these limits. if abs(sim.x) >= sim.position_limit or abs(sim.theta) >= sim.angle_limit_radians: break num_balanced += 1 print('Pole balanced for {0:d} of 100k time steps'.format(num_balanced)) print() print("Final conditions:") print(" x = {0:.4f}".format(sim.x)) print(" x_dot = {0:.4f}".format(sim.dx)) print(" theta = {0:.4f}".format(sim.theta)) print("theta_dot = {0:.4f}".format(sim.dtheta)) print() print("Making movie...") make_movie(net, discrete_actuator_force, 15.0, "nn_movie.mp4")
THETA_1.append(theta_1) THETA_1_DOT.append(theta_1_dot) THETA_2.append(theta_2) THETA_2_DOT.append(theta_2_dot) # Stop if the network fails to keep the cart within the position or angle limits. # The per-run fitness is the number of time steps the network can balance the pole # without exceeding these limits. if env.outside_bounds(): break steps += 1 # Display results print('Pole balanced for {0} of 1000 steps'.format(steps)) x, x_dot, theta_1, theta_1_dot, theta_2, theta_2_dot = env.get_state() print() print("Final conditions:") print(" x = {0:.4f}".format(x)) print(" x_dot = {0:.4f}".format(x_dot)) print(" theta_1 = {0:.4f}".format(theta_1 * 180 / math.pi)) print("theta_1_dot = {0:.4f}".format(theta_1_dot * 180 / math.pi)) print(" theta_2 = {0:.4f}".format(theta_2 * 180 / math.pi)) print("theta_2_dot = {0:.4f}".format(theta_2_dot * 180 / math.pi)) print() # Create representative movie (different evaluation) print("Making movie...") make_movie(network, 15.0, "solution.mp4")
# Run the given simulation for up to 120 seconds. balance_time = 0.0 while sim.t < 120.0: inputs = sim.get_scaled_state() action = net.advance(inputs, sim.time_step, sim.time_step) # Apply action to the simulated cart-pole force = discrete_actuator_force(action) sim.step(force) # Stop if the network fails to keep the cart within the position or angle limits. # The per-run fitness is the number of time steps the network can balance the pole # without exceeding these limits. if abs(sim.x) >= sim.position_limit or abs(sim.theta) >= sim.angle_limit_radians: break balance_time = sim.t print('Pole balanced for {0:.1f} of 120.0 seconds'.format(balance_time)) print() print("Final conditions:") print(" x = {0:.4f}".format(sim.x)) print(" x_dot = {0:.4f}".format(sim.dx)) print(" theta = {0:.4f}".format(sim.theta)) print("theta_dot = {0:.4f}".format(sim.dtheta)) print() print("Making movie...") make_movie(net, discrete_actuator_force, 15.0, "ctrnn-movie.mp4")
# Run the given simulation for up to 120 seconds. balance_time = 0.0 # while sim.t < 120.0: # inputs = sim.get_scaled_state() # action = winner_net.activate(inputs) # #action = winner_net.my_activate(inputs) # # Apply action to the simulated cart-pole # force = discrete_actuator_force(action) # sim.step(force) # # # Stop if the network fails to keep the cart within the position or angle limits. # # The per-run fitness is the number of time steps the network can balance the pole # # without exceeding these limits. # if abs(sim.x) >= sim.position_limit or abs(sim.theta) >= sim.angle_limit_radians: # break # # balance_time = sim.t # # # print('Pole balanced for {0:.1f} of 120.0 seconds'.format(balance_time)) # # print() # print("Final conditions:") # print(" x = {0:.4f}".format(sim.x)) # print(" x_dot = {0:.4f}".format(sim.dx)) # print(" theta = {0:.4f}".format(sim.theta)) # print("theta_dot = {0:.4f}".format(sim.dtheta)) # print() # print("Making movie...") make_movie(winner_net, discrete_actuator_force, 10.0, "feedforward-movie.mp4")
# Run the given simulation for up to 120 seconds. balance_time = 0.0 while sim.t < 120.0: inputs = sim.get_scaled_state() action = net.activate(inputs) # Apply action to the simulated cart-pole force = discrete_actuator_force(action) sim.step(force) # Stop if the network fails to keep the cart within the position or angle limits. # The per-run fitness is the number of time steps the network can balance the pole # without exceeding these limits. if abs(sim.x) >= sim.position_limit or abs(sim.theta) >= sim.angle_limit_radians: break balance_time = sim.t print('Pole balanced for {0:.1f} of 120.0 seconds'.format(balance_time)) print() print("Final conditions:") print(" x = {0:.4f}".format(sim.x)) print(" x_dot = {0:.4f}".format(sim.dx)) print(" theta = {0:.4f}".format(sim.theta)) print("theta_dot = {0:.4f}".format(sim.dtheta)) print() print("Making movie...") make_movie(net, discrete_actuator_force, 15.0, "feedforward-movie.mp4")
sim.theta) >= sim.angle_limit_radians: break balance_time = sim.t print('Pole balanced for {0:.1f} of 120.0 seconds'.format(balance_time)) print() print("Final conditions:") print(" x = {0:.4f}".format(sim.x)) print(" x_dot = {0:.4f}".format(sim.dx)) print(" theta = {0:.4f}".format(sim.theta)) print("theta_dot = {0:.4f}".format(sim.dtheta)) print() print("Making movie...") make_movie(net, discrete_actuator_force, 30.0, "winner-{}-feedforward-movie.mp4".format(i)) i += 5 # load the winner with open('winner-feedforward', 'rb') as f: c = pickle.load(f) print('Loaded genome:') print(c) # Load the config file, which is assumed to live in # the same directory as this script. local_dir = os.path.dirname(__file__) config_path = os.path.join(local_dir, 'config-feedforward') config = neat.Config(neat.DefaultGenome, neat.DefaultReproduction, neat.DefaultSpeciesSet, neat.DefaultStagnation,
def hole_movie(filename='hole.webm', geo='s', fps=60, res=5, steps=600, xhotf=0.1, mp=False, **kwargs): rmax = 1e6 rmin = .1 rr = np.exp( np.arange(steps) / (steps - 1) * (np.log(rmin) - np.log(rmax))) * rmax h = 150 fkwargs = dict( thot=1e8, mbase='F', geo=geo, ) fkwargs.update(kwargs) pkwargs = dict() if geo == 'e': pkwargs['mirror'] = 4 angle = 0.5 * np.pi elif geo == 's': mirror = 2 angle = np.pi else: raise AttributeError(f'Geometry not defined: {geo}') def generator(r, fig): w = r * angle x = r + h d = Diffuse( nz=round(h / res), nx=round(w * 200 / (r * res)), method='solve4', r=r, lx=w, lz=h, xhot=x * xhotf, **fkwargs, ) d.plot( vmax=1e8, vmin=3e6, xlim=1.05 * x * np.array([-1, 1]), ylim=1.05 * x * np.array([-1, 1]) - r, fig=fig, pos=[0.1, 0.2, 0.875, 0.75], cbpos=[0.1, 0.025, 0.875, 0.1], **pkwargs, ) make_movie( filename, generator=generator, canvas=MPLCanvas, ckwargs=dict(size=(600, 720)), data='r', values=rr, fps=fps, mp=mp, )
def main(): optpar = optparse.OptionParser(usage = "usage: %prog [options] config.ini") optpar.add_option("-C", "--continuous", action="store_true", dest="continuous", default=False, help="don't quit") optpar.add_option("-R", "--reset", action="store_true", dest="reset", default = False, help="reset the aKMC simulation, discarding all data") optpar.add_option("-f", "--force", action="store_true", dest="force", default = False, help="force a reset, no questions asked") optpar.add_option("-r", "--restart", action="store_true", dest="restart", default = False, help="restart the aKMC simulations from a clean dynamics.txt file") optpar.add_option("-s", "--status", action="store_true", dest="print_status", default = False, help = "print the status of the simulation and currently running jobs") optpar.add_option("-q", "--quiet", action="store_true", dest="quiet", default=False,help="only write to the log file") optpar.add_option("-m", "--movie", action="store", dest="movie_type", default = "", help="Specify the type of movie to make [dynamics, states, fastestpath, fastestfullpath, graph, processes]. Process movies are specified like so: --movie processes,statenumber,processlimit. Where processes is the string processes, statenumber is the number of the state that you want to view, and process limit is the maximum number of processes you would like in the movie. The returned processes are reverse sorted by rate such that the fastest processes is the first in the movie.") optpar.add_option("-M", "--separate-movie-files", action="store_true", dest="separate_movie_files", default=False, help="Do not write the movie into a single file but use a separate POSCAR for every state. These are created in the \"movies\" directory. Only useful in conjunction with --movie.") optpar.add_option("-n", "--no-submit", action="store_true", dest="no_submit", default=False,help="don't submit searches; only register finished results") (options, args) = optpar.parse_args() if len(args) > 1: print "akmc.py takes only one positional argument" sys.argv = sys.argv[0:1] if len(args) == 1: sys.argv += args #always run from the directory where the config file is #os.chdir(os.path.dirname(args[0])) #XXX: config is ugly as it finds out where the config file is directly from # sys.argv instead of being passed it. #import sys if len(sys.argv) > 1: config.init(sys.argv[-1]) else: config.init() #set options.path_root to be where the config file is if given as an arg if config.path_root.strip() == '.' and len(args) == 1: config.path_root = os.path.abspath(os.path.dirname(args[0])) os.chdir(config.path_root) if options.no_submit: config.comm_job_buffer_size = 0 #setup logging logging.basicConfig(level=logging.DEBUG, filename=os.path.join(config.path_results, "akmc.log"), format="%(asctime)s %(levelname)s:%(name)s: %(message)s", datefmt="%F %T") logging.raiseExceptions = False if not options.quiet: rootlogger = logging.getLogger('') console = logging.StreamHandler() console.setLevel(logging.INFO) formatter = logging.Formatter("%(message)s") console.setFormatter(formatter) rootlogger.addHandler(console) lock = locking.LockFile(os.path.join(config.path_results, "lockfile")) # Some options are mutually exclusive. Let's check them now. exclusive_options = {} if len(options.movie_type) > 0: exclusive_options['movie_type'] = True else: exclusive_options['movie_type'] = False exclusive_options['print_status'] = options.print_status exclusive_options['reset'] = options.reset if sum(exclusive_options.values()) > 1: offending_options = [ k for k,v in exclusive_options.iteritems() if v ] optpar.error("Options %s are mutually exclusive" % ", ".join(offending_options)) if len(options.movie_type) > 0: states = get_statelist(config.main_temperature / 11604.5) movie.make_movie(options.movie_type, config.path_root, states, options.separate_movie_files) sys.exit(0) # From the config file: The Novotny and C&V (ASKMC) methods should not be used together. if config.sb_on and config.askmc_on: logger.error("Both superbasin methods should not be used at the same time") sys.exit(1) if options.print_status: states = get_statelist(config.main_temperature / 11604.5) start_state_num, time, previous_state_num, first_run, previous_temperature =\ get_akmc_metadata() current_state = states.get_state(start_state_num) if config.sb_on: sb_scheme = get_superbasin_scheme(states) sb = sb_scheme.get_containing_superbasin(current_state) else: sb = None print print "General" print "-------" if not sb: print "Current state:", start_state_num else: print "Current state:", start_state_num, "in superbasin", sb.id print "Number of states:",states.get_num_states() print "Time simulated: %.3e seconds" % time print print "Current State" print "-------------" if not sb: print "Confidence: %.4f" % current_state.get_confidence() else: print "Superbasin Confidence: %.4f" % sb.get_confidence() non_ignored_states = set(sb._get_filtered_states()) for s in sb.states: if s in non_ignored_states: ignore_string = "" else: ignore_string = " (no exit from superbasin found)" print " %4i: %.4f%s" % (s.number, s.get_confidence(sb), ignore_string) print "Unique Saddles:", current_state.get_unique_saddle_count() print "Good Saddles:", current_state.get_good_saddle_count() print "Bad Saddles:", current_state.get_bad_saddle_count() print "Percentage bad saddles: %.1f" % (float(current_state.get_bad_saddle_count())/float(max(current_state.get_bad_saddle_count() + current_state.get_good_saddle_count(), 1)) * 100) print comm = communicator.get_communicator() print "Saddle Searches" print "---------------" print "Searches in queue:", comm.get_queue_size() print if config.sb_on: print "Superbasins" print "-----------" for i in sb_scheme.superbasins: print "%s: %s" % (i.id, i.state_numbers) sys.exit(0) elif options.reset: if options.force: res = 'y' else: res = raw_input("Are you sure you want to reset (all data files will be lost)? (y/N) ").lower() if len(res)>0 and res[0] == 'y': def attempt_removal(thing): if thing is None: return if os.path.isdir(thing): shutil.rmtree(thing) os.mkdir(thing) os.removedirs(thing) elif os.path.isfile(thing): os.remove(thing) rmthings = [config.path_jobs_out, config.path_jobs_in, config.path_incomplete, config.path_states, config.path_scratch, config.kdb_name, config.kdb_scratch_path, config.sb_path, config.sb_recycling_path, config.debug_results_path, os.path.join(config.path_root, "searchdata"), os.path.join(config.path_results, "askmc_data.txt"), os.path.join(config.path_results, "searches.log"), os.path.join(config.path_results, "dynamics.txt"), os.path.join(config.path_results, "info.txt"), os.path.join(config.path_results, "akmc.log"), os.path.join(config.path_results, "jobs.tbl"), os.path.join(config.path_root, "results"), os.path.join(config.path_root, "prng.pkl"), os.path.join(config.path_root, "explorer.pickle"), os.path.join(config.path_root, "temperatures.dat"), os.path.join(config.path_root, "client.log"), os.path.join(config.path_root, "lockfile"), ] for thing in rmthings: attempt_removal(thing) if not options.quiet: print "Reset" sys.exit(0) else: print "Not resetting" sys.exit(1) elif options.restart: string_sb_clear = "" if options.force: res = 'y' else: res = raw_input("Are you sure you want to restart (remove dynamics.txt, info.txt and akmc.log)? (y/N) ").lower() if len(res)>0 and res[0] == 'y': # remove akmc data that are specific for a trajectory dynamics_path = os.path.join(config.path_results, "dynamics.txt") info_path = os.path.join(config.path_results, "info.txt") log_path = os.path.join(config.path_results, "akmc.log") jobs_path = os.path.join(config.path_results, "jobs.tbl") for i in [info_path, dynamics_path, log_path, jobs_path]: if os.path.isfile(i): os.remove(i) if config.sb_on: if options.force: res = 'y' else: res = raw_input("Should the superbasins be removed? (y/N) ").lower() # remove superbasin data (specific for a trajectory) if len(res)>0 and res[0] == 'y': # remove directory superbasins if os.path.isdir(config.sb_path): shutil.rmtree(config.sb_path) #XXX: ugly way to remove all empty directories containing this one os.mkdir(config.sb_path) os.removedirs(config.sb_path) # remove superbasins files from states dirctories state_dirs = os.listdir(config.path_states) for i in state_dirs: if i != 'state_table': superbasin_file = os.path.join(config.path_states, i) superbasin_file = os.path.join(superbasin_file, config.sb_state_file) if os.path.isfile(superbasin_file): os.remove(superbasin_file) string_sb_clear = " with directory 'superbasins' and files named '" string_sb_clear += str(config.sb_state_file) + "' removed" if not options.quiet: print "Restart"+string_sb_clear+"." sys.exit(0) else: print "Not restarting" sys.exit(1) if lock.aquirelock(): if options.continuous or config.comm_type == 'mpi': # define a wait method. if config.comm_type == 'mpi': from mpiwait import mpiwait wait = mpiwait elif options.continuous: if config.comm_type == "local": # In local, everything is synchronous, so no need to wait here. wait = lambda: None else: wait = lambda: sleep(10.0) else: raise RuntimeError("You have found a bug in EON!") # Run a specified number of steps or forever. steps = 0 while True: steps = akmc(config, steps) if (config.akmc_max_kmc_steps > 0 and steps >= config.akmc_max_kmc_steps): break wait() # In MPI mode we need to signal exit to all processes. # TODO: This is the sledgehammer method, it would be cleaner to # communicate to all clients that they should exit. if config.comm_type == 'mpi': from mpi4py import MPI MPI.COMM_WORLD.Abort(0) else: akmc(config) else: logger.info("Server is locked by pid %i" % lock.pid) sys.exit(1)
print() ''' # Run the given simulation for up to 120 seconds. while sim.t < 15.0: inputs = sim.get_scaled_state() action = net.activate(inputs) sim.step(action) # Stop if the network fails to keep the cart within the position or angle limits. # The per-run fitness is the number of time steps the network can balance the pole # without exceeding these limits. if abs(sim.x) >= sim.position_limit or abs(sim.theta) >= sim.angle_limit_radians: break balance_time = sim.t print('Pole balanced for {0:.1f} of 120.0 seconds'.format(balance_time)) print() print("Final conditions:") print(" x = {0:.4f}".format(sim.x1)) print(" x_dot = {0:.4f}".format(sim.x2)) print(" theta = {0:.4f}".format(sim.theta)) print("theta_dot = {0:.4f}".format(sim.dtheta)) print() ''' print("Making movie...") make_movie(net, 10.0, "feedforward-movie.mp4")
sound = AudioSegment.from_file("./sound/nomal/voice.wav", "wav") sound_time = int(sound.duration_seconds) # cmd = 'play ./sound/{}/voice.wav'.format("nomal") # subprocess.check_output(cmd, shell=True) long_cmd=""" sox ./music/am_lov.wav ./music/second.wav trim 0 {} && \ sox -n -r 16000 ./music/air.wav trim 0 {} && \ sox ./music/lovers.wav ./music/aft.wav trim {} fade 3 && \ sox ./music/air.wav ./music/aft.wav ./music/third_ch.wav && \ sox ./music/third_ch.wav ./music/third.wav fade 0 {} 3 && \ sox ./sound/voiceAir.wav ./sound/nomal/voice.wav ./sound/comp_voice.wav && \ sox -m ./music/fadou.wav ./music/second.wav ./music/third.wav ./sound/comp_voice.wav ./movie/movie_sound.wav """.format(35+sound_time, 32+sound_time, 32+sound_time, 42+sound_time) # subprocess.check_output(long_cmd, shell=True) movie_sound = AudioSegment.from_file("./movie/movie_sound.wav", "wav") movie_time = int(movie_sound.duration_seconds) make_movie(movie_time) mv_cmd = "ffmpeg -y -i ./movie/video.mp4 -i ./movie/movie_sound.wav -vcodec libx264 ./movie/tenki.mp4" subprocess.check_output(mv_cmd, shell=True) video = open('./movie/tenki.mp4', 'rb') twitter = key.Twi_API.make_token response = twitter.upload_video(media=video, media_type='video/mp4', media_category="tweet_video", check_progress=True) twitter.update_status(status="#今日のゆかりん天気予報 ({})".format(firth_msg[1]), media_ids=[response['media_id']]) video.close()