def testSPS(): from util.util import interval from module.Module import Constant config = SPSConfig.SPSConfig() moduleFactory = ModuleFactory(config) modelfactory = ModelFactory(moduleFactory) model = modelfactory.get_built() ltl = ['U[1, {0}]'.format(730), 'T', 'failure'] # 一年之内系统失效 checker = Checker(model, ltl, duration=730, fb=False) wrapper = ExperimentWrapper(checker, samples_per_param=100) trainx = interval(1, 10, 0.5) testx = interval(1, 10, 0.1) thickness_params = [Constant('SCREEN_THICKNESS', st) for st in trainx] wrapper.setconstants([thickness_params]) result = wrapper.do_expe() cases = map(lambda tuple: tuple[0], result) labels = map(lambda tuple: tuple[1], result) regressor = BPNeuralNetwork() regressor.setup(1, 5, 1) regressor.train(cases, labels) test_cases = map(lambda c: Constant('SCREEN_THICKNESS', c), testx) test_labels = [regressor.predict(test_case) for test_case in testx] # 对多组参数进行模型验证 # logger.info("mc begin") # wrapper.setconstants([test_cases]) # mcresult = wrapper.modelcheck() # mc_labels = map(lambda tuple: tuple[1], mcresult) plt.plot(testx, [i[0] for i in test_labels], label='predict') # plt.plot(map(lambda const: const.get_value(), test_cases), mc_labels, label='mc') plt.show()
def testrunexpe(): from experiment.ExperimentWrapper import executepoissionexpe, executemodelchecking from util.util import interval from module.Module import Constant params = [] for i in interval(0.5, 5, 0.1): params.append(Constant('r', i)) result = executepoissionexpe([params]) # {paramsdict: prob} cases = map(lambda tuple: tuple[0], result) labels = map(lambda tuple: tuple[1], result) regressor = BPNeuralNetwork() regressor.setup(1, 5, 1) regressor.train(cases, labels) test_cases = map(lambda v: Constant('r', v), interval(0.5, 5, 0.05)) test_labels = [regressor.predict(test_case) for test_case in test_cases] # 对多组参数进行模型验证 mcresult = executemodelchecking([test_cases]) # mc_labels = map(lambda tuple: tuple[1], mcresult) plt.plot(map(lambda const: const.get_value(), test_cases), test_labels, label='predict') # plt.plot(map(lambda const: const.get_value(), test_cases), mc_labels, label='mc') plt.show()
def setUp(self): RegressionTestBase.setUp(self) self._parameter_name = 'psend' self._export_train_path = get_data_dir() + get_sep() + "broadcast9_train.csv" self._prism_data_path = get_data_dir() + get_sep() + "broadcast9_prism_data.csv" self._train_xs = interval(0, 1, 0.02) self._test_xs = interval(0, 1, 0.01)
def test(self): cases = interval(-pi, pi, 0.1) errors = [random() / 10 * [-1, 1][random() > 0.5] for _ in cases] labels = [cos(x) + error for x, error in zip(cases, errors)] self.setup(1, 5, 1, 0.05, 0.1) self.train(cases, labels, 10000) test_cases = interval(-pi, pi, 0.01) test_labels = [cos(x) for x in test_cases] predict_labels = [self.predict(test_case) for test_case in test_cases] plt.xlabel("x") plt.ylabel("true value") plt.plot(test_cases, test_labels, label="true value") plt.plot(test_cases, predict_labels, label="predict value") plt.legend() plt.show()
def testShowPredictError(self): training_costs = self._train() plt.subplot(121) plt.plot(range(self._get_epochs()), training_costs) plt.subplot(122) prism_xs, prism_ys = parse_csv_cols(self._prism_result_path) plt.plot(prism_xs, prism_ys, color='g') plt.scatter(self._raw_train_xs, self._raw_train_ys, color='b') test_xs = interval(0, 1, 0.005) test_ys = self._predict(test_xs) plt.plot(test_xs, test_ys, color='r') plt.show()
def _gen_training_data(self): ''' 返回x,y元祖的数组 :return: [(x, y)] ''' checker = self._get_checker() train_ys = [] self._rearrange(params=[(self._parameter_name, interval(0, 1, 0.1))]) checker.set_antithetic(True) for x in self._train_xs: self._set_parameter(self._parameter_name, x) train_ys.append(checker.run_checker()) return [(x, y) for x, y in zip(self._train_xs, train_ys)]
def main(): from test.PoissionProcess import poission_model from util.util import interval from module.Module import Constant model = poission_model() ltl = ['!', 'U[0,1]', None, 'T', 'nge4', None, None] # 一秒之内n<4 checker = Checker.Checker(model, ltl) wrapper = ExperimentWrapper(checker) constants = [] for value in interval(0.5, 5, 0.1): constants.append(Constant('r', value)) wrapper.setconstants([constants]) # results = wrapper.experiment() wrapper.modelcheck()
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)
def testWeightedRegress(self): ''' 使用加权回归神经网络模型拟合数据 :return: ''' regressor = MyNNRegressor([1, 10, 1], cost=WeightedQuadraticRegressCost) training_data = self.testGenTrainData() regressor.SGD(training_data, 30, 10, 0.3) test_x = interval(0, 1, 0.01) test_x = [np.reshape(x, [1, 1]) for x in test_x] test_y = [regressor.feedforward(x) for x in test_x] test_y = [y[0][0] for y in test_y] test_x = [x[0][0] for x in test_x] plt.plot(test_x, test_y, c="b") # load data from prism path = get_prism_model_dir() + get_sep() + "toy_theta_0_1_001" error = self._average_error(test_x, test_y, path) print "error:{}".format(error) cols = parse_csv_cols(path) prism_xs, prism_ys = cols plt.plot(prism_xs, prism_ys, c="r") plt.show()