def exp4(): basedir = Path("data/exp4") logs.set_logger(basedir, "train.log") cfg = linear.cfg # linear.load_config() # cfg.dir = Path(basedir, "data00") # cfg.label = "Z. P. Jiang (Init. Admm.)" # run_with_agent(linear.QLearnerEnv(), linear.ZLearnerAgent()) linear.load_config() cfg.dir = Path(basedir, "data01") cfg.label = "Q Learner (Init. Admm.)" run_with_agent(linear.QLearnerEnv(), linear.QLearnerAgent())
def __init__(self, conf): self.case_end_flag = False self.time_out_flag = False self.conf = conf self.sdk = api(self.conf['host'], self.conf['hub'], self.conf['user'], self.conf['pwd']) filename = "stability_debug.log" # error_file = "stability_error_" + "".join(self.conf['hub'].split(':')) + '.txt' self.logger = logs.set_logger(__name__, filename=filename) self.downlogs = Download_logs(self.conf['local_host'].split('/', 2)[2], 22, 'root', '3_5*rShsen')
def __init__(self, conf): self.case_end_flag = False self.time_out_flag = False self.conf = conf self.sdk = api(self.conf['host'], self.conf[ 'hub'], self.conf['user'], self.conf['pwd']) debug_file = "stability_debug_" + \ "".join(self.conf['hub'].split(':')) + ".txt" error_file = "stability_error_" + \ "".join(self.conf['hub'].split(':')) + '.txt' self.logger = set_logger( __name__, debugfile=debug_file, errorfile=error_file)
class testcase(unittest.TestCase): logger = set_logger(__name__) sdk = tools.get_api() model = tools.get_model() filters = tools.get_filter() filter_count = int(tools.read_job_config()['filter_count']) unfilter_count = int(tools.read_job_config()['unfilter_count']) timeout = tools.read_job_config()['case_timeout'] def setUp(self): self.timeout_flag = None self.flag1 = 0 self.flag2 = 0 self.key=None self.logger.info('X/E 测试chip0 filter uuid被动扫描 chip1 filter uuid 主动扫描,S chip0 filter uuid 主动扫描') self.timer = Timer(self.timeout, self.set_timeout) self.timer.start() def tearDown(self): self.timer.cancel() # 测试方法 def test_scan_filter_uuid(self): if self.model.startswith('S') or self.model.startswith('s'): a = threading.Thread(target=self.chip0_scan, args=(0, self.filters['filter_uuid'])) a.setDaemon(True) a.start() while True: if self.flag1 == 1: self.assertTrue(True) self.logger.info('pass\n') break elif self.timeout_flag: self.logger.info('fail\n') self.fail('Case failed,start scan timeout.') self.logger.error("Case failed,start scan timeout.") break else: a = threading.Thread(target=self.chip0_scan, args=(1, self.filters['filter_uuid'])) b = threading.Thread(target=self.chip1_scan, args=(0, self.filters['filter_uuid'])) a.setDaemon(True) b.setDaemon(True) b.start() a.start() while True: if self.flag1==1 and self.flag2==1: self.assertTrue(True) self.logger.info('pass\n') break elif self.timeout_flag: self.logger.info('fail\n') self.fail('Case failed,case timeout.') self.logger.error("Case failed,start scan timeout.") # break sys.exit(1)
class testcase(unittest.TestCase): logger = set_logger(__name__) sdk = tools.get_api() model = tools.get_model() filter_count = int(tools.read_job_config()['filter_count']) unfilter_count = int(tools.read_job_config()['unfilter_count']) timeout = tools.read_job_config()['case_timeout'] def setUp(self): self.timeout_flag = None self.flag1 = 0 self.flag2 = 0 self.key=None self.logger.info('X/E 测试chip0 filter duplicate主动扫描 chip1 不加filter duplicate主动扫描,S chip0 filter duplicate 被动扫描') self.timer = Timer(self.timeout, self.set_timeout) self.timer.start() def tearDown(self): self.timer.cancel()
class testcase(unittest.TestCase): logger = set_logger(__name__) sdk = tools.get_api() model = tools.get_model() filters = tools.get_filter() filter_count = int(tools.read_job_config()['filter_count']) unfilter_count = tools.read_job_config()['unfilter_count'] timeout = tools.read_job_config()['case_timeout'] def setUp(self): self.timeout_flag = None self.flag1 = 0 self.flag2 = 0 self.logger.info('测试X/E chip0 不加filter rssi被动扫描 chip1 filter uuid 被动扫描,S chip0 不加filter uuid 主动扫描') self.timer = Timer(10, self.set_timeout) self.timer.start() def tearDown(self): self.timer.cancel()
class testcase(unittest.TestCase): logger = set_logger(__name__) sdk = tools.get_api() model = tools.get_model() filter_count = int(tools.read_job_config()['filter_count']) unfilter_count = int(tools.read_job_config()['unfilter_count']) timeout = tools.read_job_config()['case_timeout'] local=tools.read_job_config()["local"] def setUp(self): self.timeout_flag = None self.flag1 = 0 self.flag2 = 0 self.key=None self.logger.info('X/E duration 参数测试chip0 主动扫 chip1 被动扫d=5.58s ,S chip0 主动扫d=7.8s ') self.timer = Timer(self.timeout, self.set_timeout) self.timer.start() def tearDown(self): self.timer.cancel()
class testcase(unittest.TestCase): logger = set_logger(__name__) local_sdk, cloud_sdk = tools.get_all_api() model = tools.get_model() filters = tools.get_filter() timeout = tools.read_job_config()['case_timeout'] def setUp(self): self.timeout_flag = None self.flag1 = None self.flag2 = None self.logger.info('测试点:测试chip0 local扫描的同时,chip1进行cloud扫描') self.timer = Timer(self.timeout, self.set_timeout) self.timer.start() def tearDown(self): self.timer.cancel() def test_scan_on_cloud_local(self): a = threading.Thread(target=self.local_scan) b = threading.Thread(target=self.cloud_scan) a.setDaemon(True) b.setDaemon(True) b.start() a.start() while True: if self.flag1 and self.flag2: self.assertTrue(True) self.logger.info('pass\n') break elif self.timeout_flag: self.logger.info('fail\n') self.fail('Case failed,start scan timeout.') self.logger.error("Case failed,start scan timeout.") break def test_step(self): self.flag = None #step1:start local scan. with closing(self.local_sdk.scan(chip=1)) as self.sse2: count = 0 for message in self.sse2: if message.startswith('data'): if count < 100: print('local', count, message) count += 1 else: self.flag = True self.logger.debug('Step 1: start local scan success.') break #step3:停止本地扫描 time.sleep(3) self.logger.debug('Step 2: stop local scan success.') #step2:start cloud scan. with closing(self.cloud_sdk.scan(chip=0)) as self.sse1: count = 0 for message in self.sse1: if message.startswith('data'): if count < 100: print('cloud', count, message) count += 1 else: self.flag = True self.logger.debug('Step 3:start cloud scan success.') break def set_timeout(self): self.timeout_flag = True
class testcase(unittest.TestCase): logger = set_logger(__name__) sdk = tools.get_api() model = tools.get_model() filter_count = int(tools.read_job_config()['filter_count']) unfilter_count = int(tools.read_job_config()['unfilter_count']) timeout = tools.read_job_config()['case_timeout'] local = tools.read_job_config()["local"] def setUp(self): self.timeout_flag = None self.flag1 = 0 self.flag2 = 0 self.key = None self.logger.info( 'X/E duration 参数测试chip0 被动扫 chip1 主动扫 d=9.99s,S chip0 被动扫d=9.99s ' ) self.timer = Timer(self.timeout, self.set_timeout) self.timer.start() def tearDown(self): self.timer.cancel() # 测试方法 # noinspection PyUnreachableCode def test_scan_filter_duplicates(self): # duration1=2 if self.local == "True": if self.model.startswith('S') or self.model.startswith('s'): a = threading.Thread(target=self.chip0_scan, args=(1, 9.99)) a.setDaemon(True) a.start() while True: if self.flag1 == 1: self.logger.info('pass\n') break elif self.timeout_flag: self.logger.info('fail\n') self.fail('Case failed,start scan timeout.') self.logger.error("Case failed,start scan timeout.") break else: a = threading.Thread(target=self.chip0_scan, args=(1, )) b = threading.Thread(target=self.chip1_scan, args=(0, 9.99)) a.setDaemon(True) b.setDaemon(True) b.start() a.start() while True: if self.flag1 == 1 and self.flag2 == 1: self.logger.info('pass\n') break elif self.timeout_flag: self.logger.info('fail\n') self.fail('Case failed,case timeout.') self.logger.error("Case failed,start scan timeout.") break sys.exit(1) else: self.logger.info("AC不需要测试duration参数,duration参数只对AP端生效") pass def chip0_scan(self, active=0, duration=None): # step1:chip 1 start passive scan,then start chip0 scan. if not duration is None: if isinstance(duration, str): duration1 = 5 print("字符串duration=", duration1) # 负数和0 扫描5s后停止 elif duration <= 0: duration1 = 5 print("负数和0 duration=", duration1) # 小数位数小于等于3时,会按照设置的时间停止扫描;小数位数大于3时,参数不起效,一直扫描数据 elif duration > float(str(duration)[:5]): duration1 = None print("小数位数大于3 duration=", duration1) else: duration1 = duration else: duration1 = None with closing(self.sdk.scan(chip=0, active=active, duration=duration1)) as self.sse1: if duration1: # time.sleep(duration1+0.5) d1 = datetime.datetime.now() print("start time at", datetime.datetime.now()) for message in self.sse1: continue d2 = datetime.datetime.now() print("end time at ", datetime.datetime.now()) t = float(((d2 - d1).seconds)) + float( (d2 - d1).microseconds / 1000 / 1000) - 1 print("second", t) if t <= duration1: self.flag1 += 1 self.logger.info( "Step 1:chip0 start scan with duration success.") else: self.fail("chip 0 duration failed") self.logger.debug("chip 0 duration failed") else: count = 0 for message in self.sse1: if message.startswith("data"): msg = json.loads(message[5:]) count += 1 print("chip 0 ", count, message) if count > self.unfilter_count: self.flag1 += 1 self.logger.info( "Step 1:chip0 start scan with no duration success." ) break # noinspection PyUnreachableCode def chip1_scan(self, active=0, duration=None): # step2:start chip1 scan. # 字符串 扫描5s后停止 if not duration is None: if isinstance(duration, str): duration1 = 5 print("字符串duration=", duration1) # 负数和0 扫描5s后停止 elif duration <= 0: duration1 = 5 print("负数和0 duration=", duration1) # 小数位数小于等于3时,会按照设置的时间停止扫描;小数位数大于3时,参数不起效,一直扫描数据 elif duration > float(str(duration)[:5]): duration1 = None print("小数位数大于3 duration=", duration1) else: duration1 = duration else: duration1 = None with closing(self.sdk.scan(chip=1, active=active, duration=duration1)) as self.sse1: if duration1: # time.sleep(duration1+0.5) d1 = datetime.datetime.now() print("start time at", datetime.datetime.now()) for message in self.sse1: continue d2 = datetime.datetime.now() print("end time at ", datetime.datetime.now()) t = float(((d2 - d1).seconds)) + float( (d2 - d1).microseconds / 1000 / 1000) - 0.5 print("second", t) if t <= duration1: self.flag2 += 1 self.logger.info( "Step 1:chip1 start scan with duration success.") else: self.fail("chip 0 duration failed") self.logger.debug("chip 1 duration failed") else: count = 0 for message in self.sse1: if message.startswith("data"): msg = json.loads(message[5:]) count += 1 print("chip 0 ", count, message) if count > self.unfilter_count: self.flag2 += 1 self.logger.info( "Step 1:chip1 start scan with no duration success." ) break def set_timeout(self): self.timeout_flag = True
def exp4(): """This experimet uses a simple linear model to learn the optimal policy with arbitrary initial policy. """ class Env(LearningEnv): def __init__(self): super().__init__(**vars(cfg.env_kwargs)) wingrock.load_config() self.x = wingrock.System() self.x.unc = lambda t, x: 0 self.A = wingrock.cfg.Am self.B = wingrock.cfg.B self.Kopt, self.Popt = LQR.clqr(self.A, self.B, cfg.Q, cfg.R) self.behave_K, _ = LQR.clqr(self.A - 3, self.B, cfg.Qb, cfg.Rb) self.logger = fym.logging.Logger(Path(cfg.dir, "env.h5")) self.logger.set_info(cfg=cfg) def behavior(self, t, x): un = -self.behave_K.dot(x) noise = np.sum([ 0.05 * (np.sin(t) + 1) * (np.cos(np.pi * t) + 1), -1 * np.sin(3.1 * t + 2) + 1 * np.cos(t)**2, ]) * 0.1 u = un + noise * np.exp(-0.8 * t / cfg.env_kwargs.max_t) return u def observation(self): t = self.clock.get() x = self.x.state u, c = self.deriv(t, x) xdot = self.x.deriv(t, x, u, c) return t, x, u, xdot def deriv(self, t, x): u = self.behavior(t, x) c = 0 return u, c def set_dot(self, t): x = self.x.state u, c = self.deriv(t, x) self.x.dot = self.x.deriv(t, x, u, c) def logger_callback(self, i, t, y, *args): states = self.observe_dict(y) x = states["x"] u, c = self.deriv(t, x) return dict(t=t, u=u, c=c, K=self.Kopt, P=self.Popt, **states) def load_config(): cfg.env_kwargs = SN() cfg.env_kwargs.dt = 0.01 cfg.env_kwargs.max_t = 20 agents.load_config() cfg.Agent = agents.cfg cfg.Agent.CommonAgent.memory_len = 2000 cfg.Agent.CommonAgent.batch_size = 1000 cfg.Agent.CommonAgent.train_epoch = 20 cfg.Agent.CommonAgent.train_start = 10 cfg.Agent.CommonAgent.train_period = 3 cfg.Agent.SQLAgent = SN(**vars(cfg.Agent.CommonAgent)) cfg.Agent.KLMAgent = SN(**vars(cfg.Agent.CommonAgent)) cfg.Q = np.diag([10, 10, 1]) cfg.R = np.diag([10]) cfg.F = -1 * np.eye(1) cfg.Qb = np.diag([1, 1, 1]) cfg.Rb = np.diag([1]) cfg.K_init = -5 * np.ones((1, 3)) # Init the experiment expdir = Path("data/exp4") logs.set_logger(expdir, "train.log") cfg = SN() if False: """ Data 001 ~ Data 002 These data compares SQL and KLM for wingrock linear model without uncertainty but initial unstable gain. """ # Data 001 load_config() # Load the experiment default configuration cfg.dir = Path(expdir, "data-001") cfg.label = "SQL" env = Env() agent = agents.SQLAgent(cfg.Q, cfg.R, cfg.F, K_init=cfg.K_init) agent.logger = fym.logging.Logger(Path(cfg.dir, "sql-agent.h5")) env.run(agent) # Data 002 load_config() # Load the experiment default configuration cfg.dir = Path(expdir, "data-002") cfg.label = "Kleinman" env = Env() agent = agents.KLMAgent(cfg.Q, cfg.R, K_init=cfg.K_init) agent.logger = fym.logging.Logger(Path(cfg.dir, "klm-agent.h5")) env.run(agent) """ Data 003 ~ Data 004 These data compares the two methods where uncertainty exists and an intinal stable gain is used. """ unc = lambda t, x: 0.01 * x[0]**2 + 0.05 * x[1] * x[2] K_init = np.zeros((1, 3)) # Data 003 load_config() # Load the experiment default configuration cfg.dir = Path(expdir, "data-003") cfg.label = "SQL" cfg.K_init = K_init env = Env() env.x.unc = unc agent = agents.SQLAgent(cfg.Q, cfg.R, cfg.F, K_init=cfg.K_init) agent.logger = fym.logging.Logger(Path(cfg.dir, "sql-agent.h5")) env.run(agent) # Data 004 load_config() # Load the experiment default configuration cfg.dir = Path(expdir, "data-004") cfg.label = "Kleinman" cfg.K_init = K_init env = Env() env.x.unc = unc agent = agents.KLMAgent(cfg.Q, cfg.R, K_init=cfg.K_init) agent.logger = fym.logging.Logger(Path(cfg.dir, "klm-agent.h5")) env.run(agent) """ Data 005 ~ Data 006 These data compares the two methods where uncertainty exists and an intinal unstable gain is used. """ unc = lambda t, x: 0.1 * x[0] * np.abs(x[0]) - 0.5 * x[1] * x[2] # unc = lambda t, x: 0 K_init = -np.vstack([3, 6, 7]).T # Data 005 load_config() # Load the experiment default configuration cfg.dir = Path(expdir, "data-005") cfg.label = "SQL" cfg.K_init = K_init cfg.env_kwargs.max_t = 50 env = Env() env.x.unc = unc agent = agents.SQLAgent(cfg.Q, cfg.R, cfg.F, K_init=cfg.K_init) agent.logger = fym.logging.Logger(Path(cfg.dir, "sql-agent.h5")) env.run(agent) # Data 006 load_config() # Load the experiment default configuration cfg.dir = Path(expdir, "data-006") cfg.label = "Kleinman" cfg.K_init = K_init cfg.env_kwargs.max_t = 50 env = Env() env.x.unc = unc agent = agents.KLMAgent(cfg.Q, cfg.R, K_init=cfg.K_init) agent.logger = fym.logging.Logger(Path(cfg.dir, "klm-agent.h5")) env.run(agent)
class testcase(unittest.TestCase): logger = set_logger(__name__) api = get_api() model = get_model() def setUp(self): self.logger.info('测试设备的主被动扫描切换功能') self.timer = Timer(30, self.close) self.timer.start() def tearDown(self): self.timer.cancel() # noinspection PyUnreachableCode,PyUnreachableCode,PyUnreachableCode,PyUnreachableCode,PyUnreachableCode,PyUnreachableCode,PyUnreachableCode def test_change_scan_mode(self): flag = None count = 0 if self.model.startswith('S') or self.model.startswith('s'): with closing(self.api.scan(active=0)) as self.sse1: count = 0 for data1 in self.sse1: if count < 200: if 'scanData' in data1: self.fail('passive scan start fail') self.logger.error('passive scan start fail') break else: count += 1 flag = True else: flag = True # self.sse.close() self.logger.debug('Step1:passive scan start success') break with closing(self.api.scan(active=1)) as self.sse2: count = 0 for data2 in self.sse2: if count < 200: if 'scanData' in data2: flag = True self.logger.debug( 'Step2:Active scan start success') break else: count += 1 else: self.fail('Active scan start failed') self.logger.error('Step2:Active scan start fail') break with closing(self.api.scan(active=0)) as self.sse3: count = 0 for data3 in self.sse3: if count < 200: if 'scanData' in data3: self.fail('passive scan start fail') self.logger.error('passive scan start fail') break else: count += 1 else: flag = True self.logger.debug('Step3:passive scan start success') break with closing(self.api.scan(active=1)) as self.sse4: count = 0 for data4 in self.sse4: if count < 200: if 'scanData' in data4: flag = True self.logger.debug( 'Step5:Active scan start success') break else: count += 1 else: self.fail('Active scan start failed') self.logger.error('Step5:Active scan start fail') break self.assertTrue(flag) if flag: self.logger.info('pass\n') else: self.logger.info('fail\n') else: with closing(self.api.scan(active=0)) as self.sse1: count = 0 for data1 in self.sse1: if count < 200: if 'scanData' in data1: self.fail('passive scan start fail') self.sse1.close() self.logger.error('passive scan start fail') else: count += 1 flag = True else: flag = True # self.sse.close() self.logger.debug('Step1:passive scan start success') break with closing(self.api.scan(active=1)) as self.sse2: count = 0 for data2 in self.sse2: if count < 200: if 'scanData' in data2: flag = True self.logger.debug( 'Step1:Active scan start success') break else: count += 1 flag = True else: self.fail('Active scan start failed') self.sse2.close() self.logger.error('Step2:Active scan start success') with closing(self.api.scan(active=0)) as self.sse3: count = 0 for data3 in self.sse3: if count < 200: if 'scanData' in data3: self.fail('passive scan start fail') self.sse3.close() self.logger.error('passive scan start fail') else: count += 1 flag = True else: flag = True self.logger.debug('Step3:passive scan start success') break with closing(self.api.scan(active=1)) as self.sse4: count = 0 for data4 in self.sse4: if count < 200: if 'scanData' in data4: flag = True self.logger.debug( 'Step1:Active scan start success') break else: count += 1 flag = True else: self.fail('Active scan start failed') self.sse4.close() self.logger.error('Step4:Active scan start success') with closing(self.api.scan(chip=1, active=0)) as self.sse5: count = 0 for data1 in self.sse5: if count < 200: if 'scanData' in data1: self.fail('passive scan start fail') self.sse5.close() self.logger.error('passive scan start fail') else: count += 1 flag = True else: flag = True # self.sse.close() self.logger.debug('Step1:passive scan start success') break with closing(self.api.scan(chip=1, active=1)) as self.sse6: count = 0 for data2 in self.sse6: if count < 200: if 'scanData' in data2: flag = True self.logger.debug( 'Step1:Active scan start success') break else: count += 1 flag = True else: self.fail('Active scan start failed') self.sse6.close() self.logger.error('Step2:Active scan start success') with closing(self.api.scan(chip=1, active=0)) as self.sse7: count = 0 for data3 in self.sse7: if count < 200: if 'scanData' in data3: self.fail('passive scan start fail') self.sse7.close() self.logger.error('passive scan start fail') else: count += 1 flag = True else: flag = True self.logger.debug('Step3:passive scan start success') break with closing(self.api.scan(chip=1, active=1)) as self.sse8: count = 0 for data4 in self.sse8: if count < 200: if 'scanData' in data4: flag = True self.logger.debug( 'Step1:Active scan start success') break else: count += 1 flag = True else: self.fail('Active scan start failed') self.sse8.close() self.logger.error('Step4:Active scan start success') if flag: self.logger.info('pass\n') else: self.logger.info('fail\n') # noinspection PyUnreachableCode def close(self): self.fail("Case failed,start scan timeout.") self.logger.error("Case failed,start scan timeout.")
class testcase(unittest.TestCase): logger = set_logger(__name__) sdk = get_api() model = get_model() timeout = read_job_config()['case_timeout'] def setUp(self): self.logger.info('测试在不同芯片之间,按先后顺序,能否成功开启扫描') self.timer = Timer(self.timeout,self.close) self.timer.start() def tearDown(self): self.timer.cancel() def test_start_scan_in_diff_chip(self): flag = None if self.model.startswith('S')or self.model.startswith('s'): self.logger.debug('if model is s1000系列的产品,直接pass') #if model is s1000系列的产品,直接pass self.assertTrue(True) else: #step1:chip 0 start scan,then stop scan with closing(self.sdk.scan(chip = 0)) as self.sse: for message in self.sse: if message.startswith('data'): flag = True self.logger.debug("step1:chip 0 start scan,then stop scan success") self.sse.close() elif 'keep-alive' in message: pass else: flag = False self.logger.error('start scan fail,%s'%message) #step2:chip 1 start scan,then stop scan. with closing(self.sdk.scan(chip = 1)) as self.sse: for message in self.sse: if message.startswith('data'): flag = True self.logger.debug("step2:chip 1 start scan,then stop scan success") self.sse.close() elif 'keep-alive' in message: pass else: flag = False self.logger.error('start scan fail,%s'%message) #step3:chip 0 start scan,then change to chip1 scan. with closing(self.sdk.scan(chip = 0)) as self.sse: for message in self.sse: if message.startswith('data'): flag = True self.logger.debug("step3:chip 0 start scan,then change to chip1 scan success") # self.change_scan(flag) self.sse.close() elif 'keep-alive' in message: pass else: flag = False self.logger.error('start scan fail,%s'%message) #step4:chip 1 start scan,then change to chip0 scan. with closing(self.sdk.scan(chip = 1)) as self.sse: for message in self.sse: if message.startswith('data'): flag = True self.logger.debug("step4:chip 1 start scan,then change to chip0 scan success") # self.change_scan2(flag) self.sse.close() elif 'keep-alive' in message: pass else: flag = False self.logger.error('start scan fail,%s'%message) #step5:chip 0 start scan,then change to chip1 scan. with closing(self.sdk.scan(chip = 1)) as self.sse: for message in self.sse: if message.startswith('data'): flag = True self.logger.debug("step5:chip 0 start scan,then change to chip1 scan success.") self.sse.close() elif 'keep-alive' in message: pass else: flag = False self.logger.error('start scan fail,%s'%message) self.assertTrue(flag) if flag: self.logger.info('case pass\n') else: self.logger.info('case fail\n')
import sys import os import json path = os.getcwd().split('cassia_automation')[0] + 'cassia_automation/lib/' sys.path.append(path) from logs import set_logger from api import api logger = set_logger(__name__) def read_config(): ''' 读取主配置文件,并返回文件内容 :return: dict ''' try: path = os.getcwd().split( 'cassia_automation')[0] + 'cassia_automation/config/config.json' with open(path, encoding='utf8') as conf: conf = json.load(conf) if isinstance(conf, dict): logger.debug('Read config success.') else: logger.error('config para error.') raise BaseException('config para error!') except Exception as e: logger.error('Config file read failed!') print('ERROR:Config file read failed!', e) return conf
def main(): """Experiment 7 This experiment demonstrates the performance of SQL for an unknown quadrotor model. The LQR controller is designed based on [Dydek et al., 2013](doi.org/10.1109/TCST.2012.2200104). The performance outputs are ``pos`` and ``psi``. The observed states are ``pos``, ``angle`` and their derivatives. where the controller takes angles and altitude and returns thrusts of each rotor. """ load_config() random.seed(0) np.random.seed(0) # t0 = time.time() # ------ Sampling ------ # # cfg.env.kwargs.max_t = 20 # env = Env() # sample(env, "sample_%02d.h5") # ------ Training ------# logs.set_logger(cfg.path.base, "train.log") # ------ Training Unstable: SQL ------# # agents.load_config() # agent = agents.SQLAgent( # Q=cfg.agent.Q, # R=cfg.agent.R, # F=-cfg.train.s*np.eye(cfg.agent.R.shape[0]) # ) # agent.logger = fym.logging.Logger(cfg.path.train.sql) # train(agent=agent) # ------ Training Unstable: KLM ------# # agents.load_config() # agent = agents.KLMAgent(Q=cfg.agent.Q, R=cfg.agent.R) # agent.logger = fym.Logger(cfg.path.train.klm) # train(agent=agent) # ------ Training Stable ------# env = Env() K = env.get_random_stable_gain() # ------ Training Stable: SQL ------# agents.load_config() agent = agents.SQLAgent(Q=cfg.agent.Q, R=cfg.agent.R, F=-cfg.train.s * np.eye(cfg.agent.R.shape[0]), K_init=K) agent.logger = fym.logging.Logger(cfg.path.train.sql_stable) train(agent=agent) # ------ Training Stable: KLM ------# agents.load_config() agent = agents.KLMAgent(Q=cfg.agent.Q, R=cfg.agent.R, K_init=K) agent.logger = fym.Logger(cfg.path.train.klm_stable) train(agent=agent)
class testcase(unittest.TestCase): logger = set_logger(__name__) sdk = get_api() model = get_model() def setUp(self): self.timeout_flag = None self.flag1 = None self.flag2 = None self.logger.info('测试两个芯片同时开启被动扫描') self.timer = Timer(30, self.timeout) self.timer.start() def tearDown(self): self.timer.cancel() # noinspection PyUnreachableCode def test_chips_start_scan_meanwhile(self): if self.model.startswith('S') or self.model.startswith('s'): self.assertTrue(True) else: threading.Thread(target=self.chip0_scan).start() threading.Thread(target=self.chip1_scan).start() while True: if self.flag1 and self.flag2: self.assertTrue(True) self.logger.info('pass\n') break elif self.timeout_flag: self.logger.info('fail\n') self.fail('Case failed,start scan timeout.') self.logger.error("Case failed,start scan timeout.") break # noinspection PyUnreachableCode def chip0_scan(self): #step1:chip 1 start passive scan,then start chip0 scan. with closing(self.sdk.scan(chip=0)) as self.sse1: count = 0 for message in self.sse1: if count < 300: if message.startswith('data'): # print('chip0',message) count += 1 if 'scanData' in message: self.fail('Scan passive mode error',message) break elif 'keep-alive' in message: pass else: self.logger.error('start scan fail,%s' % message) else: self.flag1 = True self.logger.debug('Step 1:chip0 start passive scan success.') break # noinspection PyUnreachableCode def chip1_scan(self): #step2:start chip0 scan. with closing(self.sdk.scan(chip=1)) as self.sse2: count = 0 for message in self.sse2: if count < 300: if message.startswith('data'): # print('chip1',message) count += 1 if 'scanData' in message: self.fail('Scan passive mode error',message) break elif 'keep-alive' in message: pass else: self.logger.error('Step 2:start scan fail,%s' % message) else: self.flag2 = True self.logger.debug('Step 2:chip1 start active scan success ') break def timeout(self): self.timeout_flag = True
class testcase(unittest.TestCase): logger = set_logger(__name__) sdk = tools.get_api() model = tools.get_model() filters = tools.get_filter() timeout = tools.read_job_config()['case_timeout'] filter_count = int(tools.read_job_config()['filter_count']) unfilter_count = int(tools.read_job_config()['unfilter_count']) def setUp(self): self.timeout_flag = None self.flag1 = 0 self.flag2 = 0 self.logger.info('测试chip0 filter name主动扫描,chip1 不加过虑主动扫描') self.timer = Timer(self.timeout, self.set_timeout) self.timer.start() def tearDown(self): self.timer.cancel() #测试方法 # noinspection PyUnreachableCode,PyUnreachableCode def test_scan_filter_name(self): if self.model.startswith('S') or self.model.startswith('s'): a = threading.Thread(target=self.chip0_scan, args=(1, self.filters['filter_name'])) b = threading.Thread(target=self.chip0_scan, args=(0, )) a.setDaemon(True) b.setDaemon(True) b.start() a.start() while True: if self.flag1 == 2: self.assertTrue(True) self.logger.info('pass\n') break elif self.timeout_flag: self.logger.info('fail\n') self.fail('Case failed,start scan timeout.') self.logger.error("Case failed,start scan timeout.") break else: a = threading.Thread(target=self.chip0_scan, args=(1, self.filters['filter_name'])) b = threading.Thread(target=self.chip1_scan, args=(0, )) a.setDaemon(True) b.setDaemon(True) b.start() a.start() while True: if self.flag1 == 1 and self.flag2 == 1: self.assertTrue(True) self.logger.info('pass\n') break elif self.timeout_flag: self.logger.info('fail\n') self.fail('Case failed,start scan timeout.') self.logger.error("Case failed,start scan timeout.") break # noinspection PyUnreachableCode def chip0_scan(self, active=0, filter_name=None): #step1:chip 1 start passive scan,then start chip0 scan. with closing( self.sdk.scan(chip=0, active=active, filter_name=filter_name)) as self.sse1: count = 0 for message in self.sse1: if message.startswith('data'): msg = json.loads(message[5:]) if filter_name: #进入开启过滤的扫描结果判断流程 if count < self.filter_count: print('chip0', count, message) name = msg['name'] if name != self.filters['filter_name']: self.fail('filter name failed.') self.logger.debug('filter name failed.') break else: count += 1 else: self.flag1 += 1 self.logger.debug( 'Step 1:chip0 start scan with filter name success.' ) break else: #进入不开启过滤的扫描结果判断流程 if count < 300: print('chip0', count, message) count += 1 else: self.flag1 += 1 self.logger.debug( 'Step 1:chip0 start scan with no filter name success.' ) break # noinspection PyUnreachableCode def chip1_scan(self, active=0, filter_name=None): #step2:start chip0 scan. with closing( self.sdk.scan(chip=1, active=active, filter_name=filter_name)) as self.sse2: count = 0 tmp_name = [] for message in self.sse2: if message.startswith('data'): msg = json.loads(message[5:]) if filter_name: if count < self.filter_count: print('chip1', count, message) name = msg['name'] if name != filter_name: self.fail('filter name failed.') self.logger.debug('filter name failed.') break else: count += 1 else: self.flag2 += 1 self.logger.debug( 'Step 2:chip1 start scan with filter name success.' ) break else: #进入不开启过滤的扫描结果判断流程 if count < self.unfilter_count: print('chip1', count, message) count += 1 else: self.flag2 += 1 self.logger.debug( 'Step 2:chip1 start scan with no filter name success.' ) break def set_timeout(self): self.timeout_flag = True
def exp6(): """This experiment uses a complex aircraft model to learn the optimal policy with arbitrary initial policy. The morphing aircraft in ``fym`` is used. """ from fym.models.aircraft import MorphingLon class Env(LearningEnv): def __init__(self): super().__init__(**vars(cfg.env_kwargs)) self.x = MorphingLon() self.PI = BaseSystem() trims = self.x.get_trim() self.trim = {k: v for k, v in zip(["x", "u", "eta"], trims)} self.A = jacob_analytic(self.x.deriv, 0)(*trims) self.B = jacob_analytic(self.x.deriv, 1)(*trims) self.Kopt, self.Popt = LQR.clqr(self.A, self.B, cfg.Q, cfg.R) self.behave_K, _ = LQR.clqr(self.A, self.B, cfg.Qb, cfg.Rb) self.add_noise = True def behavior(self, t, x): # un = self.trim["u"] - self.behave_K.dot(x - self.trim["x"]) if self.add_noise: un = self.trim["u"] noise = np.vstack([ 0.2 * (np.sin(t) + 0.5) * (np.cos(np.pi * t) + 0.5), -1 * np.sin(0.31 * t + 2) + 1 * np.cos(2 * t), ]) * 0.02 noise = noise * np.exp(-0.8 * t / cfg.env_kwargs.max_t) else: un = self.trim["u"] - self.behave_K.dot(x - self.trim["x"]) noise = 0 return un + noise def deriv(self, t, x): u = self.behavior(t, x) eta = self.trim["eta"] return u, eta def observation(self): t = self.clock.get() x = self.x.state u, eta = self.deriv(t, x) xdot = self.x.deriv(x, u, eta) dx = x - self.trim["x"] du = u - self.trim["u"] return t, dx, du, xdot def set_dot(self, t): x = self.x.state u, eta = self.deriv(t, x) self.x.dot = self.x.deriv(x, u, eta) self.PI.dot = self.get_cost(x, u) def get_cost(self, x, u): dx = x - self.trim["x"] du = u - self.trim["u"] return 0.5 * (dx.T @ cfg.Q @ dx + du.T @ cfg.R @ du) def logger_callback(self, i, t, y, *args): states = self.observe_dict(y) x = states["x"] u, eta = self.deriv(t, x) return dict(t=t, u=u, eta=eta, K=self.Kopt, P=self.Popt, **states) def load_config(): cfg.env_kwargs = SN() cfg.env_kwargs.dt = 0.01 cfg.env_kwargs.max_t = 40 agents.load_config() agents.cfg.CommonAgent.memory_len = 4000 agents.cfg.CommonAgent.batch_size = 2000 agents.cfg.CommonAgent.train_epoch = 100 agents.cfg.CommonAgent.train_start = 20 agents.cfg.CommonAgent.train_period = 2 agents.cfg.SQLAgent = SN(**vars(agents.cfg.CommonAgent)) agents.cfg.KLMAgent = SN(**vars(agents.cfg.CommonAgent)) cfg.Q = np.diag([10, 10, 1, 10]) cfg.R = np.diag([1000, 1]) cfg.F = -1 * np.eye(2) cfg.Qb = np.diag([1, 1, 1, 10]) cfg.Rb = np.diag([1000, 1]) cfg.K_init = np.zeros((2, 4)) cfg.test = SN() cfg.test.dataname_learnt = "test-learnt-env.h5" cfg.test.dataname_lqr = "test-lqr-env.h5" cfg.test.initial_perturb = np.vstack((3, 0.1, 0.1, 0.1)) def test(env): env.reset() while True: env.render() *_, done = env.step(None) if done: break env.close() # Init the experiment expdir = Path("data/exp6") logs.set_logger(expdir, "train.log") cfg = SN() """ Data 001 ~ Data 002 This sub-experiment compares SQL and KLM for morphing aircraft using an initial unstable policy """ # ------ Data 001 ------ # load_config() # Load the experiment default configuration cfg.dir = Path(expdir, "data-001") cfg.label = "SQL" # ------ Train ------ # env = Env() agent = agents.SQLAgent(cfg.Q, cfg.R, cfg.F) # Set loggeres env.logger = fym.logging.Logger(Path(cfg.dir, "env.h5")) env.logger.set_info(cfg=cfg) agent.logger = fym.logging.Logger(Path(cfg.dir, "sql-agent.h5"), max_len=1) env.run(agent) # ------ Test ------ # env = Env() env.logger = fym.logging.Logger(Path(cfg.dir, cfg.test.dataname_learnt)) env.behave_K = fym.logging.load(Path(cfg.dir, "sql-agent.h5"))["K"][-1] env.add_noise = False env.x.initial_state = env.trim["x"] + cfg.test.initial_perturb test(env) env = Env() env.logger = fym.logging.Logger(Path(cfg.dir, cfg.test.dataname_lqr)) env.behave_K = env.Kopt env.add_noise = False env.x.initial_state = env.trim["x"] + cfg.test.initial_perturb test(env) # ------ Data 002 ------ # load_config() # Load the experiment default configuration cfg.dir = Path(expdir, "data-002") cfg.label = "Kleinman" # ------ Train ------ # env = Env() agent = agents.KLMAgent(cfg.Q, cfg.R) # Set loggeres env.logger = fym.logging.Logger(Path(cfg.dir, "env.h5")) env.logger.set_info(cfg=cfg) agent.logger = fym.logging.Logger(Path(cfg.dir, "klm-agent.h5"), max_len=1) env.run(agent) # ------ Test ------ # env = Env() env.logger = fym.logging.Logger(Path(cfg.dir, cfg.test.dataname_learnt)) env.behave_K = fym.logging.load(Path(cfg.dir, "klm-agent.h5"))["K"][-1] env.add_noise = False env.x.initial_state = env.trim["x"] + cfg.test.initial_perturb test(env)