Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rapid opf local bh #13

Draft
wants to merge 21 commits into
base: basic_opf_extension
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions 00_use-case/auxfuns/mpc_data.m
Original file line number Diff line number Diff line change
Expand Up @@ -152,5 +152,16 @@
2 4 64 10;
2 5 837 8;
];
elseif strcmp(casefile, '18')

mpc.trans = ext2int(loadcase('case9'));
mpc.dist = { ext2int(loadcase('case9'))};
% region 1 - region 2
mpc.connection_array = [
% region 1 - region 2
1 2 2 1

];
end

end
109 changes: 99 additions & 10 deletions 00_use-case/getting_started_opf.m
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,35 @@
mpc.fields_to_merge = {'bus', 'gen', 'branch', 'gencost'};

mpc_temp = loadcase('case5');
mpc_temp.gencost(:, 4) = 3;
mpc_temp.gencost(:, 6:7) = rand();
%mpc.fields_to_merge = {'bus', 'gen', 'branch'};
mpc.trans = mpc_temp;
mpc.dist = { mpc_temp;
mpc_temp
};
% mpc_temp.gencost(:, 4) = 3;
% mpc_temp.gencost(:, 6:7) = rand();

% remove limits in casefile
% generator limits
% upper bounds

mpc.connection_array = [ 1 2 1 5;
2 3 4 1];
% line flow limits
% upper bounds
% mpc_temp.branch(:, 6) = 0;

%mpc.fields_to_merge = {'bus', 'gen', 'branch'};
% mpc.trans = mpc_temp;
% mpc.dist = { mpc_temp;
% mpc_temp
% };
%
% mpc.connection_array = [ 1 2 1 5;
% 2 3 4 1];
%

mpc.trans = ext2int(loadcase('case9'));
mpc.dist = { ext2int(loadcase('case9'))};
% region 1 - region 2
mpc.connection_array = [
% region 1 - region 2
1 2 2 1

];

% compatibility tests
% Opf data is provided ??
Expand Down Expand Up @@ -90,16 +109,86 @@
%% setup distributed opf
% start values from pf

%% solve pf

mpc_split = run_case_file_splitter(mpc_merge, conn, names);
problem_type = 'feasibility';
% generate distributed problem
problem = generate_distributed_pf_for_aladin(mpc_split, names, problem_type);
problem.solver = 'fmincon';


opts = struct('maxiter',50, 'solveQP','quadprog');
opts.reg ='false';
opts.rho0= 1e2;
%
% % opts.regParam = 1e-12;
% [xsol_aladin, xsol_stack_aladin, mpc_sol_aladin, logg] = solve_distributed_problem_with_aladin(mpc_split, problem, names, opts);
%%
% comparison_aladin = compare_results(xval, xsol_aladin)
% violation = compare_constraints_violation(problem, logg);
%%
% [a,b,c] = compare_power_flow_between_regions(mpc_sol_aladin, mpc_merge.connections, mpc_split.regions, conn(:,1:2));
%%
% deviation = deviation_violation_iter_plot(mpc_split, xval, logg, names, xsol_aladin);


%% solve opf problem




% generate distributed problem
problem = generate_distributed_opf_for_aladin(mpc_split, names, 'feasibility');

%
% option = AladinOption;
% option.iter_max = 15;
% option.tol = 1e-8;
% option.mu0 = 1e3;
% option.rho0 = 1e2;
% option.nlp = NLPoption;
% option.nlp.solver = 'casadi';
% option.nlp.iter_display = true;
% option.nlp.active_set = true;
% option.qp = QPoption;
% % option.qp.regularization_hess = true;
% % option.qp.solver = 'lsqminnorm';
% % option.qp.solver = 'lsqlin';
% option.qp.solver = 'casadi';
%
%
%
%
% for i = 1 : length(problem.AA)
% local_funs = originalFuns(problem.locFuns.ffi{i}, [], [], problem.AA{i}, [], [], problem.locFuns.ggi{i}, [], problem.locFuns.hhi{i}, []);
% nlps(i) = localNLP(local_funs,option.nlp,problem.llbx{i},problem.uubx{i});
% end
% [xopt,logg] = run_aladin_algorithm(nlps,problem.zz0,problem.lam0,horzcat(problem.AA(:)),problem.b,option);
%
%
%
%
% for i = 1:Nregion
%
% local_funs = originalFuns(fi{i}, [], [], AA{i}, [], [], con_eq{i}, [], con_ineq{i}, []);
%
% nlps(i) = localNLP(local_funs,option.nlp,lbx{i},ubx{i});
%
% end
%
% [xopt,logg] = run_aladin_algorithm(nlps,x0,lam0,A,b,option);



problem.solver = 'fmincon';
% [xval, xval_stacked] = validate_distributed_problem_formulation(problem, mpc_split, names);

% solve distributed ALADIN

opts = struct('maxiter',50);
opts = struct('maxiter',50, 'solveQP','quadprog');
opts.reg ='false';
opts.rho0= 1e2;
opts.maxiter = 50;

[xsol_aladin, xsol_stack_aladin, mpc_sol_aladin, logg] = solve_distributed_problem_with_aladin(mpc_split, problem, names, opts);
34 changes: 34 additions & 0 deletions 00_use-case/mini_example.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
mpc = loadcase('case3_personalized');
results_pf = runpf('case3_personalized');
results_opf = runopf('case3_personalized');

[f, df, d2f] = opf_costfcn(results_opf.x, results_opf.om);
[h, g, dh, dg] = opf_consfcn(results_opf.x, results_opf.om);

names = generate_name_struct();
mpc.fields_to_merge = {'bus', 'gen', 'branch', 'gencost'};

mpc_temp = loadcase('case3_personalized');
mpc.trans = mpc_temp;
mpc.dist = { mpc_temp;};

mpc.connection_array = [ 1 2 3 3];

fields_to_merge = mpc.fields_to_merge;
connection_array = mpc.connection_array;


trafo_params.r = 0;
trafo_params.x = 0.00623;
trafo_params.b = 0;
trafo_params.ratio = 0.985;
trafo_params.angle = 0;

conn = build_connection_table(connection_array, trafo_params);
Nconnections = height(conn);

%% main
% case-file-generator
mpc_merge = run_case_file_generator(mpc.trans, mpc.dist, conn, fields_to_merge, names);
mpc_split = run_case_file_splitter(mpc_merge, conn, names);

28 changes: 15 additions & 13 deletions 00_use-case/mpc_merge.m
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@
7 1 300 98.61 0 0 1 1 0 230 1 1.1 0.9;
8 2 300 98.61 0 0 1 1 0 230 1 1.1 0.9;
9 2 400 131.47 0 0 1 1 0 230 1 1.1 0.9;
10 1 0 0 0 0 1 1.1 0 230 1 1.1 0.9;
11 1 0 0 0 0 1 1 0 230 1 1.1 0.9;
10 1 0 0 0 0 1 1 0 230 1 1.1 0.9;
11 2 0 0 0 0 1 1 0 230 1 1.1 0.9;
12 1 300 98.61 0 0 1 1 0 230 1 1.1 0.9;
13 2 300 98.61 0 0 1 1 0 230 1 1.1 0.9;
14 2 400 131.47 0 0 1 1 0 230 1 1.1 0.9;
15 2 0 0 0 0 1 1 0 230 1 1.1 0.9;
15 1 0 0 0 0 1 1 0 230 1 1.1 0.9;
];

%% generator data
Expand All @@ -35,39 +35,40 @@
1 170 0 127.5 -127.5 1 100 1 170 0 0 0 0 0 0 0 0 0 0 0 0;
3 323.49 0 390 -390 1 100 1 520 0 0 0 0 0 0 0 0 0 0 0 0;
4 0 0 150 -150 1 100 1 200 0 0 0 0 0 0 0 0 0 0 0 0;
5 466.51 0 450 -450 1.1 100 1 600 0 0 0 0 0 0 0 0 0 0 0 0;
5 466.51 0 450 -450 1 100 1 600 0 0 0 0 0 0 0 0 0 0 0 0;
6 40 0 30 -30 1 100 1 40 0 0 0 0 0 0 0 0 0 0 0 0;
6 170 0 127.5 -127.5 1 100 1 170 0 0 0 0 0 0 0 0 0 0 0 0;
8 323.49 0 390 -390 1 100 1 520 0 0 0 0 0 0 0 0 0 0 0 0;
9 0 0 150 -150 1 100 1 200 0 0 0 0 0 0 0 0 0 0 0 0;
11 40 0 30 -30 1 100 1 40 0 0 0 0 0 0 0 0 0 0 0 0;
11 170 0 127.5 -127.5 1 100 1 170 0 0 0 0 0 0 0 0 0 0 0 0;
13 323.49 0 390 -390 1 100 1 520 0 0 0 0 0 0 0 0 0 0 0 0;
14 0 0 150 -150 1 100 1 200 0 0 0 0 0 0 0 0 0 0 0 0;
15 466.51 0 450 -450 1.1 100 1 600 0 0 0 0 0 0 0 0 0 0 0 0;
];

%% branch data
% fbus tbus r x b rateA rateB rateC ratio angle status angmin angmax
mpc.branch = [
1 2 0.00281 0.0281 0.00712 400 400 400 0 0 1 -360 360;
1 2 0.00281 0.0281 0.00712 0 0 0 0 0 1 -360 360;
1 4 0.00304 0.0304 0.00658 0 0 0 0 0 1 -360 360;
1 5 0.00064 0.0064 0.03126 0 0 0 0 0 1 -360 360;
2 3 0.00108 0.0108 0.01852 0 0 0 0 0 1 -360 360;
3 4 0.00297 0.0297 0.00674 0 0 0 0 0 1 -360 360;
4 5 0.00297 0.0297 0.00674 240 240 240 0 0 1 -360 360;
6 7 0.00281 0.0281 0.00712 400 400 400 0 0 1 -360 360;
4 5 0.00297 0.0297 0.00674 0 0 0 0 0 1 -360 360;
6 7 0.00281 0.0281 0.00712 0 0 0 0 0 1 -360 360;
6 9 0.00304 0.0304 0.00658 0 0 0 0 0 1 -360 360;
6 10 0.00064 0.0064 0.03126 0 0 0 0 0 1 -360 360;
7 8 0.00108 0.0108 0.01852 0 0 0 0 0 1 -360 360;
8 9 0.00297 0.0297 0.00674 0 0 0 0 0 1 -360 360;
9 10 0.00297 0.0297 0.00674 240 240 240 0 0 1 -360 360;
9 10 0.00297 0.0297 0.00674 0 0 0 0 0 1 -360 360;
1 10 0 0.00623 0 0 0 0 0.985 0 1 0 0;
11 12 0.00281 0.0281 0.00712 400 400 400 0 0 1 -360 360;
11 12 0.00281 0.0281 0.00712 0 0 0 0 0 1 -360 360;
11 14 0.00304 0.0304 0.00658 0 0 0 0 0 1 -360 360;
11 15 0.00064 0.0064 0.03126 0 0 0 0 0 1 -360 360;
12 13 0.00108 0.0108 0.01852 0 0 0 0 0 1 -360 360;
13 14 0.00297 0.0297 0.00674 0 0 0 0 0 1 -360 360;
14 15 0.00297 0.0297 0.00674 240 240 240 0 0 1 -360 360;
9 11 0 0.00623 0 0 0 0 0.985 0 1 0 0;
14 15 0.00297 0.0297 0.00674 0 0 0 0 0 1 -360 360;
6 15 0 0.00623 0 0 0 0 0.985 0 1 0 0;
];

%%----- OPF Data -----%%
Expand All @@ -84,7 +85,8 @@
2 0 0 2 15 0;
2 0 0 2 30 0;
2 0 0 2 40 0;
2 0 0 2 14 0;
2 0 0 2 15 0;
2 0 0 2 30 0;
2 0 0 2 40 0;
2 0 0 2 10 0;
];
28 changes: 15 additions & 13 deletions 00_use-case/mpc_merge_split.m
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@
7 1 300 98.61 0 0 1 1 0 230 1 1.1 0.9;
8 2 300 98.61 0 0 1 1 0 230 1 1.1 0.9;
9 2 400 131.47 0 0 1 1 0 230 1 1.1 0.9;
10 1 0 0 0 0 1 1.1 0 230 1 1.1 0.9;
11 1 0 0 0 0 1 1 0 230 1 1.1 0.9;
10 1 0 0 0 0 1 1 0 230 1 1.1 0.9;
11 2 0 0 0 0 1 1 0 230 1 1.1 0.9;
12 1 300 98.61 0 0 1 1 0 230 1 1.1 0.9;
13 2 300 98.61 0 0 1 1 0 230 1 1.1 0.9;
14 2 400 131.47 0 0 1 1 0 230 1 1.1 0.9;
15 2 0 0 0 0 1 1 0 230 1 1.1 0.9;
15 1 0 0 0 0 1 1 0 230 1 1.1 0.9;
];

%% generator data
Expand All @@ -35,39 +35,40 @@
1 170 0 127.5 -127.5 1 100 1 170 0 0 0 0 0 0 0 0 0 0 0 0;
3 323.49 0 390 -390 1 100 1 520 0 0 0 0 0 0 0 0 0 0 0 0;
4 0 0 150 -150 1 100 1 200 0 0 0 0 0 0 0 0 0 0 0 0;
5 466.51 0 450 -450 1.1 100 1 600 0 0 0 0 0 0 0 0 0 0 0 0;
5 466.51 0 450 -450 1 100 1 600 0 0 0 0 0 0 0 0 0 0 0 0;
6 40 0 30 -30 1 100 1 40 0 0 0 0 0 0 0 0 0 0 0 0;
6 170 0 127.5 -127.5 1 100 1 170 0 0 0 0 0 0 0 0 0 0 0 0;
8 323.49 0 390 -390 1 100 1 520 0 0 0 0 0 0 0 0 0 0 0 0;
9 0 0 150 -150 1 100 1 200 0 0 0 0 0 0 0 0 0 0 0 0;
11 40 0 30 -30 1 100 1 40 0 0 0 0 0 0 0 0 0 0 0 0;
11 170 0 127.5 -127.5 1 100 1 170 0 0 0 0 0 0 0 0 0 0 0 0;
13 323.49 0 390 -390 1 100 1 520 0 0 0 0 0 0 0 0 0 0 0 0;
14 0 0 150 -150 1 100 1 200 0 0 0 0 0 0 0 0 0 0 0 0;
15 466.51 0 450 -450 1.1 100 1 600 0 0 0 0 0 0 0 0 0 0 0 0;
];

%% branch data
% fbus tbus r x b rateA rateB rateC ratio angle status angmin angmax
mpc.branch = [
1 2 0.00281 0.0281 0.00712 400 400 400 0 0 1 -360 360;
1 2 0.00281 0.0281 0.00712 0 0 0 0 0 1 -360 360;
1 4 0.00304 0.0304 0.00658 0 0 0 0 0 1 -360 360;
1 5 0.00064 0.0064 0.03126 0 0 0 0 0 1 -360 360;
2 3 0.00108 0.0108 0.01852 0 0 0 0 0 1 -360 360;
3 4 0.00297 0.0297 0.00674 0 0 0 0 0 1 -360 360;
4 5 0.00297 0.0297 0.00674 240 240 240 0 0 1 -360 360;
6 7 0.00281 0.0281 0.00712 400 400 400 0 0 1 -360 360;
4 5 0.00297 0.0297 0.00674 0 0 0 0 0 1 -360 360;
6 7 0.00281 0.0281 0.00712 0 0 0 0 0 1 -360 360;
6 9 0.00304 0.0304 0.00658 0 0 0 0 0 1 -360 360;
6 10 0.00064 0.0064 0.03126 0 0 0 0 0 1 -360 360;
7 8 0.00108 0.0108 0.01852 0 0 0 0 0 1 -360 360;
8 9 0.00297 0.0297 0.00674 0 0 0 0 0 1 -360 360;
9 10 0.00297 0.0297 0.00674 240 240 240 0 0 1 -360 360;
9 10 0.00297 0.0297 0.00674 0 0 0 0 0 1 -360 360;
1 10 0 0.00623 0 0 0 0 0.985 0 1 0 0;
11 12 0.00281 0.0281 0.00712 400 400 400 0 0 1 -360 360;
11 12 0.00281 0.0281 0.00712 0 0 0 0 0 1 -360 360;
11 14 0.00304 0.0304 0.00658 0 0 0 0 0 1 -360 360;
11 15 0.00064 0.0064 0.03126 0 0 0 0 0 1 -360 360;
12 13 0.00108 0.0108 0.01852 0 0 0 0 0 1 -360 360;
13 14 0.00297 0.0297 0.00674 0 0 0 0 0 1 -360 360;
14 15 0.00297 0.0297 0.00674 240 240 240 0 0 1 -360 360;
9 11 0 0.00623 0 0 0 0 0.985 0 1 0 0;
14 15 0.00297 0.0297 0.00674 0 0 0 0 0 1 -360 360;
6 15 0 0.00623 0 0 0 0 0.985 0 1 0 0;
];

%%----- OPF Data -----%%
Expand All @@ -84,7 +85,8 @@
2 0 0 2 15 0;
2 0 0 2 30 0;
2 0 0 2 40 0;
2 0 0 2 14 0;
2 0 0 2 15 0;
2 0 0 2 30 0;
2 0 0 2 40 0;
2 0 0 2 10 0;
];
37 changes: 37 additions & 0 deletions 00_use-case/test_functions.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
results_pf = runpf('case3_personalized');
results_opf = runopf('case3_personalized');

[f, df, d2f] = opf_costfcn(results_opf.x, results_opf.om);

mpc = loadcase('case3_personalized');
names = generate_name_struct();

[mpc_opf, om, local_buses_to_remove, mpopt] = prepare_case_file(mpc, names);





[constraint_function, ~] = build_local_constraint_function(mpc_opf, om, mpopt);
%% cost function + cost gradient
[cost, grad_cost, hess_cost] = build_local_cost_function(om);
%% equalities + Jacobian
eq = @(x)get_eq_cons(x, constraint_function, inds);
eq_jac = @(x)get_eq_cons_jacobian(x, constraint_function, inds);
%% inequalities + Jacobian
[ineq, ineq_jac] = build_local_inequalities(constraint_function);

pf = eq(results_opf.x);
h = ineq(results_opf.x);

%% equalities
function g = get_eq_cons(x, gh_fcn, inds)
[~,g,~,~] = gh_fcn(x);
% remove power flow equations for all copy buses
g(inds) = [];
end

function dg = get_eq_cons_jacobian(x, gh_fcn, inds)
[~,~,~,dg] = gh_fcn(x);
dg(:, inds) = [];
end
Loading