-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalc_sens_bounds.m
109 lines (73 loc) · 2.62 KB
/
calc_sens_bounds.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
%% calculate differential sensitivity and differential sensitivity bounds
% SPDX-FileCopyrightText: Copyright (C) 2023 S Shermer <lw1660@gmail.com>, Swansea University
% SPDX-FileCopyrightText: Copyright (C) 2023 Sean P O'Neil <seanonei@usc.edu>, University of Southern California
%
% SPDX-License-Identifier: CC-BY-SA-4.0
% input
% prob - problem number
% tf - gate operation time
% K - number of control pulses
% algo - optimization algorithm identifier
% output
% "results" structure saved to "algorithm/results/problem_tf_K"
% subfields:
% error - nominal error
% sens - differential sensitivity to each perturbation structure
% bound2 - norm bound on differential sensitivity for static strucutre
% bound3 - bound on differential sensitivity for variable structure
% direction - uncertainty direction that maximizes "bound"
% X - cell array of K directions that maximizes "bound2"
% shouldn't need - Sol - controller data imported from
% "algorithm/controllers/problem_tf_K"
function calc_sens_bounds(prob,tf,K,algo)
switch algo
case 1, algorithm = 'quasi-newton';
case 2, algorithm = 'trust-region';
otherwise algorithm = 'trust-region';
end
if ~exist(append('results/',algorithm),'dir')
mkdir(append('results/',algorithm));
end
% load problem data
intag = sprintf('problems/problem%d',prob);
load(intag);
H = problem.H;
dH = problem.dH;
UT = problem.UT;
N = 2^problem.N;
% load controller data
infile = sprintf('controllers/problem%d_tf%d_K%d_%s.csv',prob,tf,K,algorithm);
C = csvread(infile);
num = size(C);
num = num(1,1);
% set save path
outfile = sprintf('results/%s/problem%d_tf%d_K%d',algorithm,prob,tf,K);
% loop to calcuate differential sensitivity and bounds for each controller
parfor ind = 1:num % use parfor for cluster processing
%for ind = 1:num
ind;
control = C(ind,:);
out{ind} = calc_sens_loop(ind,H,dH,N,UT,control);
end
for k = 1:num
error(k,1) = out{k}.err;
sens(k,:) = out{k}.sens;
bound2(k,:) = out{k}.bound2;
bound3(k,:) = out{k}.bound3;
direction(k,:) = out{k}.direction;
X{k} = out{k}.X;
end
results.err = error;
results.sens = sens;
results.bound2 = bound2;
results.bound3 = bound3;
results.direction = direction;
results.X = X;
results.C = C;
results.dHindex = out{end}.dHindex;
if ~exist(append('results/temp'),'dir')
mkdir(append('results/temp'));
end
% set save path
outfile = sprintf('results/temp/problem%d_tf%d_K%d_%s',prob,tf,K,algorithm);
save(outfile,'results');