Beispiel #1
0
    def __init__(self):
        self.manager_params = {
            "隐藏层神经元个数": 5,
            "训练样本取样数": 6000,
            "学习速率": 0.05,
            "矫正率": 0.1
        }

        self._params_map = {
            "nh": "隐藏层神经元个数",
            "samples": "训练样本取样数",
            "learning_rate": "学习速率",
            "correct_rate": "矫正率"
        }
        self.mdl_parser = ModelConstructor()
        self.model = None
        self.expr_params = list()  # [(name, val_list)]
        self.ltl = None
        self.checker = None
        self.regressor = BPNN()
        self.test_xs = []  # [(vals)]
        self.ltl_parser = LTLParser().build_parser()
        self.predict_xs = None
        self.predict_ys = None
        self.prism_test_datas = []
Beispiel #2
0
 def setUp(self):
     filepath = get_prism_model_dir() + get_sep() + "smalltest.prism"
     self.model_constructor = ModelConstructor()
     self.model = self.model_constructor._parse(filepath)
     self.duration = 180
     self.model.duration = self.duration
     ltl = "true U<={} failure".format(self.duration)
     parsed = LTLParser().build_parser().parse_line(ltl)
     self.checker = Checker(model=self.model, ltl=parsed)
Beispiel #3
0
 def __init__(self, model_path, ltl, path_length, params,  checking_delta=0.01, prism_data_path=None):
     '''
     :param model_path:
     :param ltl:
     :param path_length:
     :param params: value of unsure parameters: map with key to be param_name, value to be value list
     :param prism_data_path:
     '''
     unittest.TestCase.__init__(self)
     # todo check path exist and is a file
     self.model_path = model_path
     self.prism_data_path = prism_data_path
     self.ltl = ltl
     self.parsed_ltl = None
     self.modelConstructor = ModelConstructor()
     self.ltl_parser = LTLParser().build_parser()
     self.duration = path_length
     self.checking_delta = checking_delta
     self.params = params
Beispiel #4
0
def dpm_regress():
    '''
    证明回归分析的结果和prism的误差和SMC和prism的误差相差不大,
    即证明回归分析可以代替SMC
    :return: null
    '''
    from PathHelper import get_prism_model_dir
    from checker.Checker import Checker
    from compiler.LTLParser import LTLParser
    from util.util import interval
    from experiment.ExperimentWrapper import ExperimentWrapper

    base_dir = get_prism_model_dir()
    model = ModelConstructor(base_dir).parse("smalltest")
    ltl = "true U<=180 failure"
    ltl_parser = LTLParser().build_parser()
    parsed_ltl = ltl_parser.parse_line(ltl)
    checker = Checker(model, parsed_ltl, duration=180)

    expe_executor = ExperimentWrapper(checker, samples_per_param=600)
    train_xs = interval(1, 10, 0.3)
Beispiel #5
0
 def setUp(self):
     self.manager = Manager()
     filepath = get_prism_model_dir() + get_sep() + "DPM.prism"
     self.manager.read_model_file(filepath)
     self.model = self.manager.model
     ltl = "true U<=10 failure"
     parsed_ltl = LTLParser().build_parser().parse_line(ltl)
     self.manager.set_manager_param_simple("duration", 10.0)
     self.checker = Checker(model=self.model, ltl=parsed_ltl, duration=10.0)
     self.manager.set_ltl(parsed_ltl)
     self.prism_x, self.prism_y = parse_csv_cols(get_prism_model_dir() +
                                                 get_sep() +
                                                 "Q_TRIGGER_1_20_1.csv")
Beispiel #6
0
 def setUp(self):
     self._constructor = ModelConstructor()
     self._ltl_parser = LTLParser()
     self.logger = logging.getLogger(__name__)
     self.logger.addHandler(sys.stdout)
     self.logger.setLevel(logging.INFO)
     if not isdir(get_prism_model_dir()):
         raise Exception("model root path not exist: {}".format(
             get_prism_model_dir()))
     self._constructor.set_base_dir(self._get_model_root_path())
     model_path = self._get_model_root_path() + get_sep(
     ) + self._get_model_name() + self.PRISM_SUFFIX
     if not isfile(model_path):
         raise Exception("Model file {} not exist".format(model_path))
     self._renaming_helper = RenamingHelper(model_path)
     self._renaming_helper.rewrite()
     rewritten_path = self._renaming_helper.export()
     filename = basename(rewritten_path)
     filename = splitext(filename)[0]
     self._model = self._constructor.parse(filename)
     self._param_map = {}
Beispiel #7
0
class ParserTest(unittest.TestCase):
    '''
    单元测试:测试parser出的模型运转良好
    '''

    def __init__(self, model_path, ltl, path_length, params,  checking_delta=0.01, prism_data_path=None):
        '''
        :param model_path:
        :param ltl:
        :param path_length:
        :param params: value of unsure parameters: map with key to be param_name, value to be value list
        :param prism_data_path:
        '''
        unittest.TestCase.__init__(self)
        # todo check path exist and is a file
        self.model_path = model_path
        self.prism_data_path = prism_data_path
        self.ltl = ltl
        self.parsed_ltl = None
        self.modelConstructor = ModelConstructor()
        self.ltl_parser = LTLParser().build_parser()
        self.duration = path_length
        self.checking_delta = checking_delta
        self.params = params

    def setUp(self):
        self.model = self.modelConstructor._parse(self.model_path)
        self.parsed_ltl = self.ltl_parser.parse_line(self.ltl)

        if self.prism_data_path:
            self.prism_data_rows = parse_csv_rows(self.prism_data_path)
        if self.prism_data_rows:
            self.prism_data_map = {}
            for row in self.prism_data_rows:
                attr = row[:-1]
                label = row[-1]
                self.prism_data_map[tuple(attr)] = label
        checker = Checker(self.model, self.parsed_ltl, duration=self.duration)
        self.experiment_wrapper = ExperimentWrapper(checker, self.params)
        self.logger = logging.getLogger(__name__)
        self.logger.addHandler(logging.StreamHandler(sys.stdout))
        self.logger.setLevel(logging.DEBUG)

    def testChecking(self):
        '''
        verify the difference between smc and provided prism_data is within checking_delta
        :return: None
        '''
        checked_result_map = {}
        for name, values in self.params.items():
            self.model.commPrepared = False
            constant_obj = Constant(name, )
            set_result = self.model.set_constant(constant_obj)
            assert set_result
            result = self.checker.run_checker()
            checked_result_map[thickness] = result
        error_map = {}
        self.logger.info("check result: %s", str(checked_result_map))
        for k, v in checked_result_map.items():
            error_map[float(k)] = fabs(v - self.prism_data_map[float(k)])
        self.logger.info("error_map: %s", str(error_map))
        self.assertGreaterEqual(0.01, sum(error_map.values()) / len(checked_result_map))

    def testGenRandomPath(self):
        '''
        验证model产生的随机路径的长度要么等于duration,要么其中包含failure状态
        :return: None
        '''
        self.model.set_constant_name_value(self.PARAM_NAME, self.thicknesses[4])
        genCnt = 0
        while True:
            genCnt += 1
            self.logger.info("genCnt = %d", genCnt)
            path = self.model.get_random_path_V2()
            ap_sets = [step.ap_set for step in path]
            ap_set = reduce(lambda set1, set2: set1.union(set2), ap_sets)
            if len(path) == self.duration:
                if "failure" not in ap_set:
                    self.assertFalse(self.checker.verify(path))
                else:
                    self.assertTrue(self.checker.verify(path))
                continue
            elif "failure" in ap_set:
                self.assertTrue(self.checker.verify(path))
            else:
                break
Beispiel #8
0
 def setUp(self):
     self.ltls = []
     self.ltls.append("true U<= 10 failure")
     self.ltls.append("true U<=10 failure")
     self.ltls.append("true U<= 10.2 failure")
     self.parser = LTLParser().build_parser()