def generate_one(self, power, idx, out_path): print(idx, out_path) if not power: self.input_fmt['value'] = self.data_set.ori_data.loc[idx].values power = restore_power_input(self.input_fmt, self.base_power, normalize=False) print('old=', self.input_fmt['value']) # TODO self.input_fmt['value'] *= np.random.normal(loc=1.0, scale=0.1, size=self.input_fmt.shape[0]) power = restore_power_input(self.input_fmt, power, normalize=False) print('new=', self.input_fmt['value']) shutil.rmtree(out_path, ignore_errors=True) power.save_power(out_path, fmt=self.fmt, lp=False) shutil.copy(os.path.join(self.base_path, 'LF.L0'), out_path) shutil.copy(os.path.join(self.base_path, 'ST.S0'), out_path) call_wmlf(out_path) ret = check_lfcal(out_path) if ret: idx = os.path.split(out_path)[-1] new_data = load_power_input(self.input_fmt, power) new_data = self.data_set.normalize_it(new_data) new_feature = self.feature_model.predict(new_data[np.newaxis, :]) self.features.loc[idx] = new_feature.reshape(-1) self.generated_num += 1 return ret
def load_actions(power, base_path, out_path, files, fmt='on', st=True, wmlf=True): """ 从文件加载并执行修改动作 :param power: Power. Power实例。 :param base_path: str. 基础数据目录。 :param out_path: str. 输出目录,输入的动作文件也在这里。 :param files: [str]. 文件列表。 :param fmt: str. 数据格式类型。 :param st: bool. 是否输出ST*文件。 :param wmlf: bool. 是否计算潮流。 :return dict. 每个文件对应目录的输出是否成功(潮流是否收敛) """ ret = {} if not power: power = Power(fmt=fmt) power.load_power(base_path, fmt=fmt, lp=False, st=st) power.set_index(idx='name') for f in files: actions = pd.read_csv(os.path.join(out_path, f), encoding='gbk', index_col=False) for _, etype, idx, dtype, value in actions.itertuples(): set_values(power.data, etype, dtype, {idx: value}) if '.' in f: name = f[:f.index('.')] path = os.path.join(out_path, name) power.save_power(path, fmt=power.fmt, lp=False, st=st) shutil.copy(os.path.join(base_path, 'LF.L0'), path) if st: shutil.copy(os.path.join(base_path, 'ST.S0'), path) if wmlf: call_wmlf(path) ret[name] = check_lfcal(path) else: ret[name] = True return ret
def run_step(self, actions=None): """ 按照给定actions运行一步。 :param actions: str. “random",随机初始化,仅用于测试; or dict. 动作集合,由load_action函数执行动作。 :return: (np.array, float, bool). 状态量、回报值、结束标志。 """ self.step += 1 path = self.get_ep_path(step=self.step) if actions == 'random': # just for test distribute_generators_p(self.power.data['generator'], 1., sigma=0.1) distribute_loads_p(self.power.data['load'], 1., p_sigma=0.1, keep_factor=True, factor_sigma=0.1) elif actions is not None: self.load_action(actions) shutil.rmtree(path, ignore_errors=True) self.power.save_power(path, self.fmt, lf=True, lp=False, st=True) shutil.copy(os.path.join(self.base_path, 'LF.L0'), path) shutil.copy(os.path.join(self.base_path, 'ST.S0'), path) call_wmlf(path) if check_lfcal(path): self.power.drop_data(self.fmt, 'lp') self.power.load_power(path, self.fmt, lf=False, lp=True, st=False) self.power.data['generator']['p0'] = self.power.data['generator']['p'] if self.step == 0: self.load_init_info() state = self.get_state() if os.name != 'nt': call_psa(path) assess, done, _ = self.make_assessment(path, method='min', min_n=3) else: assess = np.random.rand() done = (assess < 0.1) else: state = [] assess = PF_NOTCONV_REWARD done = True self.assessments.append(assess) if self.step == 0: reward = 0. else: reward = self.assessments[-1] - self.assessments[-2] + STEP_REWARD # reward = assess if not done: # reward *= CCT_CHANGE_RATIO loads = self.power.data['load'] load_p = np.sum(loads.loc[loads['mark'] == 1, 'p0']) if abs(load_p - self.init_load_p) / self.init_load_p >= LOAD_CHANGE_THR: reward += PF_LOADFULL_REWARD done = True else: reward = assess return state, reward, done
def random_generate(base_path, fmt, size, out_path, min_p=None, max_p=None, gl_ratio=0.9, random_q0=True, random_open=False, open_prob=[0.8]): power = Power(fmt=fmt) power.load_power(base_path, fmt=fmt) generators_bak = power.data['generator'].copy() loads_bak = power.data['load'].copy() if random_open: aclines_bak = power.data['acline'].copy() min_p = np.sum(generators_bak['pmin']) if not min_p else min_p max_p = np.sum(generators_bak['pmax']) if not max_p else max_p p0 = np.sum(generators_bak['p0']) shutil.rmtree(out_path, ignore_errors=True) os.mkdir(out_path) conv_count = 0 for i in range(size): generators = power.data['generator'] = generators_bak.copy() loads = power.data['load'] = loads_bak.copy() if random_open: power.data['acline'] = aclines_bak.copy() p = min_p + (max_p - min_p) * np.random.rand() distribute_generators_p(generators, p - p0, sigma=0.2) gen_p = np.sum(generators['p0']) load_p = np.sum(loads['p0']) distribute_loads_p(loads, gl_ratio * gen_p - load_p, p_sigma=0.2, keep_factor=False) if random_q0: random_load_q0(loads, sigma=None) if random_open: open_num = np.sum(np.random.rand(1) > open_prob) random_open_acline(power, num=open_num) path = os.path.join(out_path, '%08d' % i) power.save_power(path, fmt, lf=True, lp=False, st=True) shutil.copy(os.path.join(base_path, 'LF.L0'), path) shutil.copy(os.path.join(base_path, 'ST.S0'), path) call_wmlf(path) if check_lfcal(path): conv_count += 1 print('Random generate done: %d / %d' % (conv_count, size))