def create_problem(self,net): import pfnet # Parameters params = self._parameters thermal_limits = params['thermal_limits'] # Clear flags net.clear_flags() # Set flags net.set_flags('bus', 'variable', 'not slack', 'voltage angle') net.set_flags('generator', ['variable','bounded'], ['adjustable active power','not on outage'], 'active power') net.set_flags('load', ['variable','bounded'], 'adjustable active power', 'active power') if params['renewable_curtailment']: net.set_flags('variable generator', ['variable','bounded'], 'any', 'active power') try: num_gvar = len([g for g in net.generators if (not g.is_on_outage()) and g.is_P_adjustable()]) num_cur = net.num_var_generators if params['renewable_curtailment'] else 0 assert(net.num_bounded == (num_gvar+net.get_num_P_adjust_loads()+num_cur)*net.num_periods) assert(net.num_vars == (net.num_buses-net.get_num_slack_buses()+ num_gvar+net.get_num_P_adjust_loads()+ num_cur)*net.num_periods) except AssertionError: raise PFmethodError_BadProblem() # Set up problem problem = pfnet.Problem(net) problem.add_constraint(pfnet.Constraint('variable bounds',net)) problem.add_constraint(pfnet.Constraint('DC power balance',net)) if thermal_limits: problem.add_constraint(pfnet.Constraint('DC branch flow limits',net)) problem.add_function(pfnet.Function('generation cost',1.,net)) problem.add_function(pfnet.Function('consumption utility',-1.,net)) problem.analyze() # Return return problem
info = problem.solve(solver=optalg.opt_solver.OptSolverIpopt(), parameters={'tol': 1e-4}, fast_evaluator=True) print("OPTMOD", func.get_value()) print(info) print('time construction', time_construction) t = time.time() # PFNET and OPTALG net.set_flags('bus', 'variable', 'not slack', 'voltage angle') net.set_flags('bus', ['variable', 'bounded'], 'any', 'voltage magnitude') net.set_flags('generator', ['variable', 'bounded'], 'any', ['active power', 'reactive power']) p = pfnet.Problem(net) p.add_function(pfnet.Function('generation redispatch penalty', 1., net)) p.add_constraint(pfnet.Constraint('AC power balance', net)) p.add_constraint(pfnet.Constraint('variable bounds', net)) time_construction = time.time() - t t = time.time() p.analyze() p.show() time_transformation = time.time() - t t = time.time() solver = optalg.opt_solver.OptSolverIpopt()
def create_problem(self,net): import pfnet # Parameters params = self._parameters wcost = params['weight_cost'] wvmag = params['weight_vmag'] wvang = params['weight_vang'] wpq = params['weight_pq'] wt = params['weight_t'] wb = params['weight_b'] th = params['thermal_limits'] # Clear flags net.clear_flags() # Voltage magnitudes net.set_flags('bus', ['variable','bounded'], 'any', 'voltage magnitude') # Voltage angles net.set_flags('bus', 'variable', 'not slack', 'voltage angle') # Generator powers net.set_flags('generator', ['variable','bounded'], 'any', ['active power','reactive power']) try: assert(net.num_vars == (2*net.get_num_buses(True)-net.get_num_slack_buses(True) + 2*net.get_num_generators(True))*net.num_periods) assert(net.num_bounded == (2*net.get_num_generators(True) + net.get_num_buses(True))*net.num_periods) except AssertionError: raise PFmethodError_BadProblem() # Problem problem = pfnet.Problem(net) # Constraints problem.add_constraint(pfnet.Constraint('AC power balance',net)) problem.add_constraint(pfnet.Constraint('variable bounds',net)) if th == 'nonlinear': problem.add_constraint(pfnet.Constraint("AC branch flow limits",net)) elif th == 'linear': problem.add_constraint(pfnet.Constraint("linearized AC branch flow limits",net)) elif th == 'none': pass else: raise PFmethodError_BadParamValue('thermal_limits') # Functions problem.add_function(pfnet.Function('generation cost', wcost/max([net.get_num_generators(True),1.]),net)) if wvmag: problem.add_function(pfnet.Function('voltage magnitude regularization', wvmag/max([net.get_num_buses(True),1.]),net)) if wvang: problem.add_function(pfnet.Function('voltage angle regularization', wvang/max([net.get_num_buses(True),1.]),net)) if wpq: problem.add_function(pfnet.Function('generator powers regularization', wpq/max([net.get_num_generators(True),1.]),net)) if wt: problem.add_function(pfnet.Function('tap ratio regularization', wt/max([net.get_num_tap_changers(True),1.]),net)) if wb: problem.add_function(pfnet.Function('susceptance regularization', wb/max([net.get_num_switched_shunts(True),1.]),net)) problem.analyze() # Return return problem
def test_gen_cost(self): for case in test_cases.CASES: net = pf.Parser(case).parse(case) self.assertEqual(net.num_periods,1) # variables net.set_flags('generator', 'variable', 'any', 'active power') self.assertEqual(net.num_vars,net.num_generators) # pre contingency net.update_properties() gen_cost_base = net.gen_P_cost func = pf.Function('generation cost',1.,net) func.analyze() func.eval(net.get_var_values()) phi_base = func.phi gphi_base = func.gphi.copy() Hphi_base = func.Hphi.copy() self.assertEqual(phi_base,gen_cost_base) self.assertTupleEqual(gphi_base.shape,(net.num_vars,)) self.assertEqual(Hphi_base.nnz,net.num_vars) # gen outages counter = 0 for gen in net.generators: cont = pf.Contingency() cont.add_generator_outage(gen) cont.apply(net) func.del_matvec() func.analyze() func.eval(net.get_var_values()) net.update_properties() # value self.assertLess(np.abs(phi_base-gen.P_cost-func.phi),1e-8) self.assertLess(np.abs(gen_cost_base-gen.P_cost-net.gen_P_cost),1e-8) # grad gphi = func.gphi self.assertEqual(gphi[gen.index_P],0.) gphi[gen.index_P] = gphi_base[gen.index_P] self.assertLess(np.linalg.norm(gphi-gphi_base,np.inf),1e-8) # Hessian Hphi = func.Hphi self.assertTrue(np.all(Hphi.row != gen.index_P)) self.assertTrue(np.all(Hphi.col != gen.index_P)) cont.clear(net) counter += 1 if counter > TEST_GENS: break # branch outages counter = 0 for br in net.branches: if br.bus_k.degree == 1 or br.bus_m.degree == 1: continue cont = pf.Contingency() cont.add_branch_outage(br) cont.apply(net) func.del_matvec() func.analyze() func.eval(net.get_var_values()) net.update_properties() # value self.assertLess(np.abs(phi_base-func.phi),1e-8) self.assertLess(np.abs(gen_cost_base-net.gen_P_cost),1e-8) # grad self.assertLess(np.linalg.norm(func.gphi-gphi_base,np.inf),1e-8) # Hessian E = func.Hphi-Hphi_base self.assertEqual(E.nnz,0) cont.clear(net) counter += 1 if counter > TEST_BRANCHES: break
def create_problem(self, net): import pfnet # Parameters params = self._parameters wm = params['weight_vang'] wa = params['weight_vmag'] wp = params['weight_pq'] wt = params['weight_t'] wb = params['weight_b'] limit_gens = params['limit_gens'] lock_taps = params['lock_taps'] lock_shunts = params['lock_shunts'] solver_name = params['solver'] # Clear flags net.clear_flags() # OPT-based ########### if solver_name != 'nr': # Set up variables net.set_flags('bus', 'variable', 'not slack', ['voltage angle', 'voltage magnitude']) if not limit_gens: net.set_flags('bus', 'fixed', 'regulated by generator', 'voltage magnitude') net.set_flags('generator', 'variable', 'slack', 'active power') net.set_flags('generator', 'variable', 'regulator', 'reactive power') # Tap ratios if not lock_taps: net.set_flags('branch', 'variable', 'tap changer - v', 'tap ratio') # Shunt voltage control if not lock_shunts: net.set_flags('shunt', 'variable', 'switching - v', 'susceptance') try: num_vars = (2 * (net.num_buses - net.get_num_slack_buses()) + net.get_num_slack_gens() + net.get_num_reg_gens()) * net.num_periods if not lock_taps: num_vars += net.get_num_tap_changers_v() * net.num_periods if not lock_shunts: num_vars += net.get_num_switched_shunts() * net.num_periods assert (net.num_vars == num_vars) if limit_gens: assert (net.num_fixed == 0) else: assert (net.num_fixed == net.get_num_buses_reg_by_gen() * net.num_periods) except AssertionError: raise PFmethodError_BadProblem() # Set up problem problem = pfnet.Problem(net) problem.add_constraint(pfnet.Constraint('AC power balance', net)) problem.add_constraint( pfnet.Constraint('generator active power participation', net)) problem.add_constraint( pfnet.Constraint('generator reactive power participation', net)) problem.add_function( pfnet.Function('voltage magnitude regularization', wm / max([net.num_buses, 1.]), net)) problem.add_function( pfnet.Function('voltage angle regularization', wa / max([net.num_buses, 1.]), net)) problem.add_function( pfnet.Function('generator powers regularization', wp / max([net.num_generators, 1.]), net)) if limit_gens: problem.add_constraint( pfnet.Constraint('voltage regulation by generators', net)) else: problem.add_constraint(pfnet.Constraint( 'variable fixing', net)) if not lock_taps: problem.add_constraint( pfnet.Constraint('voltage regulation by transformers', net)) problem.add_function( pfnet.Function( 'tap ratio regularization', wt / max([net.get_num_tap_changers_v(), 1.]), net)) if not lock_shunts: problem.add_constraint( pfnet.Constraint('voltage regulation by shunts', net)) problem.add_function( pfnet.Function( 'susceptance regularization', wb / max([net.get_num_switched_shunts(), 1.]), net)) problem.analyze() # Return return problem # NR-based ########## elif solver_name == 'nr': # Voltages net.set_flags('bus', 'variable', 'not slack', ['voltage magnitude', 'voltage angle']) net.set_flags('bus', 'fixed', 'regulated by generator', 'voltage magnitude') # Gen active powers net.set_flags('generator', 'variable', 'slack', 'active power') # Gen reactive powers net.set_flags('generator', 'variable', 'regulator', 'reactive power') # Tap ratios net.set_flags('branch', ['variable', 'fixed'], 'tap changer - v', 'tap ratio') # Shunt susceptances net.set_flags('shunt', ['variable', 'fixed'], 'switching - v', 'susceptance') try: assert (net.num_vars == (2 * (net.num_buses - net.get_num_slack_buses()) + net.get_num_slack_gens() + net.get_num_reg_gens() + net.get_num_tap_changers_v() + net.get_num_switched_shunts()) * net.num_periods) assert (net.num_fixed == (net.get_num_buses_reg_by_gen() + net.get_num_tap_changers_v() + net.get_num_switched_shunts()) * net.num_periods) except AssertionError: raise PFmethodError_BadProblem() # Set up problem problem = pfnet.Problem(net) problem.add_constraint(pfnet.Constraint('AC power balance', net)) problem.add_constraint( pfnet.Constraint('generator active power participation', net)) problem.add_constraint( pfnet.Constraint('generator reactive power participation', net)) problem.add_constraint(pfnet.Constraint('variable fixing', net)) if limit_gens: problem.add_heuristic(pfnet.HEUR_TYPE_PVPQ) problem.analyze() # Return return problem # Invalid ######### else: raise PFmethodError_BadOptSolver()
def create_problem_opt(self, net): import pfnet # Parameters params = self._parameters wm = params['weight_vmag'] wa = params['weight_vang'] wp = params['weight_powers'] wc = params['weight_controls'] wv = params['weight_var'] wr = params['weight_redispatch'] v_limits = params['v_limits'] Q_mode = params['Q_mode'] Q_limits = params['Q_limits'] shunt_mode = params['shunt_mode'] shunt_limits = params['shunt_limits'] tap_mode = params['tap_mode'] tap_limits = params['tap_limits'] lock_vsc_P_dc = params['lock_vsc_P_dc'] lock_csc_P_dc = params['lock_csc_P_dc'] lock_csc_i_dc = params['lock_csc_i_dc'] vdep_loads = params['vdep_loads'] v_mag_warm_ref = params['v_mag_warm_ref'] gens_redispatch = params['gens_redispatch'] curtail_load_q = params['load_q_curtail'] # Check shunt options if shunt_mode not in [self.CONTROL_MODE_LOCKED, self.CONTROL_MODE_FREE, self.CONTROL_MODE_REG]: raise ValueError('invalid shunts mode') if shunt_mode == self.CONTROL_MODE_REG and not shunt_limits: raise ValueError('unsupported shunts configuration') # Check tap options if tap_mode not in [self.CONTROL_MODE_LOCKED, self.CONTROL_MODE_FREE, self.CONTROL_MODE_REG]: raise ValueError('invalid taps mode') if tap_mode == self.CONTROL_MODE_REG and not tap_limits: raise ValueError('unsupported taps configuration') # Check Q options if Q_mode not in [self.CONTROL_MODE_REG, self.CONTROL_MODE_FREE]: raise ValueError('invalid reactive power mode') # Clear flags net.clear_flags() # Buses net.set_flags('bus', 'variable', 'not slack', 'voltage angle') net.set_flags('bus', 'variable', 'any', 'voltage magnitude') if Q_mode == self.CONTROL_MODE_REG and not Q_limits: net.set_flags('bus', 'fixed', 'v set regulated', 'voltage magnitude') if v_limits: net.set_flags('bus', 'bounded', 'any', 'voltage magnitude') # Genertors if gens_redispatch: # Assume slack gens (excep renewables) are redispatchable net.set_flags('generator', ['variable', 'bounded'], 'redispatchable', 'active power') else: net.set_flags('generator', 'variable', 'slack', 'active power') net.set_flags('generator', 'variable', 'regulator', 'reactive power') if Q_mode == self.CONTROL_MODE_FREE and Q_limits: net.set_flags('generator', 'bounded', 'regulator', 'reactive power') # Loads if vdep_loads: for load in net.loads: if load.is_voltage_dependent() and load.is_in_service(): net.set_flags_of_component(load, 'variable', ['active power', 'reactive power']) if curtail_load_q: for load in net.loads: load.Q_min = np.minimum(load.Q, 0) load.Q_max = np.maximum(load.Q, 0) net.set_flags_of_component(load, ['variable','bounded'], 'reactive power') # VSC HVDC net.set_flags('vsc converter', 'variable', 'any', ['dc power', 'active power', 'reactive power']) if Q_mode == self.CONTROL_MODE_FREE and Q_limits: net.set_flags('vsc converter', 'bounded', 'any', 'reactive power') # CSC HVDC net.set_flags('csc converter', 'variable', 'any', ['dc power', 'active power', 'reactive power']) # DC buses net.set_flags('dc bus', 'variable', 'any', 'voltage') # FACTS net.set_flags('facts', 'variable', 'any', 'all') if Q_mode == self.CONTROL_MODE_FREE and Q_limits: net.set_flags('facts', 'bounded', 'any', 'reactive power') # Tap changers if tap_mode != self.CONTROL_MODE_LOCKED: net.set_flags('branch', 'variable', 'tap changer - v', 'tap ratio') if tap_mode == self.CONTROL_MODE_FREE and tap_limits: net.set_flags('branch', 'bounded', 'tap changer - v', 'tap ratio') # Swtiched shunts if shunt_mode != self.CONTROL_MODE_LOCKED: net.set_flags('shunt', 'variable', 'switching - v', 'susceptance') if shunt_mode == self.CONTROL_MODE_FREE and shunt_limits: net.set_flags('shunt', 'bounded', 'switching - v', 'susceptance') # Set up problem problem = pfnet.Problem(net) problem.add_constraint(pfnet.Constraint('AC power balance', net)) problem.add_constraint(pfnet.Constraint('HVDC power balance', net)) problem.add_constraint(pfnet.Constraint('generator active power participation', net)) problem.add_constraint(pfnet.Constraint('VSC converter equations', net)) problem.add_constraint(pfnet.Constraint('CSC converter equations', net)) problem.add_constraint(pfnet.Constraint('FACTS equations', net)) problem.add_constraint(pfnet.Constraint('VSC DC voltage control', net)) problem.add_constraint(pfnet.Constraint('CSC DC voltage control', net)) problem.add_constraint(pfnet.Constraint('power factor regulation', net)) if lock_vsc_P_dc: problem.add_constraint(pfnet.Constraint('VSC DC power control', net)) if lock_csc_P_dc: problem.add_constraint(pfnet.Constraint('CSC DC power control', net)) if lock_csc_i_dc: problem.add_constraint(pfnet.Constraint('CSC DC current control', net)) func = pfnet.Function('voltage magnitude regularization', wm/(net.get_num_buses(True)+1.), net) func.set_parameter('v_set_reference', not v_mag_warm_ref) problem.add_function(func) problem.add_function(pfnet.Function('variable regularization', wv/(net.num_vars+1.), net)) problem.add_function(pfnet.Function('voltage angle regularization', wa/(net.get_num_buses(True)+1.), net)) problem.add_function(pfnet.Function('generator powers regularization', wp/(net.get_num_generators(True)+1.), net)) problem.add_function(pfnet.Function('VSC DC power control', wc/(net.get_num_vsc_converters(True)+1.), net)) problem.add_function(pfnet.Function('CSC DC power control', wc/(net.get_num_csc_converters(True)+1.), net)) problem.add_function(pfnet.Function('CSC DC current control', wc/(net.get_num_csc_converters(True)+1.), net)) problem.add_function(pfnet.Function('FACTS active power control', wc/(net.get_num_facts(True)+1.), net)) problem.add_function(pfnet.Function('FACTS reactive power control', wc/(net.get_num_facts(True)+1.), net)) if gens_redispatch: problem.add_function(pfnet.Function('generation redispatch penalty', wr/(net.get_num_generators(True)+1.), net)) if Q_mode == self.CONTROL_MODE_REG and Q_limits: problem.add_constraint(pfnet.Constraint('voltage set point regulation', net)) if net.num_fixed > 0: problem.add_constraint(pfnet.Constraint('variable fixing', net)) if tap_mode != self.CONTROL_MODE_LOCKED: problem.add_function(pfnet.Function('tap ratio regularization', wc/(net.get_num_tap_changers_v(True)+1.), net)) if tap_mode == self.CONTROL_MODE_REG and tap_limits: problem.add_constraint(pfnet.Constraint('voltage regulation by transformers', net)) if shunt_mode != self.CONTROL_MODE_LOCKED: problem.add_function(pfnet.Function('susceptance regularization', wc/(net.get_num_switched_v_shunts(True)+1.), net)) if shunt_mode == self.CONTROL_MODE_REG and shunt_limits: problem.add_constraint(pfnet.Constraint('voltage regulation by shunts', net)) if vdep_loads: problem.add_constraint(pfnet.Constraint('load voltage dependence', net)) if net.num_bounded > 0: problem.add_constraint(pfnet.Constraint('variable bounds', net)) # Analyze problem.analyze() # Return return problem
# Optimization Problems - Functions import sys sys.path.append('.') import pfnet net = pfnet.Parser(sys.argv[1]).parse(sys.argv[1]) net.set_flags('bus', 'variable', 'any', 'voltage magnitude') print(net.num_vars == net.num_buses) func = pfnet.Function('voltage magnitude regularization',0.3,net) print(func.name == 'voltage magnitude regularization') print(func.weight) x = net.get_var_values() func.analyze() func.eval(x) print(x.shape) print(func.phi) print(type(func.gphi), func.gphi.shape)