def res_random(inst, res): """Randomly choose one of the sub-results to execute. The "chance" value defines the percentage chance for any result to be chosen. "weights" defines the weighting for each result. Wrap a set of results in a "group" property block to treat them as a single result to be executed in order. """ # Note: 'global' results like "Has" won't delete themselves! # Instead they're replaced by 'dummy' results that don't execute. # Otherwise the chances would be messed up. seed, chance, weight, results = res.value random.seed("random_case_{}:{}_{}_{}".format(seed, inst["targetname", ""], inst["origin"], inst["angles"])) if random.randrange(100) > chance: return ind = random.choice(weight) choice = results[ind] # type: Property if choice.name == "group": for sub_res in choice.value: should_del = Condition.test_result(inst, sub_res) if should_del is RES_EXHAUSTED: # This Result doesn't do anything! sub_res.name = "nop" sub_res.value = None else: should_del = Condition.test_result(inst, choice) if should_del is RES_EXHAUSTED: choice.name = "nop" choice.value = None
def do_fast_estimation(self, custom_cond, common_cond_nz): nz_sides = [cond.get_left_side() for cond in common_cond_nz] count_simple = 0 for trans in self.__transitions: tr_left = trans.get_left_side() tr_right = trans.get_right_side() # print "Let see on %s" % str(trans) # print "List of non zero sides {\n\t%s\n}" % "\n\t".join(map(str, nz_sides)) if tr_left in nz_sides and tr_right not in nz_sides: custom_cond.append_condition( Condition.create_non_zero_condition(tr_right.copy())) nz_sides.append(tr_right) if tr_right.has_only_one_unknown(): trans.set_simple() count_simple += 1 elif tr_left not in nz_sides and tr_right in nz_sides: custom_cond.append_condition( Condition.create_non_zero_condition(tr_left.copy())) nz_sides.append(tr_left) if tr_left.has_only_one_unknown(): trans.set_simple() count_simple += 1 if not tr_left.contains_unknown() and ( not tr_right.contains_unknown()): trans.set_simple() count_simple += 1 return count_simple == len(self.__transitions)
def res_random_setup(res: Property): weight = '' results = [] chance = 100 seed = '' for prop in res: if prop.name == 'chance': # Allow ending with '%' sign chance = srctools.conv_int( prop.value.rstrip('%'), chance, ) elif prop.name == 'weights': weight = prop.value elif prop.name == 'seed': seed = prop.value else: results.append(prop) if not results: return None # Invalid! weight = conditions.weighted_random(len(results), weight) # We also need to execute result setups on all child properties! for prop in results[:]: if prop.name == 'group': for sub_prop in prop.value[:]: Condition.setup_result(prop.value, sub_prop) else: Condition.setup_result(results, prop) return seed, chance, weight, results
def res_random_setup(res): weight = '' results = [] chance = 100 seed = '' for prop in res: if prop.name == 'chance': # Allow ending with '%' sign chance = utils.conv_int( prop.value.rstrip('%'), chance, ) elif prop.name == 'weights': weight = prop.value elif prop.name == 'seed': seed = prop.value else: results.append(prop) if not results: return None # Invalid! weight = conditions.weighted_random(len(results), weight) # We also need to execute result setups on all child properties! for prop in results[:]: if prop.name == 'group': for sub_prop in prop.value[:]: Condition.setup_result(prop.value, sub_prop) else: Condition.setup_result(results, prop) return seed, chance, weight, results
def make_condition(self): non_zero_side = None if self.__left.is_empty() and not self.__right.is_empty(): non_zero_side = self.__right elif not self.__left.is_empty() and self.__right.is_empty(): non_zero_side = self.__left else: raise Exception("One side must be not empty in transition") return Condition.create_zero_condition(non_zero_side)
def res_random(inst: Entity, res: Property): """Randomly choose one of the sub-results to execute. The "chance" value defines the percentage chance for any result to be chosen. "weights" defines the weighting for each result. Wrap a set of results in a "group" property block to treat them as a single result to be executed in order. """ # Note: 'global' results like "Has" won't delete themselves! # Instead they're replaced by 'dummy' results that don't execute. # Otherwise the chances would be messed up. seed, chance, weight, results = res.value random.seed('random_case_{}:{}_{}_{}'.format( seed, inst['targetname', ''], inst['origin'], inst['angles'], )) if random.randrange(100) > chance: return ind = random.choice(weight) choice = results[ind] # type: Property if choice.name == 'group': for sub_res in choice.value: should_del = Condition.test_result( inst, sub_res, ) if should_del is RES_EXHAUSTED: # This Result doesn't do anything! sub_res.name = 'nop' sub_res.value = None else: should_del = Condition.test_result( inst, choice, ) if should_del is RES_EXHAUSTED: choice.name = 'nop' choice.value = None
def res_random(inst: Entity, res: Property) -> None: """Randomly choose one of the sub-results to execute. The `chance` value defines the percentage chance for any result to be chosen. `weights` defines the weighting for each result. Both are comma-separated, matching up with the results following. Wrap a set of results in a `group` property block to treat them as a single result to be executed in order. """ # Note: 'global' results like "Has" won't delete themselves! # Instead they're replaced by 'dummy' results that don't execute. # Otherwise the chances would be messed up. seed, chance, weight, results = res.value # type: str, float, List[int], List[Property] set_random_seed(inst, seed) if random.randrange(100) > chance: return ind = random.choice(weight) choice = results[ind] if choice.name == 'nop': pass elif choice.name == 'group': for sub_res in choice: should_del = Condition.test_result( inst, sub_res, ) if should_del is RES_EXHAUSTED: # This Result doesn't do anything! sub_res.name = 'nop' sub_res.value = None else: should_del = Condition.test_result( inst, choice, ) if should_del is RES_EXHAUSTED: choice.name = 'nop' choice.value = None
def pull_data(self, dirs, fs): """ Parses the folders for each participant and generates the corresponding Condition objects. :param dirs: string, A list of directories relevant to this participant :param fs: string, may be provided to specify a different folder structure. (Environment is assumed to the be the 1st condition, separated by underscores) """ # Find the folder structure levels for interface and conditions ui_level = fs.split("/").index("Interface") cond_level = fs.split("/").index("Conditions") # Initialize a temporary data structure for condition info conditions = {} # Loop through each directory for this participant and parse out the conditions for _dir in dirs: path = _dir[len("../Data")+1:] cond = (path.split("\\")[ui_level], path.split("\\")[cond_level]) # If it's the first time seeing this condition, add it if cond not in conditions: conditions[cond] = [] # Add the current directory to the list for this Condition conditions[cond].append(_dir) # Once we have all conditions for the participant, simply create # each object, populate it, and add it to the participant for c in sorted(conditions.keys()): ui = c[0] env = c[1].split("_")[0] conds = [] if "_" not in c[1] else c[1].split("_")[1] condition = Condition(ui, env, conds) condition.pull_data(conditions[c], fs) self.add_condition(condition)
def res_random(inst: Entity, res: Property) -> None: """Randomly choose one of the sub-results to execute. The "chance" value defines the percentage chance for any result to be chosen. "weights" defines the weighting for each result. Wrap a set of results in a "group" property block to treat them as a single result to be executed in order. """ # Note: 'global' results like "Has" won't delete themselves! # Instead they're replaced by 'dummy' results that don't execute. # Otherwise the chances would be messed up. seed, chance, weight, results = res.value # type: str, float, List[int], List[Property] set_random_seed(inst, seed) if random.randrange(100) > chance: return ind = random.choice(weight) choice = results[ind] if choice.name == 'nop': pass elif choice.name == 'group': for sub_res in choice: should_del = Condition.test_result( inst, sub_res, ) if should_del is RES_EXHAUSTED: # This Result doesn't do anything! sub_res.name = 'nop' sub_res.value = None else: should_del = Condition.test_result( inst, choice, ) if should_del is RES_EXHAUSTED: choice.name = 'nop' choice.value = None
def res_cust_output_setup(res: Property): conds = [ Condition.parse(sub_res) for sub_res in res if sub_res.name == 'targcondition' ] outputs = list(res.find_all('addOut')) dec_con_count = srctools.conv_bool(res["decConCount", '0'], False) sign_type = IND_PANEL_TYPES.get(res['sign_type', None], None) if sign_type is None: sign_act = sign_deact = (None, '') else: # The outputs which trigger the sign. sign_act = Output.parse_name(res['sign_activate', '']) sign_deact = Output.parse_name(res['sign_deactivate', '']) return outputs, dec_con_count, conds, sign_type, sign_act, sign_deact
def res_cust_output_setup(res): conds = [ Condition.parse(sub_res) for sub_res in res if sub_res.name == 'targcondition' ] outputs = list(res.find_all('addOut')) dec_con_count = utils.conv_bool(res["decConCount", '0'], False) sign_type = IND_PANEL_TYPES.get(res['sign_type', None], None) if sign_type is None: sign_act = sign_deact = (None, '') else: # The outputs which trigger the sign. sign_act = VLib.Output.parse_name(res['sign_activate', '']) sign_deact = VLib.Output.parse_name(res['sign_deactivate', '']) return outputs, dec_con_count, conds, sign_type, sign_act, sign_deact
def setUp(self): self.condition = Condition((0, 0), (0, 0))
a3 = Variable(TypeVariable.INPUT) g1 = Variable(TypeVariable.OUTPUT) g2 = Variable(TypeVariable.OUTPUT) g3 = Variable(TypeVariable.OUTPUT) # """ test of contradictions """ # test = Condition(Side(a), Side(), StateConditions.IS_ZERO) # test2 = Condition(Side(a), Side(), StateConditions.IS_NOT_ZERO) # assert test.is_contradiction(test2) # test = Condition(Side(a, a2, g3), Side(), StateConditions.IS_ZERO) # test2 = Condition(Side(a, a2, g3), Side(), StateConditions.IS_NOT_ZERO) # assert test.is_contradiction(test2) """ test of useless """ test = Condition(Side(), Side(), StateConditions.IS_ZERO) assert test.is_useless() test = Condition(Side(a, g3), Side(g3, a), StateConditions.IS_EQUAL) assert test.is_useless() test = Condition(Side(a, g3), Side(a, g3), StateConditions.IS_EQUAL) assert test.is_useless() test = Condition(Side(a, g3), Side(), StateConditions.IS_ZERO) assert test.is_useless() == False """ test of normalise """ test = Condition(Side(a, g3), Side(), StateConditions.IS_EQUAL) test2 = Condition(Side(g3), Side(a), StateConditions.IS_EQUAL) test.normalise() assert test == test2
def estimate(system, custom_cond, common_in, common_out, comcon_nz, res_list, call_as_fork): count_triviality = 0 count_with_unknowns = 0 print "#"*50 + "start estimation" + "#"*50 print "This call of function is %s FORK" % ("" if call_as_fork else "NOT") print "Start estimate function with " print "system \n%s" % str(system) print "and custom_conditions %s\n" % str(custom_cond) transitions = system.get_transitions() for x in xrange(len(transitions) - 1, -1, -1): if custom_cond.exist_contradiction(comcon_nz): print "ESTIMATE: CONDITIONS HAVE CONTADICTIONS" print "custom_conditions %s\n" % str(custom_cond) print "common_in ", str(common_in) print "common_out ", str(common_out) SystemTransition.__write_result( system, custom_cond, common_in, common_out, count_triviality, count_with_unknowns, res_list, call_as_fork, True) return None transition = transitions[x] print "\nAnalyse transition ", str(transition) left = transition.get_left_side() right = transition.get_right_side() assert len(left) > 0 and len(right) > 0 # 2 sides does not have unknowns if not left.contains_unknown() and not right.contains_unknown(): count_triviality += 1 print "Transition is triviality" continue is_left_non_zero = custom_cond.is_side_non_zero(left) is_right_non_zero = custom_cond.is_side_non_zero(right) if is_left_non_zero and is_right_non_zero: print "Both sides of transition are NOT ZERO" # do nothing, just increase counter count_with_unknowns += 1 continue elif is_left_non_zero and not is_right_non_zero: print "Left side '%s' is NON ZERO. Rigth is undefined" % str(left) # fixed left side: not fork nz = Condition.create_non_zero_condition(right.copy()) print "Create non zero condition " + str(nz) try: custom_cond.append_condition(nz) except ConditionExeption: print "#"*30 + "UNEXPECTED CONDITION EXEPTION" + "#"*30 SystemTransition.__write_result( system, custom_cond, common_in, common_out, count_triviality, count_with_unknowns, res_list, call_as_fork, True) return print "Updated custom condition is " + str(custom_cond) count_with_unknowns += 1 continue elif not is_left_non_zero and is_right_non_zero: print "Right side '%s' is NON ZERO. Left undefined" % str(right) # fixed right side: not fork nz = Condition.create_non_zero_condition(left.copy()) print "Create non zero condition " + str(nz) try: custom_cond.append_condition(nz) except ConditionExeption: print "#"*30 + "UNEXPECTED CONDITION EXEPTION" + "#"*30 SystemTransition.__write_result( system, custom_cond, common_in, common_out, count_triviality, count_with_unknowns, res_list, call_as_fork, True) return print "Updated custom condition is " + str(custom_cond) count_with_unknowns += 1 continue else: new_res_list = [] res_list.append(new_res_list) res_list = new_res_list fork = False # both sides not zero # check that they does not contain unkwowns if not left.contains_unknown() or not right.contains_unknown(): print 'Left or right sides does not contains UNKNOWN' # need divide in two cases: zero and not zero # zero case else: print "Left and right contains UNKNOWN and sides in undefined" print "IT IS FOOORKKK!!!!!" fork = True res_list.append("fork") print "Creating new components for zero case" new_custom_c = custom_cond.copy() left_zc = Condition.create_zero_condition(left.copy()) right_zc = Condition.create_zero_condition(right.copy()) print "New zero conditions %s and %s" % (str(left_zc), str(right_zc)) new_system = system.copy() print "New system copy \n" + str(new_system) try: new_custom_c.append_condition(left_zc) new_custom_c.append_condition(right_zc) new_system.simplify_with_custom_conditions(new_custom_c) print "system after simplify \n" + str(new_system) print "Updated custom conditions " + str(new_custom_c) if new_custom_c.exist_contradiction(comcon_nz): print "ESTIMATE: CONDITIONS HAVE CONTADICTIONS2" print "custom_conditions %s\n" % str(custom_cond) print "common_in ", str(common_in) print "common_out ", str(common_out) SystemTransition.__write_result( new_system, custom_cond, common_in, common_out, count_triviality, count_with_unknowns, res_list, call_as_fork, True) return print "Goto recursion" except ConditionExeption: print "#"*30 + "Catche ConditionExeption. System FAIL." + "#"*30 SystemTransition.__write_result( new_system, custom_cond, common_in, common_out, count_triviality, count_with_unknowns, res_list, call_as_fork, True) else: if not fork: SystemTransition.estimate( new_system, new_custom_c, common_in, common_out, comcon_nz, res_list, False) else: # fork SystemTransition.estimate( new_system, new_custom_c, common_in, common_out, comcon_nz, res_list, True) # none zero case left_nzc = Condition.create_non_zero_condition(left.copy()) right_nzc = Condition.create_non_zero_condition(right.copy()) print "New non zero conditions %s and %s" % (str(left_nzc), str(right_nzc)) try: custom_cond.append_condition(left_nzc) custom_cond.append_condition(right_nzc) print "Updated custom conditions " + str(custom_cond) count_with_unknowns += 1 except ConditionExeption: print "#"*30 + "Catche ConditionExeption. System FAIL." + "#"*30 SystemTransition.__write_result( system, custom_cond, common_in, common_out, count_triviality, count_with_unknowns, res_list, call_as_fork, True) return continue SystemTransition.__write_result(system, custom_cond, common_in, common_out, count_triviality, count_with_unknowns, res_list, call_as_fork, False)
def estimate(system, custom_cond, common_in, common_out, comcon_nz, res_list, call_as_fork): count_triviality = 0 count_with_unknowns = 0 print "#" * 50 + "start estimation" + "#" * 50 print "This call of function is %s FORK" % ("" if call_as_fork else "NOT") print "Start estimate function with " print "system \n%s" % str(system) print "and custom_conditions %s\n" % str(custom_cond) transitions = system.get_transitions() for x in xrange(len(transitions) - 1, -1, -1): if custom_cond.exist_contradiction(comcon_nz): print "ESTIMATE: CONDITIONS HAVE CONTADICTIONS" print "custom_conditions %s\n" % str(custom_cond) print "common_in ", str(common_in) print "common_out ", str(common_out) SystemTransition.__write_result(system, custom_cond, common_in, common_out, count_triviality, count_with_unknowns, res_list, call_as_fork, True) return None transition = transitions[x] print "\nAnalyse transition ", str(transition) left = transition.get_left_side() right = transition.get_right_side() assert len(left) > 0 and len(right) > 0 # 2 sides does not have unknowns if not left.contains_unknown() and not right.contains_unknown(): count_triviality += 1 print "Transition is triviality" continue is_left_non_zero = custom_cond.is_side_non_zero(left) is_right_non_zero = custom_cond.is_side_non_zero(right) if is_left_non_zero and is_right_non_zero: print "Both sides of transition are NOT ZERO" # do nothing, just increase counter count_with_unknowns += 1 continue elif is_left_non_zero and not is_right_non_zero: print "Left side '%s' is NON ZERO. Rigth is undefined" % str( left) # fixed left side: not fork nz = Condition.create_non_zero_condition(right.copy()) print "Create non zero condition " + str(nz) try: custom_cond.append_condition(nz) except ConditionExeption: print "#" * 30 + "UNEXPECTED CONDITION EXEPTION" + "#" * 30 SystemTransition.__write_result(system, custom_cond, common_in, common_out, count_triviality, count_with_unknowns, res_list, call_as_fork, True) return print "Updated custom condition is " + str(custom_cond) count_with_unknowns += 1 continue elif not is_left_non_zero and is_right_non_zero: print "Right side '%s' is NON ZERO. Left undefined" % str( right) # fixed right side: not fork nz = Condition.create_non_zero_condition(left.copy()) print "Create non zero condition " + str(nz) try: custom_cond.append_condition(nz) except ConditionExeption: print "#" * 30 + "UNEXPECTED CONDITION EXEPTION" + "#" * 30 SystemTransition.__write_result(system, custom_cond, common_in, common_out, count_triviality, count_with_unknowns, res_list, call_as_fork, True) return print "Updated custom condition is " + str(custom_cond) count_with_unknowns += 1 continue else: new_res_list = [] res_list.append(new_res_list) res_list = new_res_list fork = False # both sides not zero # check that they does not contain unkwowns if not left.contains_unknown() or not right.contains_unknown(): print 'Left or right sides does not contains UNKNOWN' # need divide in two cases: zero and not zero # zero case else: print "Left and right contains UNKNOWN and sides in undefined" print "IT IS FOOORKKK!!!!!" fork = True res_list.append("fork") print "Creating new components for zero case" new_custom_c = custom_cond.copy() left_zc = Condition.create_zero_condition(left.copy()) right_zc = Condition.create_zero_condition(right.copy()) print "New zero conditions %s and %s" % (str(left_zc), str(right_zc)) new_system = system.copy() print "New system copy \n" + str(new_system) try: new_custom_c.append_condition(left_zc) new_custom_c.append_condition(right_zc) new_system.simplify_with_custom_conditions(new_custom_c) print "system after simplify \n" + str(new_system) print "Updated custom conditions " + str(new_custom_c) if new_custom_c.exist_contradiction(comcon_nz): print "ESTIMATE: CONDITIONS HAVE CONTADICTIONS2" print "custom_conditions %s\n" % str(custom_cond) print "common_in ", str(common_in) print "common_out ", str(common_out) SystemTransition.__write_result( new_system, custom_cond, common_in, common_out, count_triviality, count_with_unknowns, res_list, call_as_fork, True) return print "Goto recursion" except ConditionExeption: print "#" * 30 + "Catche ConditionExeption. System FAIL." + "#" * 30 SystemTransition.__write_result(new_system, custom_cond, common_in, common_out, count_triviality, count_with_unknowns, res_list, call_as_fork, True) else: if not fork: SystemTransition.estimate(new_system, new_custom_c, common_in, common_out, comcon_nz, res_list, False) else: # fork SystemTransition.estimate(new_system, new_custom_c, common_in, common_out, comcon_nz, res_list, True) # none zero case left_nzc = Condition.create_non_zero_condition(left.copy()) right_nzc = Condition.create_non_zero_condition(right.copy()) print "New non zero conditions %s and %s" % (str(left_nzc), str(right_nzc)) try: custom_cond.append_condition(left_nzc) custom_cond.append_condition(right_nzc) print "Updated custom conditions " + str(custom_cond) count_with_unknowns += 1 except ConditionExeption: print "#" * 30 + "Catche ConditionExeption. System FAIL." + "#" * 30 SystemTransition.__write_result(system, custom_cond, common_in, common_out, count_triviality, count_with_unknowns, res_list, call_as_fork, True) return continue SystemTransition.__write_result(system, custom_cond, common_in, common_out, count_triviality, count_with_unknowns, res_list, call_as_fork, False)
def condition(self): if not self._condition: self._condition = Condition() return self._condition
g1 = Variable(TypeVariable.OUTPUT) g2 = Variable(TypeVariable.OUTPUT) g3 = Variable(TypeVariable.OUTPUT) # """ test of contradictions """ # test = Condition(Side(a), Side(), StateConditions.IS_ZERO) # test2 = Condition(Side(a), Side(), StateConditions.IS_NOT_ZERO) # assert test.is_contradiction(test2) # test = Condition(Side(a, a2, g3), Side(), StateConditions.IS_ZERO) # test2 = Condition(Side(a, a2, g3), Side(), StateConditions.IS_NOT_ZERO) # assert test.is_contradiction(test2) """ test of useless """ test = Condition(Side(), Side(), StateConditions.IS_ZERO) assert test.is_useless() test = Condition(Side(a, g3), Side(g3, a), StateConditions.IS_EQUAL) assert test.is_useless() test = Condition(Side(a, g3), Side(a, g3), StateConditions.IS_EQUAL) assert test.is_useless() test = Condition(Side(a, g3), Side(), StateConditions.IS_ZERO) assert test.is_useless() == False """ test of normalise """ test = Condition(Side(a, g3), Side(), StateConditions.IS_EQUAL) test2 = Condition(Side(g3), Side(a), StateConditions.IS_EQUAL) test.normalise()