def learn(self, n_trajectories: int) -> None: '''Learn an optimal policy given an environment.''' # Simulate a series of trajectories in the environment. trajectories = self.simulate_n_trajectories(n_trajectories) # Compile the necessary information for the TRPO update. from ipdb import set_trace as debug debug() # VALIDATE STATES INDEXING. states = trajectories[:, :, 0] actions = trajectories[:, :, 1] action_vectors = trajectories[:, :, 2] rewards = trajectories[:, :, 3] # TRPO update! theta = self.flatten_theta() # Compute the policy gradient -> Fx = g. g = self.session.run(self.policy_gradient, feed_dict={ self.action_vectors: action_vectors, self.actions: actions, self.states: states }) # Evaluate the natural gradient using conjugate gradient descent. natural_gradient = conjugate_gradient_descent(fisher_vp, g)
def image_path_to_image_id(path_to_image): """Convert the path to an image to a proper image_id in the new style.""" path_list = path_to_image.split("/") date = path_list[-3] site = path_list[-2] image_number = path_list[-1].replace(".JPG", "") # Extract name of site. try: site_name = re.search(r"([\w\.\s]+)", site)[1].strip() site_name = site_name.replace(".", "").replace(" ", "_").lower() site_name = SITE_NAME_MAPPING[site_name] except: print("Cannot extract site name.") if site_name not in skip_sites: print(site_name) debug() else: return None # Extract altitude. try: altitude = re.search(r"\((\d+)\)", site)[1] except: print("Cannot extract site altitude.") return None # Extract date. match = re.search(DATE_REGEX, date) if match is None: print("Cannot extract data.") return None else: year = match[1] month = match[2] day = match[3] image_id = f"{year}-{month}-{day}-{site_name}-{altitude}-{image_number.replace('DJI', 'IMG')}" return image_id
def main(): # create the environment env = gym.make('FrozenLake-v0') # uncomment next line to try the deterministic version # env = gym.make('Deterministic-4x4-FrozenLake-v0') print_env_info(env) print_model_info(env, 0, lake_env.DOWN) print_model_info(env, 1, lake_env.DOWN) print_model_info(env, 14, lake_env.RIGHT) # input('Hit enter to run a random policy...') gamma = 0.9 policy, i = rl.value_iteration(env, gamma) print(policy) policy, _, i, i = rl.policy_iteration(env, gamma) print(policy) debug() # total_reward, num_steps = run_policy(env, policy) # debug() total_reward, num_steps = run_random_policy(env) print('Agent received total reward of: %f' % total_reward) print('Agent took %d steps' % num_steps)
def _upsert(cls, resource_id=None): '''Upsert a resource. Responds to a patch to the URL.''' debug() params = assemble_params(cls, 'upsert', resource_id, request) data = deserialize(request.json) # Attempt to find the resource using query. matches = cls.find_where(data['query']) if len(matches) == 0: # no matches cls._obj = cls.create(data) params['resp'] = cls._to_response(cls._obj._doc) else: params['resp'] = cls._to_response(matches[0]) return (params['resp'], params['status_code'], {})
def outline_to_lines(asker, root): debug() result = asker.ask(fields.get_field(cached_lines(), root)).answer if result is not None: return asker.reply(answer=result) base_headline = asker.ask(fields.get_field(headline(), root)).firm_answer root = asker.refresh(root) prefix = "* " if convert.check_hard(asker, is_pointer(), root) else " " full_headline = strings.string_concat(T.from_str(prefix), base_headline) root = asker.refresh(root) children = asker.ask(fields.get_field(visible_children(), root)).firm_answer body = empty_lines() for child in lists.iterator(asker, children): section = asker.ask(outline_to_lines(child)).firm_answer body = concat_lines(body, section) result = concat_lines(one_line(full_headline), indent_lines(body)) asker.update(updates.set_field(cached_lines(), result), root) return asker.reply(answer=result)
def extract_tiles_from_image( image, annotations, tile_size=128, target_species=None, include_target=True, max_nb_tiles=2000, ): """Pull the given annotation from the image.""" labels = [] tiles = np.array([]) for annotation in annotations: if "plant" not in annotation.keys(): continue if include_target: if plant_to_id[annotation["plant"]] != target_species: continue else: # require this species if plant_to_id[annotation["plant"]] == target_species: continue # Extract tiles, if we're still here. if target_species < 0: debug() col, row = annotation["col"], annotation["row"] col_, row_ = ( col * 4000 / annotation["imageWidth"], row * 3000 / annotation["imageHeight"], ) tiles_ = np.array(create_rotated_pics(image, row_, col_, tile_size, 25)) if tiles.shape[0] > 0: tiles = np.vstack((tiles, tiles_)) else: tiles = tiles_ # Only return max numbers of tiles. if tiles.shape[0] > max_nb_tiles: break return tiles
def single_path(self) -> None: '''Perform a single path rollout.''' # Randomly sample the initial state. state = self.env.reset() from ipdb import set_trace as debug debug() # LOOK AT THE STATE OBJECT. done = False # Stores (S_t, A_(t-1), AV_(t-1), R_t). history = [(state, -1, -1)] # Run a sample trajectory. while not done: # Evaluate the policy to find an action to take - A_(t-1). action_vectors, action = self.act(state) # Take the action! state, reward, done, info = self.env.step(action) history.append((state, action, action_vectors, reward)) return np.array(history)
def smart_batch(scientific_name, tile_size=128, nb_tiles_per_class=1000, samples_per_tile=10): """Generate a smart batch that balances the target against a mix of its confusors.""" # Extract tiles associated with the primary target. print("> Extracting targets.") X, y = extract_smart_training_tiles( scientific_name, label=1, tile_size=tile_size, nb_tiles_per_class=nb_tiles_per_class, samples_per_tile=samples_per_tile, ) # Now extract tiles from each of the confusing classes. confusors = CONFUSORS[scientific_name] nb_tiles_per_confusor = int(nb_tiles_per_class / len(confusors)) print("> Extracting confusors.") for confusor in confusors: X_, y_ = extract_smart_training_tiles( confusor, label=0, tile_size=tile_size, nb_tiles_per_class=nb_tiles_per_confusor, samples_per_tile=samples_per_tile, ) if X_ is not None: try: X = np.vstack((X, X_)) y = np.hstack((y, y_)) except: debug() idx = np.arange(X.shape[0]) np.random.shuffle(idx) X = X[idx, :] y = y[idx] return X, y
def plot_output_q2(ys: list = [], ts: list = [], rs: list = []) -> None: '''Plot the output for question 2.''' plt.figure() lines = [] debug() labels = [ 'Susceptible Population', 'Infected Population', 'Recovered Population' ] titles = [fr'$\beta$ = {b[1]}' for i, b in enumerate(rs) if i % 3 == 0] cols = [fr'$X_0$ = {b[2]}' for b in rs] for i, tup in enumerate(zip(ys, ts, rs)): y, t, rb = tup S, I, R = zip(*y) S, I, R = np.array(S), np.array(I), np.array(R) plt.subplot(3, 3, i + 1) for var, label in zip([S, I, R], labels): line, = plt.plot(t, var) lines.append(line) if i < 3: plt.title(titles[i]) if i % 3 == 0: plt.ylabel(cols[int(i / 3)]) # plt.xlabel('Time') # plt.ylabel('Compartments') # plt.title(fr'$R_0$ = {rb[0]}, $\beta$ = {rb[1]}') plt.grid() plt.subplots_adjust(hspace=0.5) plt.figlegend((lines[0], lines[1], lines[2]), labels) plt.show()
def reintroduce_modifier(asker, property, update, input, output): result = translator.dispatch(asker, property, update, input, output) if result is None: debug() return result
plt.ylabel('Total Yield (1,000 $)') plt.title('Price Of Oats vs. Total Yield') plt.grid() plt.subplot(3, 1, 2) corn, wheat, oats = zip(*xopt.values()) plt.plot(oat_price_range, corn, label='Corn') plt.plot(oat_price_range, wheat, label='Wheat') plt.plot(oat_price_range, oats, label='Oats') plt.xlabel('Price of Oats ($/acre)') plt.ylabel('Crops Planted (acre)') plt.legend(loc='lower left') plt.title('Price of Oats vs. Crops Planted') plt.grid() plt.subplot(3, 1, 3) debug() water, labor, land = zip(*lamb.values()) plt.plot(oat_price_range, water, label='Water') plt.plot(oat_price_range, labor, label='Labor') plt.plot(oat_price_range, land, label='Land') plt.legend(loc='lower left') plt.title('Price of Oats vs. Value of Constraints') plt.xlabel('Price of Oats ($/acre)') plt.ylabel('Shadow Price ($/unit)') plt.grid() plt.subplots_adjust(hspace=0.5) plt.show()
def ask(self, Q, handler=None, **kwargs): response = self.ask_func(Q, parent=self) if getattr(self, "slow", False): debug() return response.set(self.process_response(response.value, Q=Q, handler=handler))
for tag in training_tags: try: tag_counts[tag] += 1 except: tag_counts[tag] = 1 for ngram in nltk.ngrams(training_words, 2): training_word_ngrams.append(ngram) for ngram in nltk.ngrams(training_tags, 2): training_tag_ngrams.append(ngram) for ngram in training_tag_ngrams: try: ngram_tag_counts[ngram] += 1 except: ngram_tag_counts[ngram] = 1 tag_transition_probs = {} for tag in tag_counts: tag_transition_probs[tag] = {} for ngram in ngram_tag_counts: if ngram[0] != tag: continue tag_transition_probs[tag][ngram] = ngram_tag_counts[ngram] / tag_counts[ngram[0]] debug()
def map_summaries(): """Return the map summary information.""" flights = [] years = glob(f"{ARGOS_ROOT}/*") maps = [] for year in years: months = sorted(glob(f"{year}/*")) # print(year) # print(months) for month in months: days = sorted(glob(f"{month}/*")) for day in days: sites = sorted(glob(f"{day}/*")) for site in sites: altitudes = sorted(glob(f"{site}/*")) for altitude in altitudes: if altitude.split("/")[-1] != "obliques": images = sorted(glob(f"{altitude}/images/*.JPG")) images.sort(key=os.path.getmtime) nb_images = len(images) if nb_images > 0: # if no images, just why? first_meta = extract_info(images[0]) last_meta = extract_info(images[-1]) start = first_meta["date_time"] try: datetime_obj = datetime.strptime( start, "%Y:%m:%d %H:%M:%S" ) except: debug() datetime_str = datetime_obj.strftime("%d %b %Y") smalldate = datetime_obj.strftime("%Y-%m-%d") time_str = datetime_obj.strftime("%I-%M%p") alt = altitude.split("/")[-1] sitename = site.split("/")[-1] year_ = year.split("/")[-1] month_ = month.split("/")[-1] day_ = day.split("/")[-1] end = last_meta["date_time"] path_to_map = f"{altitude}/maps/map_small.jpg" path_to_geomap = f"{altitude}/maps/map.tif" path_to_images = f"{altitude}/images" map_id = f"{year_}-{month_}-{day_}-{sitename}-{alt}" # Get map sizes. if len(glob(path_to_geomap)) == 0: continue # ds = gdal.Open(path_to_geomap) # geo_rows = ds.RasterYSize # geo_cols = ds.RasterXSize # small_map = plt.imread(path_to_map) # small_rows, small_cols, _ = small_map.shape else: continue # No images? Don't return this map. if len(glob(f"{altitude}/maps/map.tif")) > 0: maps.append( { "map_id": map_id, "year": year_, "month": month_, "day": day_, "site": sitename, "altitude": alt, "nb_images": nb_images, # "geo_rows": geo_rows, # "geo_cols": geo_cols, # "small_rows": small_rows, # "small_cols": small_cols, # "lat": f"{first_meta['img_lat']:.4f}", # "lon": f"{first_meta['img_lon']:.4f}", "start": start, "end": end, "time": time_str.replace("-", ":"), "datetime": datetime_str, "path_to_geomap": "/".join( path_to_geomap.split("/")[-7:] ), "path_to_map": "/".join( path_to_map.split("/")[-7:] ), "path_to_images": "/".join( path_to_images.split("/")[-6:] ), } ) maps = sorted(maps, key=lambda x: x["start"]) return maps