| @@ -0,0 +1,54 @@ | ||
| #!/usr/bin/env python | ||
| """ | ||
| Created on Thu Mar 01 11:24:43 2018 | ||
| @author: cartemic | ||
| """ | ||
|
|
||
| import subprocess | ||
| import os | ||
| from uuid import uuid4 | ||
| import sys | ||
|
|
||
|
|
||
| class Simulator(): | ||
| def __init__(self, instance): | ||
| self.instance = instance | ||
|
|
||
| def evaluate(self, waypoints): | ||
| filename = 'waypoints-' + str(uuid4()) | ||
| # filename = 'best-waypoints' | ||
|
|
||
| # output waypoints | ||
| with open(filename, 'w') as f: | ||
| for i in xrange(len(waypoints)): | ||
| f.write(str(waypoints[i][0]) + | ||
| ' ' + | ||
| str(waypoints[i][1]) + | ||
| '\n') | ||
|
|
||
| # evaluate score | ||
| score = float(subprocess.check_output(['simulator', | ||
| filename, | ||
| str(self.instance)]). | ||
| split()[-1][:-1]) | ||
|
|
||
| # remove file | ||
| # os.remove(filename) | ||
| return score | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| instance = 0 | ||
| num_points = 4 | ||
| basic_points = [[-10, -10], [10, 10]] | ||
| if len(sys.argv) > 1: | ||
| if len(sys.argv) > 2: | ||
| print('Too many arguments, using the first one') | ||
| try: | ||
| instance = int(sys.argv[1]) | ||
| except (TypeError, ValueError): | ||
| print('Please input something numeric. Defaulting to 0.') | ||
|
|
||
| test = Simulator(instance) | ||
| print test.evaluate(basic_points) |
| @@ -0,0 +1,51 @@ | ||
| #!/usr/bin/env python | ||
|
|
||
|
|
||
| from simulator import Simulator | ||
| from optimizer import optimize | ||
| from threading import Thread, Lock | ||
| from time import sleep | ||
|
|
||
|
|
||
| best_waypoints = [(-10, -10), (10, 10)] | ||
| is_done = False | ||
| waypoint_lock = Lock() | ||
|
|
||
|
|
||
| def record_waypoints(waypoints): | ||
| global best_waypoints | ||
| global waypoint_lock | ||
|
|
||
| with waypoint_lock: | ||
| best_waypoints = waypoints[:] | ||
|
|
||
|
|
||
| def done(): | ||
| with waypoint_lock: | ||
| return is_done | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| instance = 10 | ||
| s = Simulator(instance) | ||
|
|
||
| baseline_cost = s.evaluate([(-10, -10), (10, 10)]) | ||
|
|
||
| t = Thread(target=optimize, args=(s, record_waypoints, done)) | ||
| t.start() | ||
|
|
||
| # Give the other code some time to work. | ||
| # We will almost certainly give you more than 1 second. | ||
| sleep(1) | ||
| with waypoint_lock: | ||
| best_guess = best_waypoints[:] | ||
| is_done = True | ||
|
|
||
| print 'Baseline cost:', baseline_cost | ||
|
|
||
| t = Simulator(instance) | ||
| print 'Lowest cost found:', t.evaluate(best_guess) | ||
|
|
||
| with open('best-waypoints', 'w') as f: | ||
| for w in best_guess: | ||
| f.write('{0} {1}\n'.format(*w)) |
| @@ -0,0 +1,47 @@ | ||
| #!/usr/bin/env python | ||
| """ | ||
| Created on Thu Mar 15 10:15:13 2018 | ||
| @author: cartemic | ||
| """ | ||
|
|
||
| import copy | ||
| import inspect | ||
|
|
||
|
|
||
| class wingus(): | ||
| def do_stuff(self, thing1, thing2, thing3): | ||
| print thing1, thing2, thing3 | ||
|
|
||
|
|
||
| def intercept(f): | ||
| # make a copy of the class | ||
| newf = copy.deepcopy(f) | ||
|
|
||
| # collect built-in functions | ||
| builtin_functions = [function for function in dir(newf) | ||
| if '__' in function] | ||
|
|
||
| # collect custom functions | ||
| custom_functions = [function for function in dir(newf) | ||
| if '__' not in function] | ||
|
|
||
| # collect arguments | ||
| mean_args = inspect.getargspec(getattr(newf, custom_functions[0]))[0][1:] | ||
|
|
||
| # do mean things and then make it look like you didn't | ||
| def mean_function(f, *mean_args): | ||
| lists = [item for item in mean_args] | ||
| new_func = custom_functions[0] + '0' | ||
| exec('f.{0}(*lists)'.format(new_func)) in locals() | ||
|
|
||
| # replace original function with mean function, and move original to a | ||
| # new location so it can still be called by mean function | ||
| move_func = eval('f.{0}'.format(custom_functions[0])) | ||
| setattr(f, custom_functions[0]+'0', move_func) | ||
| setattr(f, custom_functions[0], mean_function) | ||
|
|
||
|
|
||
| intercept(wingus) | ||
| test = wingus() | ||
| test.do_stuff(1, 2, 3) |