Ejemplo n.º 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 = []
Ejemplo n.º 2
0
def test():
    # get model object
    constructor = Constructor()
    model_file_path = "../../prism_model/smalltest.prism"
    constructor._parse(model_file_path)
    model = constructor.model
    # set day's value
    days = range(1, 365*5)
    # 运用parsed function计算出的结果
    parsed_prbs = []
    # 运用模型中的数据计算出的真实的结果
    prbs = []  # [(sb_fail_prb, s3r_fail_prb)]
    for day_val in days:
        # compute the failure probability of modules
        # get the fail prob function and compute
        var_day = model.getLocalVar("day")
        var_day.set_value(day_val)
        prbs.append(fail_prob_of_model(model, constructor._parser.vfmap))

        # get the parsed function and use it to compute the failure probability
        f_sb = constructor._parser.vfmap["sb_fail_prob"]
        f_s3r = constructor._parser.vfmap["s3r_fail_prob"]
        parsed_prbs.append((f_sb(), f_s3r()))
    # compare
    assert len(prbs) == len(parsed_prbs)
    for tuple1, tuple2 in zip(prbs, parsed_prbs):
        f1, f2 = tuple1
        f3, f4 = tuple2
        precision = 1e-8
        # logger.info("f1={}, f3={}".format(f1, f3))
        # logger.info("f2={}, f4={}".format(f2, f4))
        assert fabs(f1 - f3) < precision, "day={}, f1={}, f3={}".format(day_val, f1, f3)
        assert fabs(f2 - f4) < precision, "day={}, f2={}, f4={}".format(day_val, f2, f4)
Ejemplo n.º 3
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)
Ejemplo n.º 4
0
class Manager(object):
    def __init__(self):
        self.mdl_parser = ModelConstructor()
        self.model = None

    def input_file(self, file_path):
        self.model = self.mdl_parser.parseModelFile(file_path)
Ejemplo n.º 5
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
Ejemplo n.º 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 = {}
Ejemplo n.º 7
0
class ModelFactory(object):
    module_factory = ModuleFactory(SPSConfig())
    model_constructor = ModelConstructor()

    @classmethod
    def get_built(cls):
        def failurecondition(vs, cs):
            sb_status = vs['sb_status'].get_value()
            s3r_status = vs['s3r_status'].get_value()
            bcr_status = vs['bcr_status'].get_value()
            bdr_status = vs['bdr_status'].get_value()
            return (sb_status + s3r_status + bcr_status + bdr_status) < 4

        labels = {}
        labels['failure'] = failurecondition

        model = ModulesFile(ModelType.DTMC,
                            failureCondition=failurecondition,
                            stopCondition=failurecondition,
                            modules=[
                                cls.module_factory.timermodule(),
                                cls.module_factory.s3rmodule(),
                                cls.module_factory.sbmodule(),
                                cls.module_factory.bcrmodule(),
                                cls.module_factory.bdrmodule()
                            ],
                            labels=labels)
        model._constants[
            'SCREEN_THICKNESS'] = cls.module_factory.config.getParam(
                'SCREEN_THICKNESS')
        model.duration = 730
        return model

    @classmethod
    def setParam(cls, name, value):
        if name not in cls.module_factory.config.params.keys():
            return
        cls.module_factory.config.setParam(name, value)

    @classmethod
    def get_parsed(cls, duration=730):
        sep = get_sep()
        mdl_dir = sep.join((get_proj_dir(), 'prism_model', 'smalltest.prism'))
        model = cls.model_constructor._parse(mdl_dir)
        model.duration = duration
        return model
Ejemplo n.º 8
0
class DTMCSyncUnittest(unittest.TestCase):

    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)

    def test_parsing(self):
        self.assertEqual(self.model.model_type, ModelType.DTMC)

    def test_gen_path(self):
        # 测试生成路径的正确性
        # 生成的路径要么总长为duration,要么出现failure
        for _ in range(5000):
            path = self.model.get_random_path_V2()
            for step in path:
                logger.info(step)
            logger.info("----------------")
            if {"failure"} in [step.ap_set for step in path]:
                continue
            passed_time = path[-1].next_move.passed_time + \
                path[-1].next_move.holding_time
            if int(passed_time) < self.duration:
                for step in path:
                    logger.error(step)
                logger.error("-------------")

    def test_checking(self):
        thickness_vals = range(1, 10, 1)
        constant_objs = [Constant("SCREEN_THICKNESS", v)
                         for v in thickness_vals]
        for obj in constant_objs:
            self.model_constructor._parser.vcf_map[obj.get_name()].set_value(
                obj.get_value())
            self.model.commPrepared = False
            logger.info(
                "param={}, checker's result={}".format(
                    obj.get_value(),
                    self.checker.run_checker()))
Ejemplo n.º 9
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)
Ejemplo n.º 10
0
 def get_parsed(self):
     constructor = ModelConstructor()
     model = constructor.parseModelFile("../../prism_model/smalltest.prism")
     return model
Ejemplo n.º 11
0
 def get_parsed():
     sep = get_sep()
     mdl_dir = sep.join((get_proj_dir(), 'prism_model', 'smalltest.prism'))
     return ModelConstructor().parseModelFile(mdl_dir)
Ejemplo n.º 12
0
from compiler.PRISMParser import ModelConstructor

constructor = ModelConstructor()
mdl_fl_path = "../../prism_model/smalltest.prism"
model = constructor._parse(mdl_fl_path)
assert model.labels["failure"]() is False
model.localVars["sb_status"].set_value(0)
assert model.labels["failure"]() is True
Ejemplo n.º 13
0
class ModelTestBase(unittest.TestCase):

    PRISM_SUFFIX = ".prism"

    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 = {}

    def _get_model_name(self):
        '''
        return the name of model file(extension not included)
        supposed to be implemented by subclass
        :return: string
        '''
        pass

    def _get_model_root_path(self):
        '''
        could be overrided if necessary
        :return:
        '''
        return get_prism_model_dir()

    def _set_parameters(self, name, values):
        '''
        store parameters in this object
        :param name: parameter name
        :param values: list of value
        :return:
        '''
        self._param_map[name] = values

    def _set_parameter(self, k, v):
        '''
        设置参数到_checker._constants
        :param k: parameter name
        :param v: parameter value
        :return:
        '''
        self._model.set_constant(k, v)

    def get_model(self):
        return self._model
Ejemplo n.º 14
0
def buildCommand():
    constructor = Constructor()
    model = constructor._parse("../../prism_model/CommandTest.prism")
    return model.modules.values()[0].commands.values()[0]
Ejemplo n.º 15
0
class Manager(object):
    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 = []

    def set_manager_param(self, name, param):
        self.manager_params[name] = param

    def set_manager_param_simple(self, name, value):
        if name not in self._params_map.keys():
            # todo system-level logger
            pass
        self.manager_params[self._params_map[name]] = value

    def get_manager_param(self, name):
        if name not in self._params_map.keys():
            print "key not exist when get manager parameter: {}".format(name)
        return self.manager_params[self._params_map[name]]

    def set_random_path_duration(self, duration):
        self.set_manager_param("duration", duration)
        self.model.duration = duration

    def read_model_file(self, file_path):
        self.model = self.mdl_parser._parse(file_path)
        self.checker = Checker(model=self.model)

    def _set_constants(self, *constants):
        '''constants: ([constant_obj])'''
        while len(self.expr_params):
            self.expr_params.pop(0)
        self.expr_params.extend(constants)

    def set_train_constants(self, *constants):
        '''设置训练时需要的参数
        constant: (name, val_list)
        '''
        self.expr_params = constants

    def set_ltl(self, ltl):
        if self.checker:
            self.checker.ltl = ltl
            return ltl
        else:
            return

    def set_model_duration(self, str_duration):
        '''duration: str_valued duration'''
        if self.model.model_type == ModelType.DTMC:
            self.model.duration = int(str_duration)
        else:
            # CTMC case
            self.model.duration = float(str_duration)

    def _set_param(self, *constants):
        '''
        将参数设置到ModulesFile中
        :param constants: [constant_obj]
        :return: None
        '''
        model = self.checker.model
        if model:
            for constant_obj in constants:
                model.set_constant(constant_obj)

    def _clear_param(self, *constants):
        for constant_obj in constants:
            self.mdl_parser._parser.vcf_map[constant_obj.get_name()].set_value(
                None)

    def set_test_xs(self, test_xs):
        '''
        设置回归分析时参数的值
        :param test_xs: [(param1_value, param2_val, ...)]
        :return: None
        '''
        self.test_xs = test_xs

    def _to_constant_objs(self):
        '''
        将self.expr_params转化为[[constant_obj]]
        :return: [[constant_obj]]
        '''
        result = []
        for n, vl in self.expr_params:
            objs = []
            for v in vl:
                objs.append(Constant(n, v))
            result.append(objs)
        return result

    def train_network(self):
        '''给定参数,先在checker中跑,得到训练参数上的ltl公式验证结果(train_y),然后用这些数据对神经网络进行训练'''
        train_data_x = []
        train_data_y = []
        constant_objs = self._to_constant_objs()
        self.checker.samples = self.get_manager_param("samples")
        for constant_list in itertools.product(*constant_objs):
            try:
                self._set_param(*constant_list)
                self.model.prepare()
                train_y = self.checker.run_checker()
                train_x = [c_obj.get_value() for c_obj in constant_list]
                train_data_x.append(train_x)
                train_data_y.append(train_y)
            finally:
                self._clear_param(*constant_list)

        self.regressor.setup(len(self.expr_params),
                             int(self.get_manager_param("nh")), 1,
                             self.get_manager_param("learning_rate"),
                             self.get_manager_param("correct_rate"))

        print train_data_x
        print train_data_y
        self.regressor.train(train_data_x, train_data_y)

        # dump the network
        network_obj = self.regressor
        dump_file = "nn.txt"
        try:
            f = open(dump_file, "wb")
            pickle.dump(network_obj, f)
            f.close()
        except IOError:
            print "IOError when dumping network object. Please check your access permission"

    def compute_error(self, err_func, values1, values2):
        # param of red_func: val1, val2)计算val1和val2之间的误差,并将所有的误差相加
        errors = sum(map(err_func, [t for t in zip(values1, values2)]))
        return errors

    def run_test(self):
        '''对给定测试参数运行神经网络进行预测'''
        # try loading dumped network

        if os.path.exists("nn.txt") and os.access("nn.txt", os.R_OK):
            f = open("nn.txt", "rb")
            network_obj = pickle.load(f)
            if network_obj:
                self.regressor = network_obj
            f.close()

        test_xs = self.test_xs  # [(vals)]
        test_expr_ys = []
        for test_x in test_xs:
            results = self.regressor.predict(list(test_x))
            # results is of length 1
            test_expr_ys.append(results[0])
        print "Predict results: "
        for (x, y) in zip(test_xs, test_expr_ys):
            print "x={}, predict={}".format(x, y)
        return test_expr_ys

    def unsure_param_names(self):
        return self.mdl_parser._parser.unsure_parameters()

    def plot_expr_datas(self, expr_xs, expr_ys, true_ys=None):
        if true_ys:
            plot_multi((expr_xs, expr_ys, "predict"),
                       (expr_xs, true_ys, "prism"))
        else:
            param = (expr_xs, expr_ys, "predict")
            plot_multi(param)

    def save_predict_results(self, xs, ys):
        assert len(ys) == len(xs)
        self.predict_xs = xs
        self.predict_ys = ys
Ejemplo n.º 16
0
 def __init__(self):
     self.mdl_parser = ModelConstructor()
     self.model = None
Ejemplo n.º 17
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
Ejemplo n.º 18
0
from compiler.PRISMParser import ModelConstructor

constructor = ModelConstructor()
mdl_fl_path = "../../prism_model/smalltest.prism"
model = constructor.parseModelFile(mdl_fl_path)
assert model.labels["failure"]() is False
model.localVars["sb_status"].setValue(0)
assert model.labels["failure"]() is True
Ejemplo n.º 19
0
def get_parsed():
    constructor = ModelConstructor()
    model = constructor._parse("../../prism_model/smalltest.prism")
    return model