def test_all(self): l1 = len(storage.all('State')) state = State(name="State test all") state.save() output = storage.all('State') self.assertEqual(len(output), l1 + 1) self.assertIn(state.id, output.keys())
def test_view_one_state_wrong(self): """the id does not match a state""" state_args = {"name": "Zanzibar", "id": "ZA1"} state = State(**state_args) state.save() rv = self.app.get('{}/states/{}/'.format(self.path, "noID")) self.assertEqual(rv.status_code, 404) storage.delete(state)
def test_count_state(self): """test count with an argument""" test_len = len(storage.all("State")) a = State(name="test_state_count_arg") a.save() self.assertEqual(test_len + 1, storage.count("State")) storage.delete(a) self.assertEqual(test_len, storage.count("State"))
def test_save(self): test_len = len(storage.all()) a = Amenity(name="another") a.save() self.assertEqual(len(storage.all()), test_len + 1) b = State(name="california") self.assertNotEqual(len(storage.all()), test_len + 2) b.save() self.assertEqual(len(storage.all()), test_len + 2)
def test_update_state_bad_id(self): """test update with no matching id""" state_args = {"name": "Zanzibar", "id": "ZA6"} state = State(**state_args) state.save() rv = self.app.put('{}/states/{}/'.format(self.path, "noID"), content_type="application/json", data=json.dumps({"id": "Z"}), follow_redirects=True) self.assertEqual(rv.status_code, 404) storage.delete(state)
def test_count(self): """test count all""" test_len = len(storage.all()) a = Amenity(name="test_amenity") a.save() self.assertEqual(test_len + 1, storage.count()) b = State(name="State test count") b.save() self.assertEqual(test_len + 2, storage.count()) storage.delete(b) self.assertEqual(test_len + 1, storage.count())
def test_update_state_bad_json(self): """test update with ill formedt json""" state_args = {"name": "Zanzibar", "id": "ZA5"} state = State(**state_args) state.save() rv = self.app.put('{}/states/{}/'.format(self.path, state.id), content_type="application/json", data={"id": "Z"}, follow_redirects=True) self.assertEqual(rv.status_code, 400) self.assertEqual(rv.get_data(), b"Not a JSON") storage.delete(state)
def test_view_one_state(self): """test retrieving one state""" state_args = {"name": "Zanzibar", "id": "ZA3"} state = State(**state_args) state.save() rv = self.app.get('{}/states/{}/'.format(self.path, state_args["id"])) self.assertEqual(rv.status_code, 200) self.assertEqual(rv.headers.get("Content-Type"), "application/json") json_format = getJson(rv) self.assertEqual(json_format.get("name"), state_args["name"]) self.assertEqual(json_format.get("id"), state_args["id"]) storage.delete(state)
def test_getstates(self): """test listing all states""" state_args = {"name": "Zanzibar"} state = State(**state_args) state.save() rv = self.app.get('{}/states/'.format(self.path)) self.assertEqual(rv.status_code, 200) self.assertEqual(rv.headers.get("Content-Type"), "application/json") json_format = getJson(rv) self.assertTrue(type(json_format), list) self.assertIn(state_args["name"], [e.get("name") for e in json_format]) storage.delete(state)
def test_delete_state(self): """test delete a state""" state_args = {"name": "Zanzibar", "id": "ZA"} state = State(**state_args) state.save() rv = self.app.delete('{}/states/{}/'.format(self.path, state_args["id"]), follow_redirects=True) self.assertEqual(rv.status_code, 200) self.assertEqual(rv.headers.get("Content-Type"), "application/json") json_format = getJson(rv) self.assertEqual(json_format, {}) self.assertIsNone(storage.get("State", state_args["id"]))
def test_update_state_id(self): """test cannot update state id""" state_args = {"name": "Zanzibar", "id": "ZA4"} state = State(**state_args) state.save() rv = self.app.put('{}/states/{}/'.format(self.path, state.id), content_type="application/json", data=json.dumps({"id": "Z"}), follow_redirects=True) self.assertEqual(rv.status_code, 200) self.assertEqual(rv.headers.get("Content-Type"), "application/json") json_format = getJson(rv) self.assertEqual(json_format.get("name"), state_args["name"]) self.assertEqual(json_format.get("id"), state_args["id"]) storage.delete(state)
def test_editor(self, exploration_id, state_id=None, **kwargs): """Gets the user and exploration id if the user can edit it. Args: self: the handler instance exploration_id: the exploration id state_id: the state id, if it exists **kwargs: any other arguments passed to the handler Returns: The user and exploration instance, if the user is authorized to edit this exploration. Also, the state instance, if one is supplied. Raises: self.NotLoggedInException: if there is no current user. self.UnauthorizedUserException: if the user exists but does not have the right credentials. """ user = users.get_current_user() if not user: self.redirect(users.create_login_url(self.request.uri)) return exploration = Exploration.get(exploration_id) if not exploration.is_editable_by(user): raise self.UnauthorizedUserException( '%s does not have the credentials to edit this exploration.', user) if not state_id: return handler(self, user, exploration, **kwargs) state = State.get(state_id, exploration) return handler(self, user, exploration, state, **kwargs)
def test_get_state(self): """test get with valid cls and id""" a = State(name="test_state3", id="test_3") a.save() result = storage.get("State", "test_3") self.assertEqual(a.name, result.name) # does not work as the database loses last argument tzinfo for datetime # self.assertEqual(a.created_at, result.created_at) self.assertEqual(a.created_at.year, result.created_at.year) self.assertEqual(a.created_at.month, result.created_at.month) self.assertEqual(a.created_at.day, result.created_at.day) self.assertEqual(a.created_at.hour, result.created_at.hour) self.assertEqual(a.created_at.minute, result.created_at.minute) self.assertEqual(a.created_at.second, result.created_at.second) storage.delete(a) result = storage.get("State", "test_3") self.assertIsNone(result)
def test_terminal_functions(self): state = State.state_from_field(self.field) self.assertFalse(state.is_terminal()) self.assertFalse(state.predators_have_collided()) self.assertFalse(state.prey_is_caught()) # Move prey to location of first predator self.field.get_prey().location = (0, 0) state = State.state_from_field(self.field) self.assertTrue(state.is_terminal()) self.assertFalse(state.predators_have_collided()) self.assertTrue(state.prey_is_caught()) # Move predator 1 to location of last predator self.field.get_predators()[0].location = (10, 0) state = State.state_from_field(self.field) self.assertTrue(state.is_terminal()) self.assertTrue(state.predators_have_collided()) self.assertFalse(state.prey_is_caught())
def test_predators_have_collided(self): f = Field(11, 11) predator1 = Predator(id="Plato", location=(1, 1)) predator2 = Predator(id="Pythagoras", location=(1, 1)) chip = Prey(id="Kant", location=(5, 5)) f.add_player(predator1) f.add_player(predator2) f.add_player(chip) s = State.state_from_field(f) self.assertTrue(s.predators_have_collided()) self.assertTrue(s.prey_is_caught() == False)
def init_players(self): """ After adding all the players to the field, this function will call the init_player function on all the players. :return: """ self.players.sort(key=lambda player: player.id) for player in self.players: player.init_player() self.state = State.state_from_field(self) self.initialized = True
def run_step(self): """ Runs a step of the current episode by telling all agents to pick their respective next action based on the state of the field. Then, it computes the locations of all players in the next state and possible rewards. Finally, new state and reward is distributed to all players so they can learn from their action. :return: """ assert(self.initialized is True) old_state = self.state actions = dict() rewards = dict() # for every player: for player in self.players: # call act() function on player and get desired action in return actions[player] = player.act(self.state) # compute next state based on actions chosen by players for pred in self.get_predators(): pred.location = self.transition(pred, actions[pred]) # if the prey has been caught, it cannot move anymore if not State.state_from_field(self).prey_is_caught(): self.get_prey().location = self.transition(self.get_prey(), actions[self.get_prey()]) # get new field state after every player moved new_state = State.state_from_field(self) # compute all rewards for player in self.players: rewards[player] = self.get_reward(new_state, player) # tell each player their new location and reward for player in self.players: player.update(old_state=old_state, new_state=new_state, actions=actions, rewards=rewards) self.steps += 1 self.update_state() return
def test_to_dict_id_str(self): """test type of 'id' value is a str""" s = State() s_dictionary = s.to_dict() self.assertEqual(str, type(s_dictionary['id']))
def test_class_name(self): """test __class__ key in dictionary""" s = State() state_dictionary = s.to_dict() self.assertIn('__class__', state_dictionary)
def test_created_and_updated_at_init(self): """created_at and updated_at attrs initialized to current datetime test""" s = State() self.assertEqual(s.created_at, s.updated_at)
def test_id(self): """id attr test""" s = State() self.assertEqual(str, type(s.id))
def test_state_type(self): """tests state instance is created""" s = State() self.assertEqual(type(State()), type(s))
def test_all_states(self): all_states = State.all_states(self.field) self.assertEqual(len(all_states), 1771561)
def test_save_method(self): """test save method""" dummy_instance = State() dummy_instance.save() self.assertNotEqual(dummy_instance.created_at, dummy_instance.updated_at)
def test_field_name(self): """ test first name field""" dummy_instance = State() self.assertEqual(self.state.name, "toor") self.assertIsInstance(self.state.name, str) self.assertEqual(dummy_instance.name, "")
def setUp(cls): """steup class """ cls.state = State() cls.state.name = "toor" cls.state.save()
def setUpClass(cls): '''setting method to tests instance''' cls.insta = State()
def test_uniq_id(self): """Tests unique user ids.""" l = [State().id for i in range(1000)] self.assertEqual(len(set(l)), len(l))
def test_state_from_field(self): state = State.state_from_field(self.field) self.assertEqual(state.relative_distances, [(5, 5), (5, -5), (-5, 5)])
def test_new(self): """Test new method.""" st = State(name="Washington") self.storage.new(st) store = list(self.storage._DBStorage__session.new) self.assertIn(st, store)
def run_wolf(n_episodes=1000): # initialize the environment field = Field(3, 3) """ initial state: | | | | |X|O|X| | | | | """ pred1loc = (0, 1) pred2loc = (2, 1) preyloc = (1, 1) predator1 = Predator(id="Plato", location=pred1loc) predator2 = Predator(id="Pythagoras", location=pred2loc) # WoLF predator1.plearner = Wolf_phc.create_greedy_plearner(field=field, agent=predator1) predator2.plearner = Wolf_phc.create_greedy_plearner(field=field, agent=predator2) field.add_player(predator1) field.add_player(predator2) chip = Prey(id="Kant", location=preyloc) chip.plearner = Wolf_phc.create_greedy_plearner(field=field, agent=chip, epsilon=0.01) field.add_player(chip) field.init_players() plot_state = State.state_from_field(field) num_steps = [] pred_win = [] value_of_pred1 = [] value_of_pred2 = [] value_of_prey = [] for i in range(0, n_episodes): predator1.location = pred1loc predator2.location = pred2loc chip.location = preyloc field.update_state() field.steps = 0 # run the simulation while not field.is_ended(): field.run_step() num_steps.append(field.steps) pred_win.append(field.state.prey_is_caught()) value_of_pred1.append(predator1.plearner.policy.get_probability_mapping(plot_state)) value_of_pred2.append(predator2.plearner.policy.get_probability_mapping(plot_state)) value_of_prey.append(chip.plearner.policy.get_probability_mapping(plot_state)) # print progress every 10% if n_episodes > 10 and i % (n_episodes / 10) == 0: print int(1.0 * i / n_episodes * 100), "%" # some list wrangling to get a list of 5 action lists with values for each predator vp1 = [[val[0] for val in sublist] for sublist in zip(*value_of_pred1)] vp2 = [[val[0] for val in sublist] for sublist in zip(*value_of_pred2)] vpc = [[val[0] for val in sublist] for sublist in zip(*value_of_prey)] # create plots colors = ["r", "b", "g", "k", "m"] actions = { (0, 0): "stay", (-1, 0): "left", (1, 0): "right", (0, -1): "up", (0, 1): "down" } plt.figure(figsize=(15, 15)) s = plt.subplot(3, 1, 1) s.set_yscale("log") for index, action in enumerate(predator1.actions): plt.plot(vp1[index], c=colors[index], label=actions[action]) plt.title("action probabilities for predator 1") plt.legend(loc="upper right") s = plt.subplot(3, 1, 2) s.set_yscale("log") for index, action in enumerate(predator2.actions): plt.plot(vp2[index], c=colors[index], label=actions[action]) plt.title("action probabilities for predator 2") # plt.legend(loc="upper left") s = plt.subplot(3, 1, 3) s.set_yscale("log") for index, action in enumerate(chip.actions): plt.plot(vpc[index], c=colors[index], label=actions[action]) plt.title("action probabilities for prey") plt.suptitle(str(n_episodes) + " episodes") plt.savefig(get_output_path() + "policychange-wolf-" + str(n_episodes) + ".pdf")
def test_get_method(self): """Test get method for the API""" test = State(name="California") test.save() self.assertEqual(storage.get("State", test.id), test)
def test_state(self): """test State creation with a keyword argument""" a = State(name="Kamchatka", id="Kamchatka666") a.save() self.assertIn("Kamchatka666", storage.all("State").keys())
def test_count(self): """ Test again!! angel gulity""" counter = storage.count(State) test = State(name="California") test.save() self.assertEqual(storage.count(State), counter + 1)
def test_dict_to_created_at_attr_type(self): """test dict -> instance's created_at attr type""" s = State() s_dictionary = s.to_dict() s2 = State(**s_dictionary) self.assertEqual(type(datetime.now()), type(s2.created_at))
def setUp(self): """Set up which instance to call""" self._class = State() self._class2 = State() self._name = "State"
def test_name_default(self): """tests default value of name attr""" s = State() self.assertEqual("", s.name)
def test_State_inheritence(self): '''Test that State class inherits from BaseModel.''' new_state = State() self.assertIsInstance(new_state, BaseModel)
def test_updated_at(self): """created_at attr test""" s = State() self.assertEqual(datetime, type(s.updated_at))
def post(self, exploration_id, state_id): """Handles feedback interactions with readers.""" values = {'error': []} exploration = Exploration.get(exploration_id) state = State.get(state_id, exploration) old_state = state payload = json.loads(self.request.get('payload')) # The 0-based index of the last content block already on the page. block_number = payload.get('block_number') params = self.get_params(state, payload.get('params')) # The reader's answer. answer = payload.get('answer') # Add the reader's answer to the parameter list. This must happen before # the interactive widget is constructed. params['answer'] = answer interactive_widget_properties = ( InteractiveWidget.get_with_params( state.widget.widget_id)['actions']['submit']) dest_id, feedback, rule, recorded_answer = state.transition( answer, params, interactive_widget_properties) if recorded_answer: EventHandler.record_rule_hit( exploration_id, state_id, rule, recorded_answer) # Add this answer to the state's 'unresolved answers' list. if recorded_answer not in old_state.unresolved_answers: old_state.unresolved_answers[recorded_answer] = 0 old_state.unresolved_answers[recorded_answer] += 1 # TODO(sll): Make this async? old_state.put() assert dest_id html_output, widget_output = '', [] # TODO(sll): The following is a special-case for multiple choice input, # in which the choice text must be displayed instead of the choice # number. We might need to find a way to do this more generically. if state.widget.widget_id == 'MultipleChoiceInput': answer = state.widget.params['choices'][int(answer)] # Append reader's answer. values['reader_html'] = feconf.JINJA_ENV.get_template( 'reader_response.html').render({'response': answer}) if dest_id == feconf.END_DEST: # This leads to a FINISHED state. if feedback: html_output, widget_output = self.append_feedback( feedback, html_output, widget_output, block_number, params) EventHandler.record_exploration_completed(exploration_id) else: state = State.get(dest_id, exploration) EventHandler.record_state_hit(exploration_id, state_id) if feedback: html_output, widget_output = self.append_feedback( feedback, html_output, widget_output, block_number, params) # Append text for the new state only if the new and old states # differ. if old_state.id != state.id: state_html, state_widgets = parse_content_into_html( state.content, block_number, params) # Separate text for the new state and feedback for the old state # by an additional line. if state_html and feedback: html_output += '<br>' html_output += state_html widget_output.append(state_widgets) if state.widget.widget_id in DEFAULT_ANSWERS: values['default_answer'] = DEFAULT_ANSWERS[state.widget.widget_id] values['exploration_id'] = exploration.id values['state_id'] = state.id values['oppia_html'] = html_output values['widgets'] = widget_output values['block_number'] = block_number + 1 values['params'] = params if dest_id != feconf.END_DEST: values['finished'] = False if state.widget.sticky and ( state.widget.widget_id == old_state.widget.widget_id): values['interactive_widget_html'] = '' values['sticky_interactive_widget'] = True else: values['interactive_widget_html'] = ( InteractiveWidget.get_raw_code( state.widget.widget_id, params=utils.parse_dict_with_params( state.widget.params, params) ) ) else: values['finished'] = True values['interactive_widget_html'] = '' self.response.write(json.dumps(values))
def test_save_updated_at(self): """save method changing updated_at attr to current datetime test""" s = State() prev_updated_at = s.updated_at s.save() self.assertNotEqual(prev_updated_at, s.updated_at)
def update_state(self): """ workaround until above todo is fixed :return: """ self.state = State.state_from_field(self)
def test_class_name_user(self): """test __class__ key in dictionary value is State""" s = State() state_dictionary = s.to_dict() self.assertEqual('State', state_dictionary['__class__'])
def test_heritage(self): """ check it is a subclass of BaseModel """ my_class = State() self.assertTrue(issubclass(type(my_class), BaseModel))
def test_to_dict_updated_at(self): """test 'updated_at' is in dictionary returned by to_dict method""" s = State() s_dictionary = s.to_dict() self.assertIn('updated_at', s_dictionary)
def setUpClass(cls): """set up for test""" cls.state = State() cls.state.name = "CA"
def test_name(self): """test for name""" e = State() e.name = "abc" self.assertEqual(type(State.name), str)
def test_to_dict_created_at_str(self): """test type of 'created_at' value is a str""" s = State() s_dictionary = s.to_dict() self.assertEqual(str, type(s_dictionary['created_at']))
def put(self, unused_user, exploration, state): """Saves updates to a state.""" payload = json.loads(self.request.get('payload')) yaml_file = payload.get('yaml_file') if yaml_file and feconf.ALLOW_YAML_FILE_UPLOAD: # The user has uploaded a YAML file. Process only this action. state = State.modify_using_dict( exploration, state, utils.dict_from_yaml(yaml_file)) self.response.write(json.dumps( get_state_for_frontend(state, exploration))) return state_name = payload.get('state_name') param_changes = payload.get('param_changes') interactive_widget = payload.get('interactive_widget') interactive_params = payload.get('interactive_params') interactive_rulesets = payload.get('interactive_rulesets') sticky_interactive_widget = payload.get('sticky_interactive_widget') content = payload.get('content') unresolved_answers = payload.get('unresolved_answers') if 'state_name' in payload: # Replace the state name with this one, after checking validity. if state_name == feconf.END_DEST: raise self.InvalidInputException('Invalid state name: END') exploration.rename_state(state, state_name) if 'param_changes' in payload: state.param_changes = [ ParamChange( name=param_change['name'], values=param_change['values'], obj_type='UnicodeString' ) for param_change in param_changes ] if interactive_widget: state.widget.widget_id = interactive_widget if interactive_params: state.widget.params = interactive_params if sticky_interactive_widget is not None: state.widget.sticky = sticky_interactive_widget if interactive_rulesets: ruleset = interactive_rulesets['submit'] utils.recursively_remove_key(ruleset, u'$$hashKey') state.widget.handlers = [AnswerHandlerInstance( name='submit', rules=[])] # This is part of the state. The rules should be put into it. state_ruleset = state.widget.handlers[0].rules # TODO(yanamal): Do additional calculations here to get the # parameter changes, if necessary. for rule_ind in range(len(ruleset)): rule = ruleset[rule_ind] state_rule = Rule() if 'attrs' in rule and 'classifier' in rule['attrs']: state_rule.name = rule['attrs']['classifier'] state_rule.inputs = rule.get('inputs') state_rule.dest = rule.get('dest') state_rule.feedback = rule.get('feedback') # Generate the code to be executed. if rule['rule'] == 'Default': # This is the default rule. assert rule_ind == len(ruleset) - 1 state_rule.name = 'Default' state_ruleset.append(state_rule) continue # Normalize the params here, then store them. classifier_func = state_rule.name.replace(' ', '') first_bracket = classifier_func.find('(') mutable_rule = rule['rule'] params = classifier_func[first_bracket + 1: -1].split(',') for index, param in enumerate(params): if param not in rule['inputs']: raise self.InvalidInputException( 'Parameter %s could not be replaced.' % param) typed_object = state.get_typed_object(mutable_rule, param) # TODO(sll): Make the following check more robust. if (not isinstance(rule['inputs'][param], basestring) or '{{' not in rule['inputs'][param] or '}}' not in rule['inputs'][param]): normalized_param = typed_object.normalize( rule['inputs'][param]) else: normalized_param = rule['inputs'][param] if normalized_param is None: raise self.InvalidInputException( '%s has the wrong type. Please replace it with a ' '%s.' % (rule['inputs'][param], typed_object.__name__)) state_rule.inputs[param] = normalized_param state_ruleset.append(state_rule) if content: state.content = [Content(type=item['type'], value=item['value']) for item in content] if 'unresolved_answers' in payload: state.unresolved_answers = {} for answer, count in unresolved_answers.iteritems(): if count > 0: state.unresolved_answers[answer] = count state.put() self.response.write(json.dumps( get_state_for_frontend(state, exploration)))
def test_dict_to_instance(self): """test dict -> instance""" s = State() s_dictionary = s.to_dict() s2 = State(**s_dictionary) self.assertEqual(type(s), type(s2))
def test_dict_to_id_attr_type(self): """test dict -> instance's id attr""" s = State() s_dictionary = s.to_dict() s2 = State(**s_dictionary) self.assertEqual(str, type(s2.id))
def test_dict_to_created_at_attr(self): """test dict -> instance's created_at attr""" s = State() s_dictionary = s.to_dict() s2 = State(**s_dictionary) self.assertEqual(s.created_at, s2.created_at)
def test_State_attributes(self): '''Test that State class contains the attribute `name`.''' new_state = State() self.assertTrue("name" in new_state.__dir__())
def test_all_states_without_terminal(self): states = State.all_states_without_terminal(self.field) self.assertEqual(len(states), 1685040)
def test_State_attributes(self): ''' Test that State class contains the attribute `name`. ''' new_state = State() self.assertTrue("name" in new_state.__dir__())
def test_State_attributes_type(self): '''Test that State class attribute name is class type str.''' new_state = State() name = getattr(new_state, "name") self.assertIsInstance(name, str)