From 51f6dfdc58e5b8a857e2927ab1a577882ce1c706 Mon Sep 17 00:00:00 2001 From: Hassan Date: Tue, 13 Mar 2018 17:31:14 -0600 Subject: [PATCH] supporting add(var) and add(constr) and cleaning up acopf and socopf, to merge with master --- examples/MINLP/Power/ACOPF/ACOPF_main.cpp | 81 +++++++-------------- examples/MINLP/Power/SOCOPF/SOCOPF_main.cpp | 26 +++---- include/gravity/model.h | 10 +++ src/model.cpp | 1 + 4 files changed, 49 insertions(+), 69 deletions(-) diff --git a/examples/MINLP/Power/ACOPF/ACOPF_main.cpp b/examples/MINLP/Power/ACOPF/ACOPF_main.cpp index 2637fd2d7..7644a37a4 100644 --- a/examples/MINLP/Power/ACOPF/ACOPF_main.cpp +++ b/examples/MINLP/Power/ACOPF/ACOPF_main.cpp @@ -82,8 +82,8 @@ int main (int argc, char * argv[]) /* Power generation variables */ var Pg("Pg", grid.pg_min, grid.pg_max); var Qg ("Qg", grid.qg_min, grid.qg_max); - ACOPF.add_var(Pg.in(grid.gens)); - ACOPF.add_var(Qg.in(grid.gens)); + ACOPF.add(Pg.in(grid.gens)); + ACOPF.add(Qg.in(grid.gens)); /* Power flow variables */ var Pf_from("Pf_from", grid.S_max); @@ -91,50 +91,26 @@ int main (int argc, char * argv[]) var Pf_to("Pf_to", grid.S_max); var Qf_to("Qf_to", grid.S_max); - ACOPF.add_var(Pf_from.in(grid.arcs)); - ACOPF.add_var(Qf_from.in(grid.arcs)); - ACOPF.add_var(Pf_to.in(grid.arcs)); - ACOPF.add_var(Qf_to.in(grid.arcs)); + ACOPF.add(Pf_from.in(grid.arcs)); + ACOPF.add(Qf_from.in(grid.arcs)); + ACOPF.add(Pf_to.in(grid.arcs)); + ACOPF.add(Qf_to.in(grid.arcs)); - vector rev, imv; - ifstream file("../nesta_case30_fsr__api.log", std::ifstream::in); - if(!file.is_open()) throw invalid_argument("Could not open file\n"); - string word; - for(int i = 0; i < 30; i++){ - file >> word; - rev.push_back(atof(word.c_str())); - cout << "\nrev[" << i << "] = " << atof(word.c_str()); - } - for(int i = 0; i < 30; i++){ - file >> word; - imv.push_back(atof(word.c_str())); - cout << "\nimv[" << i << "] = " << atof(word.c_str()); - } - - param t_min, t_max; - for(int i = 0; i < 30; i++){ - grid.v_min.set_val(grid.nodes[i]->_name, sqrt(rev[i]*rev[i] + imv[i]*imv[i])); - grid.v_max.set_val(grid.nodes[i]->_name, sqrt(rev[i]*rev[i] + imv[i]*imv[i])); - t_min.set_val(grid.nodes[i]->_name, atan(imv[i]/rev[i])); - t_max.set_val(grid.nodes[i]->_name, atan(imv[i]/rev[i])); - } /** Voltage related variables */ - var theta("theta", t_min, t_max); + var theta("theta"); var v("|V|", grid.v_min, grid.v_max); -// var vr("vr"); -// var vi("vi"); var vr("vr", grid.v_max); var vi("vi", grid.v_max); if (polar) { - ACOPF.add_var(v.in(grid.nodes)); - ACOPF.add_var(theta.in(grid.nodes)); + ACOPF.add(v.in(grid.nodes)); + ACOPF.add(theta.in(grid.nodes)); v.initialize_all(1); } else { - ACOPF.add_var(vr.in(grid.nodes)); - ACOPF.add_var(vi.in(grid.nodes)); + ACOPF.add(vr.in(grid.nodes)); + ACOPF.add(vi.in(grid.nodes)); vr.initialize_all(1.0); } @@ -152,7 +128,7 @@ int main (int argc, char * argv[]) else { Ref_Bus = vi(grid.get_ref_bus()); } - ACOPF.add_constraint(Ref_Bus == 0); + ACOPF.add(Ref_Bus == 0); /** KCL Flow conservation */ Constraint KCL_P("KCL_P"); @@ -168,8 +144,8 @@ int main (int argc, char * argv[]) KCL_P += grid.gs*(power(vr,2)+power(vi,2)); KCL_Q -= grid.bs*(power(vr,2)+power(vi,2)); } - ACOPF.add_constraint(KCL_P.in(grid.nodes) == 0); - ACOPF.add_constraint(KCL_Q.in(grid.nodes) == 0); + ACOPF.add(KCL_P.in(grid.nodes) == 0); + ACOPF.add(KCL_Q.in(grid.nodes) == 0); /** AC Power Flows */ /** TODO write the constraints in Complex form */ @@ -185,7 +161,7 @@ int main (int argc, char * argv[]) Flow_P_From -= grid.g_ft*(vr.from()*vr.to() + vi.from()*vi.to()); Flow_P_From -= grid.b_ft*(vi.from()*vr.to() - vr.from()*vi.to()); } - ACOPF.add_constraint(Flow_P_From.in(grid.arcs)==0); + ACOPF.add(Flow_P_From.in(grid.arcs)==0); Constraint Flow_P_To("Flow_P_To"); Flow_P_To += Pf_to; @@ -199,7 +175,7 @@ int main (int argc, char * argv[]) Flow_P_To -= grid.g_tf*(vr.from()*vr.to() + vi.from()*vi.to()); Flow_P_To -= grid.b_tf*(vi.to()*vr.from() - vr.to()*vi.from()); } - ACOPF.add_constraint(Flow_P_To.in(grid.arcs)==0); + ACOPF.add(Flow_P_To.in(grid.arcs)==0); Constraint Flow_Q_From("Flow_Q_From"); Flow_Q_From += Qf_from; @@ -213,7 +189,7 @@ int main (int argc, char * argv[]) Flow_Q_From += grid.b_ft*(vr.from()*vr.to() + vi.from()*vi.to()); Flow_Q_From -= grid.g_ft*(vi.from()*vr.to() - vr.from()*vi.to()); } - ACOPF.add_constraint(Flow_Q_From.in(grid.arcs)==0); + ACOPF.add(Flow_Q_From.in(grid.arcs)==0); Constraint Flow_Q_To("Flow_Q_To"); Flow_Q_To += Qf_to; if (polar) { @@ -226,21 +202,19 @@ int main (int argc, char * argv[]) Flow_Q_To += grid.b_tf*(vr.from()*vr.to() + vi.from()*vi.to()); Flow_Q_To -= grid.g_tf*(vi.to()*vr.from() - vr.to()*vi.from()); } - ACOPF.add_constraint(Flow_Q_To.in(grid.arcs)==0); + ACOPF.add(Flow_Q_To.in(grid.arcs)==0); /** AC voltage limit constraints. */ if (!polar) { Constraint Vol_limit_UB("Vol_limit_UB"); Vol_limit_UB = power(vr, 2) + power(vi, 2); Vol_limit_UB -= power(grid.v_max, 2); - ACOPF.add_constraint(Vol_limit_UB.in(grid.nodes) <= 0); + ACOPF.add(Vol_limit_UB.in(grid.nodes) <= 0); Constraint Vol_limit_LB("Vol_limit_LB"); Vol_limit_LB = power(vr, 2) + power(vi, 2); Vol_limit_LB -= power(grid.v_min,2); - ACOPF.add_constraint(Vol_limit_LB.in(grid.nodes) >= 0); - DebugOff(grid.v_min.to_str(true) << endl); - DebugOff(grid.v_max.to_str(true) << endl); + ACOPF.add(Vol_limit_LB.in(grid.nodes) >= 0); } @@ -253,8 +227,6 @@ int main (int argc, char * argv[]) PAD_UB -= grid.th_max; PAD_LB = theta.from() - theta.to(); PAD_LB -= grid.th_min; - DebugOff(grid.th_min.to_str(true) << endl); - DebugOff(grid.th_max.to_str(true) << endl); } else { DebugOff("Number of bus_pairs = " << bus_pairs.size() << endl); @@ -263,28 +235,25 @@ int main (int argc, char * argv[]) PAD_LB = vi.from()*vr.to() - vr.from()*vi.to(); PAD_LB -= grid.tan_th_min*(vr.from()*vr.to() + vi.from()*vi.to()); - DebugOff(grid.th_min.to_str(true) << endl); - DebugOff(grid.th_max.to_str(true) << endl); } - ACOPF.add_constraint(PAD_UB.in(bus_pairs) <= 0); - ACOPF.add_constraint(PAD_LB.in(bus_pairs) >= 0); + ACOPF.add(PAD_UB.in(bus_pairs) <= 0); + ACOPF.add(PAD_LB.in(bus_pairs) >= 0); /* Thermal Limit Constraints */ Constraint Thermal_Limit_from("Thermal_Limit_from"); Thermal_Limit_from += power(Pf_from, 2) + power(Qf_from, 2); Thermal_Limit_from -= power(grid.S_max, 2); - ACOPF.add_constraint(Thermal_Limit_from.in(grid.arcs) <= 0); + ACOPF.add(Thermal_Limit_from.in(grid.arcs) <= 0); Constraint Thermal_Limit_to("Thermal_Limit_to"); Thermal_Limit_to += power(Pf_to, 2) + power(Qf_to, 2); Thermal_Limit_to -= power(grid.S_max,2); - ACOPF.add_constraint(Thermal_Limit_to.in(grid.arcs) <= 0); - DebugOff(grid.S_max.in(grid.arcs).to_str(true) << endl); + ACOPF.add(Thermal_Limit_to.in(grid.arcs) <= 0); solver OPF(ACOPF,ipopt); double solver_time_start = get_wall_time(); - OPF.run(output, relax = false, tol = 1e-4, "ma27", mehrotra = "no"); + OPF.run(output, relax = false, tol = 1e-6, "ma27", mehrotra = "no"); double solver_time_end = get_wall_time(); double total_time_end = get_wall_time(); auto solve_time = solver_time_end - solver_time_start; diff --git a/examples/MINLP/Power/SOCOPF/SOCOPF_main.cpp b/examples/MINLP/Power/SOCOPF/SOCOPF_main.cpp index 592f213a7..9eb034c49 100644 --- a/examples/MINLP/Power/SOCOPF/SOCOPF_main.cpp +++ b/examples/MINLP/Power/SOCOPF/SOCOPF_main.cpp @@ -171,19 +171,19 @@ int main (int argc, char * argv[]) SOCP.add(Thermal_Limit_to.in(grid.arcs)); /* Lifted Nonlinear Cuts */ -// Constraint LNC1("LNC1"); -// LNC1 += (grid.v_min.from()+grid.v_max.from())*(grid.v_min.to()+grid.v_max.to())*(grid.sphi*Im_Wij + grid.cphi*R_Wij); -// LNC1 -= grid.v_max.to()*grid.cos_d*(grid.v_min.to()+grid.v_max.to())*Wii.from(); -// LNC1 -= grid.v_max.from()*grid.cos_d*(grid.v_min.from()+grid.v_max.from())*Wii.to(); -// LNC1 -= grid.v_max.from()*grid.v_max.to()*grid.cos_d*(grid.v_min.from()*grid.v_min.to() - grid.v_max.from()*grid.v_max.to()); -//// SOCP.add(LNC1.in(bus_pairs) >= 0); -// -// Constraint LNC2("LNC2"); -// LNC2 += (grid.v_min.from()+grid.v_max.from())*(grid.v_min.to()+grid.v_max.to())*(grid.sphi*Im_Wij + grid.cphi*R_Wij); -// LNC2 -= grid.v_min.to()*grid.cos_d*(grid.v_min.to()+grid.v_max.to())*Wii.from(); -// LNC2 -= grid.v_min.from()*grid.cos_d*(grid.v_min.from()+grid.v_max.from())*Wii.to(); -// LNC2 += grid.v_min.from()*grid.v_min.to()*grid.cos_d*(grid.v_min.from()*grid.v_min.to() - grid.v_max.from()*grid.v_max.to()); -//// SOCP.add(LNC2.in(bus_pairs) >= 0); + Constraint LNC1("LNC1"); + LNC1 += (grid.v_min.from()+grid.v_max.from())*(grid.v_min.to()+grid.v_max.to())*(grid.sphi*Im_Wij + grid.cphi*R_Wij); + LNC1 -= grid.v_max.to()*grid.cos_d*(grid.v_min.to()+grid.v_max.to())*Wii.from(); + LNC1 -= grid.v_max.from()*grid.cos_d*(grid.v_min.from()+grid.v_max.from())*Wii.to(); + LNC1 -= grid.v_max.from()*grid.v_max.to()*grid.cos_d*(grid.v_min.from()*grid.v_min.to() - grid.v_max.from()*grid.v_max.to()); + SOCP.add(LNC1.in(bus_pairs) >= 0); + + Constraint LNC2("LNC2"); + LNC2 += (grid.v_min.from()+grid.v_max.from())*(grid.v_min.to()+grid.v_max.to())*(grid.sphi*Im_Wij + grid.cphi*R_Wij); + LNC2 -= grid.v_min.to()*grid.cos_d*(grid.v_min.to()+grid.v_max.to())*Wii.from(); + LNC2 -= grid.v_min.from()*grid.cos_d*(grid.v_min.from()+grid.v_max.from())*Wii.to(); + LNC2 += grid.v_min.from()*grid.v_min.to()*grid.cos_d*(grid.v_min.from()*grid.v_min.to() - grid.v_max.from()*grid.v_max.to()); + SOCP.add(LNC2.in(bus_pairs) >= 0); /* Solver selection */ diff --git a/include/gravity/model.h b/include/gravity/model.h index 33b6c268d..0ee8b9b95 100755 --- a/include/gravity/model.h +++ b/include/gravity/model.h @@ -158,6 +158,11 @@ namespace gravity { } }; + template + void add(var& v){//Add variables by copy + add_var(v); + } + template void add_var(var&& v){//Add variables by copy @@ -200,6 +205,11 @@ namespace gravity { } }; + template + void add(var&& v){ + add_var(move(v)); + } + void del_var(const param_& v); diff --git a/src/model.cpp b/src/model.cpp index 1dd33b26e..34560bf88 100755 --- a/src/model.cpp +++ b/src/model.cpp @@ -232,6 +232,7 @@ void Model::del_param(const param_& v){ } }; + void Model::add(const Constraint& c){ add_constraint(c); }