Skip to content

Commit

Permalink
supporting add(var) and add(constr) and cleaning up acopf and socopf,…
Browse files Browse the repository at this point in the history
… to merge with master
  • Loading branch information
hhijazi committed Mar 13, 2018
1 parent 94771c5 commit 51f6dfd
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 69 deletions.
81 changes: 25 additions & 56 deletions examples/MINLP/Power/ACOPF/ACOPF_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,59 +82,35 @@ int main (int argc, char * argv[])
/* Power generation variables */
var<Real> Pg("Pg", grid.pg_min, grid.pg_max);
var<Real> 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<Real> Pf_from("Pf_from", grid.S_max);
var<Real> Qf_from("Qf_from", grid.S_max);
var<Real> Pf_to("Pf_to", grid.S_max);
var<Real> 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<double> 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<Real> 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<Real> theta("theta", t_min, t_max);
var<Real> theta("theta");
var<Real> v("|V|", grid.v_min, grid.v_max);
// var<Real> vr("vr");
// var<Real> vi("vi");
var<Real> vr("vr", grid.v_max);
var<Real> 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);
}

Expand All @@ -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");
Expand All @@ -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 */
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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) {
Expand All @@ -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);
}


Expand All @@ -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);
Expand All @@ -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;
Expand Down
26 changes: 13 additions & 13 deletions examples/MINLP/Power/SOCOPF/SOCOPF_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down
10 changes: 10 additions & 0 deletions include/gravity/model.h
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,11 @@ namespace gravity {
}
};

template <typename type>
void add(var<type>& v){//Add variables by copy
add_var(v);
}


template <typename type>
void add_var(var<type>&& v){//Add variables by copy
Expand Down Expand Up @@ -200,6 +205,11 @@ namespace gravity {
}
};

template <typename type>
void add(var<type>&& v){
add_var(move(v));
}

void del_var(const param_& v);


Expand Down
1 change: 1 addition & 0 deletions src/model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ void Model::del_param(const param_& v){
}
};


void Model::add(const Constraint& c){
add_constraint(c);
}
Expand Down

0 comments on commit 51f6dfd

Please sign in to comment.