def main(): ''' tree_file = sys.argv[1] test_file = sys.argv[2] ''' tree_file = "out" test_file = "mnist_test.csv" file = open(tree_file, 'rb') tree = pickle.load(file) print_conds(tree) #DELETE!!! tests_img = ReadCsvFiles.FileToSet(test_file) bound_pics = [] sharp_pics = [] pic_id = 0 for pic in tests_img: print(pic_id) pic.append(pic_id) sharped = Condition.SharpImage(pic) sharp_pics.append(sharped) bound_pics.append(Condition.CreateBounderiesImage(sharped)) pic_id += 1 good = 0 for img in tests_img: to_print = BinTree.getLabelByTree(tree, img, [sharp_pics, bound_pics]) print(to_print) if img[0] == to_print: good += 1 print(good / len(tests_img) * 100) #DELETE! ONLY FOR PRIVATE USE! NOT FOR SUBMISSION
def make_postcondition(self, node, story): #If we have a modification, we should apply it modification = node.get_modification() if not modification == None: for mod in modification: #If our modification is actually a function of the social node if mod.keys()[0] == "Method": method = mod["Method"] #Replace any references with the node they are referencing if isinstance(method["self"], dict): method_self = story.get_cast()[method["self"]["ref"]] else: method_self = method["self"] #We will make an argument list with our arguments, fixing any #references as we go arg_list = [] for arg in method["args"]: if isinstance(arg, dict): arg_list.append(story.get_cast()[arg["ref"]]) else: arg_list.append(arg) method_self.set_condition_only(True) #Get the modified edges from the method new_method = method_self.get_function_from_name(str(method["name"])) pre_conditions, new_conditions, lost_conditions = new_method(method_self, arg_list) method_self.set_condition_only(False) node.add_preconditions(pre_conditions) node.add_postconditions(new_conditions) node.add_lostconditions(lost_conditions) #If our modification is an attribute switch elif mod.keys()[0] == "Attribute": attribute = mod["Attribute"] #Replace the name, if it is a reference if isinstance(attribute["name"], dict): attr_name = story.get_cast()[attribute["name"]["ref"]] else: attr_name = attribute["name"] condition = Condition(True) #Add the condition as a postcondition to our narrative condition.set_first_object(attr_name) condition.set_key(attribute["key"]) condition.set_value(attribute["value"]) node.add_postcondition(condition) else: print "Modification Error"
def _patch_cond(f, keys, prev_cond): if f.type == FragmentType.ElseStatement: cond = str.format("!({})", prev_cond) else: cond = ConditionAccessor.get_condition(f) patched_cond = Condition.simplify(cond, keys) if patched_cond is not cond: ConditionAccessor.set_condition(f, patched_cond) return patched_cond
def parseConditions(self, root): """ return an array of Condition objects for example, from <condition alias='C30'> <variable name='gamma irradiation' value='false'/> <variable name='time' value='30' units='minutes'/> </condition> <condition alias='G0'> <variable name='gamma irradiation' value='true'/> <variable name='time' value='0' units='minutes'/> </condition> string representation of the two resulting condition objects: [note that no units ('None') are specified for irradiation] condition: C30, gamma irradiation: false, None, time: 30, minutes condition: G0 gamma irradiation: true, None time: 0, minutes """ conditionElements = root.getChildren("condition") result = [] # use an array so that conditions can be stored in the # order in which they appear in the file, which may # reflect an order the user carefully created for c in conditionElements: alias = c.getAttribute("alias").value assert alias condition = Condition(alias) for v in c.getChildren("variable"): name = v.getAttribute("name").value value = v.getAttribute("value").value unitsAttribute = v.getAttribute("units") units = None if unitsAttribute: units = unitsAttribute.value condition.addVariable(Variable(name, value, units)) result.append(condition) return result
def unserialize(cls, step_data): kwargs = { "conditions": [Condition.unserialize(cond) for cond in step_data.get("conditions", ())], "actions": [Action.unserialize(action) for action in step_data.get("actions", ())], } if "next" in step_data: kwargs["next"] = StepNext(step_data["next"]) if "cond_op" in step_data: kwargs["cond_op"] = StepConditionOperator(step_data["cond_op"]) if "enabled" in step_data: kwargs["enabled"] = bool(step_data["enabled"]) return cls(**kwargs)
def get_context_data(self, **kwargs): context = super(EditScriptContentView, self).get_context_data(**kwargs) context["title"] = get_create_or_change_title(self.request, self.object) context["action_infos"] = Action.get_ui_info_map() context["condition_infos"] = Condition.get_ui_info_map() context["cond_op_names"] = get_enum_choices_dict(StepConditionOperator) context["step_next_names"] = get_enum_choices_dict(StepNext) context["toolbar"] = Toolbar([ JavaScriptActionButton( text=_("Save"), icon="fa fa-save", extra_css_class="btn-success", onclick="window.ScriptEditor.save();return false" ), get_discard_button(get_model_url(self.object, "edit")) ], view=self) return context
# 프로그램 설계 # 1. 전수검색 후 조건에 맞추어 삭제 # -> 이때 데이터 저장과 저장형태, 속도에 대한 걸림돌 발생/ # -> 다만 가장 쉽게 개발가능 # # 2. 조건에 맞추어서 숫자를 넣기 # -> 조건에 대한 자세한 이해와 다양한 경험필요 # -> 다만 가장 쉽게 자료형 설정가능 print("a") #알고리즘 선택하기 wayToUse = 0 #조건생성 # wayToUse = 0 # 전수조사 # wayToUse = 1 #실행부 #만약 조건생성일경우 if wayToUse == 0: #필요한 정보(이전날 근무, 영외활동자 리스트) 세팅 Condition.setting() #알고리즘 실행 Condition.algorithm()
def generate_preconditions(self): #Generate the attribute conditions first for node in self._nodes: for key in node.get_attributes(): condition = Condition(True) condition.set_first_object(node) condition.set_key(key) condition.set_value(node.get_attributes()[key]) self._preconditions.append(condition) #Following this we can generate the edge conditions for edge in self._edges: condition = Condition(False) condition.set_first_object(edge.get_from_node()) condition.set_second_object(edge.get_to_node()) condition.set_key(edge.get_name().keys()[0]) condition.set_value(edge.get_name()[edge.get_name().keys()[0]]) self._preconditions.append(condition)
def test_equals_false(self): condition = "a == false" res = Condition.simplify(condition, {"a": True}) self.assertEqual(False, res)
def main(): ''' version = sys.argv[1] p = sys.argv[2] l = sys.argv[3] train_set_file_name = sys.argv[4] output_tree_file_name = sys.argv[5] ''' ''' print("Enter Version") version = int(input()) print("Enter p value") p = int(input()) l = int(input("Enter l value")) train_set_file_name = input("Enter train set file") output_tree_file_name = input("Enter output file") ''' start_time = time.time() bound_pics = [] version = 2 p = 20 l = 5 train_set_file_name = "mnist_train.csv" output_tree_file_name = "out" pictures = ReadCsvFiles.FileToSet(train_set_file_name) cond_nums = [1, 2, 3, 5, 6] # # NOT FOR SUBMISSION # random.shuffle(pictures) # pictures = pictures[:5000] # ---- in case condition 5 is been used, we will build the bounded pics and set in global array ---- if (5 in cond_nums or 6 in cond_nums): print("Creating bounderies and sharped images...") print("") sharp_pics = [] bound_pics1 = [] pic_id = 0 for pic in pictures: print(pic_id) pic.append(pic_id) sharped = Condition.SharpImage(pic) sharp_pics.append(sharped) bound_pics1.append(Condition.CreateBounderiesImage(sharped)) pic_id += 1 bound_pics = [sharp_pics, bound_pics1] random.shuffle(pictures) valid_set_size = int(p / 100 * len(pictures)) valid_set = pictures[0:valid_set_size] train_set = pictures[valid_set_size:] conds = [] if (version == 1): conds = Condition.BuildVersion1ConditionArray() else: conds1 = Condition.BuildVersion1ConditionArray() conds2 = Condition.BuildType2ConditionArray() conds3 = Condition.BuildType3ConditionArray() conds5 = Condition.BuildType5ConditionArray() conds6 = Condition.BuildType6ConditionArray() conds = conds1 + conds2 + conds3 + conds5 + conds6 # conds = conds1 + conds2 + conds3 t_trees = [] id_leaf = 1 # ---- Build first leaf ---- max_digit = BinTree.common_label(train_set) tree = BinTree.BinTree(None, max_digit, id_leaf) id_leaf += 1 tree.examples = train_set # ---- End build first leaf ---- last_t = 0 leaves = [tree] max_l = -1 max_value = 0 for i in range(l + 1): res_build = BuildDecisionTree(int(math.pow(2, i)), train_set, conds, bound_pics, tree, leaves, id_leaf, last_t) t_trees.append(res_build[0]) tree = res_build[0] leaves = res_build[1] id_leaf = res_build[2] last_t = int(math.pow(2, i)) succeeded = 0 for img in valid_set: predicted_label = BinTree.getLabelByTree(tree, img, bound_pics) if (predicted_label == img[0]): succeeded += 1 if (max_value < succeeded): max_value = succeeded max_l = i # Find tree with optimal L value # max_l = -1 # max_value = 0 # for i in range(len(t_trees)): # succeeded = 0 # for img in valid_set: # predicted_label = BinTree.getLabelByTree(t_trees[i],img, bound_pics) # if(predicted_label == img[0]): # succeeded += 1 # if (max_value < succeeded): # max_value = succeeded # max_l = i # ---- Build Final Tree ---- id_leaf = 1 # ---- Build first leaf ---- max_digit = BinTree.common_label(train_set) tree = BinTree.BinTree(None, max_digit, id_leaf) id_leaf += 1 tree.examples = train_set # ---- End build first leaf ---- last_t = 0 leaves = [tree] final_t = int(math.pow(2, max_l)) res_final = BuildDecisionTree(final_t, pictures, conds, bound_pics, tree, leaves, id_leaf, last_t) final_tree = res_final[0] # ---- Finish Build Final Tree ---- filehandler = open(output_tree_file_name, 'wb') pickle.dump(final_tree, filehandler) print("num: " + str(len(train_set))) errors = 0 for img in pictures: predicted_label = BinTree.getLabelByTree(final_tree, img, bound_pics) if (predicted_label != img[0]): errors += 1 print("error: " + str(errors)) print("size: " + str(final_t)) print("TIME: " + str(time.time() - start_time)) #NOT FOR SUBMISSION!!!
def test_false(self): condition = "false" res = Condition.simplify(condition, {}) self.assertEqual(False, res)
def test_ignore_case(self): condition = "(a && A) && aA" res = Condition.simplify(condition, {"a": True}) self.assertEqual('aA', res)
def _test_equals_in_brackets(self): condition = "(a == true)" res = Condition.simplify(condition, {"a": True}) self.assertEqual(True, res)
def test_1_simple_or_true(self): condition = "a || b" res = Condition.simplify(condition, {"a": True}) self.assertEqual(True, res)
def test_1_simple_or_false(self): condition = "a || b" res = Condition.simplify(condition, {"a": False}) self.assertEqual("b", res)
def test_1_simple_and_false(self): condition = "(a && b)" res = Condition.simplify(condition, {"a": False}) self.assertEqual(False, res)
def test_no_keys_no_simplification(self): condition = "!(aB || b) && (c)|| ! Ab" res = Condition.simplify(condition, {}) self.assertEqual("!(aB || b) && (c)|| ! Ab", res)
def test_1_simple_and_true(self): condition = "(a && b)" res = Condition.simplify(condition, {"a": True}) self.assertEqual('b', res)
import Condition import MoneyManager import Signal import Stoploss import ProfitGoal import Slippage if __name__ == "__main__": suite = unittest.TestSuite() suite.addTest(Datetime.suite()) suite.addTest(Parameter.suite()) suite.addTest(MarketInfo.suite()) suite.addTest(StockTypeInfo.suite()) suite.addTest(Stock.suite()) suite.addTest(KData.suite()) suite.addTest(Indicator.suite()) suite.addTest(TradeCost.suite()) suite.addTest(Environment.suite()) suite.addTest(Condition.suite()) suite.addTest(MoneyManager.suite()) suite.addTest(Signal.suite()) suite.addTest(Stoploss.suite()) suite.addTest(ProfitGoal.suite()) suite.addTest(Slippage.suite()) unittest.TextTestRunner(verbosity=2).run(suite) #unittest.main()
if __name__ == "__main__": suite = unittest.TestSuite() suite.addTest(Datetime.suite()) suite.addTest(Parameter.suite()) suite.addTest(MarketInfo.suite()) suite.addTest(StockTypeInfo.suite()) suite.addTest(Stock.suite()) suite.addTest(KData.suite()) suite.addTest(Indicator.suite()) suite.addTest(TradeCost.suite()) suite.addTest(Environment.suite()) suite.addTest(Environment.suiteTestCrtEV()) suite.addTest(Condition.suite()) suite.addTest(Condition.suiteTestCrtCN()) suite.addTest(MoneyManager.suite()) suite.addTest(MoneyManager.suiteTestCrtMM()) suite.addTest(Signal.suite()) suite.addTest(Signal.suiteTestCrtSG()) suite.addTest(Stoploss.suite()) suite.addTest(Stoploss.suiteTestCrtST()) suite.addTest(ProfitGoal.suite()) suite.addTest(ProfitGoal.suiteTestCrtPG()) suite.addTest(Slippage.suite()) suite.addTest(Slippage.suiteTestCrtSL()) suite.addTest(AllocateFunds.suite()) unittest.TextTestRunner(verbosity=2).run(suite)
__author__ = 'sunbeansoft' import Condition as c c.condition()
def test_equals_true(self): condition = "a == true" res = Condition.simplify(condition, {"a": True}) self.assertEqual(True, res)
def test_ignore_case2(self): condition = "( a && !A) || a1 || !A2" res = Condition.simplify(condition, {"A": True}) self.assertEqual('a1 || !A2', res)
def test_true(self): condition = "true" res = Condition.simplify(condition, {}) self.assertEqual(True, res)
from Condition import * def ifTrueLogic(): print("true") def ifFalseLogic(): print("false") trueCondition = Condition(1 == 1) #trueCondition.ifTrue(lambda : print("true")).ifFalse(lambda : print("false")) trueCondition.ifTrue(ifTrueLogic).ifFalse(ifFalseLogic) falseCondition = Condition(1 == 2) #falseCondition.ifTrue(lambda : print("true")).ifFalse(lambda : print("false")) falseCondition.ifTrue(ifTrueLogic).ifFalse(ifFalseLogic)
def test_1_complex(self): condition = "!(a || b) && c || !d" res = Condition.simplify(condition, {"a": True}) self.assertEqual("!d", res)
def test_2_complex(self): condition = "((a && c) || (!b && d)) && !e" res = Condition.simplify(condition, {"a": True, "b": False}) self.assertEqual("!e && (c || d)", res)
def test_missing_key_no_simplification(self): condition = "!(aB || b) && (c)|| ! Ab" res = Condition.simplify(condition, {"d": True}) self.assertEqual("!(aB || b) && (c)|| ! Ab", res)