123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291 |
- function [D_full, w_fin] = NLP_beamlet_optimizer
- fprintf('starting NLP optimization process: ')
- patient_dir = 'C:\010-work\003_localGit\WiscPlan_v2\data\PatientData';
- load([patient_dir '\matlab_files\Geometry.mat'])
- beamlet_batch_filename = [patient_dir '\beamlet_batch_files\' 'beamletbatch0.bin'];
- beamlet_cell_array = read_ryan_beamlets(beamlet_batch_filename, 'ryan');
- fprintf('.')
- numVox = numel(Geometry.data);
- numBeam = size(beamlet_cell_array,2);
- fprintf('.')
- beamlets = sparse(numVox, numBeam);
- fprintf('.')
- wbar1 = waitbar(0, 'Creating beamlet array');
- for beam_i=1:numBeam
-
- idx=beamlet_cell_array{1, beam_i}.non_zero_indices;
- beamlets(idx, beam_i) = 1000*beamlet_cell_array{1, beam_i}.non_zero_values;
- waitbar(beam_i/numBeam)
- end
- close(wbar1)
- fprintf('.')
- optGoal = make_ROI_goals(Geometry, beamlets);
- w0 = ones(numBeam,1);
- w0 = mean(optGoal{1}.target ./ (optGoal{1}.beamlets_pruned*w0)) * w0;
- RO_params=0;
- optGoal = make_robust_optGoal(optGoal, RO_params);
- fun = @(x) get_penalty(x, optGoal);
- A = [];
- b = [];
- Aeq = [];
- beq = [];
- lb = zeros(1, numBeam);
- ub = [];
- nonlcon = [];
- options = optimoptions('fmincon');
- options.MaxFunctionEvaluations = 30000;
- options.Display = 'iter';
- options.PlotFcn = 'optimplotfval';
- fprintf('-')
- tic
- w_fin = fmincon(fun,w0,A,b,Aeq,beq,lb,ub,nonlcon,options);
- t=toc;
- disp(['Optimization run time = ',num2str(t)]);
- D_full = reshape(beamlets * w_fin, size(Geometry.data));
- NLP_result.dose = D_full;
- NLP_result.weights = w_fin;
- save('C:\010-work\003_localGit\WiscPlan_v2\data\PatientData\matlab_files\NLP_result.mat', 'NLP_result');
- optGoal_idx=[1,3];
- plot_DVH(D_full, optGoal, optGoal_idx)
- end
- function penalty = get_penalty(x, optGoal)
-
-
-
- NumScenarios = optGoal{1}.NbrRandScenarios * optGoal{1}.NbrSystSetUpScenarios * optGoal{1}.NbrRangeScenarios;
- fobj = zeros(NumScenarios,1);
- sc_i = 1;
-
- for nrs_i = 1:optGoal{1}.NbrRandScenarios
- for sss_i = 1:optGoal{1}.NbrSystSetUpScenarios
- for rrs_i = 1:optGoal{1}.NbrRangeScenarios
- fobj(sc_i)=eval_f(x, optGoal, nrs_i, sss_i, rrs_i);
- sc_i = sc_i + 1;
- end
- end
- end
-
- penalty=max(fobj);
- end
- function penalty = eval_f(x, optGoal, nrs_i, sss_i, rrs_i)
- penalty = 0;
-
- for goal_i = 1:numel(optGoal)
- switch optGoal{goal_i}.function
- case 'min'
-
- d_penalty = 1.0e0 * sum(max(0, ...
- (optGoal{goal_i}.nrs{nrs_i}.sss{sss_i}.rrs{rrs_i}.target) -...
- (optGoal{goal_i}.nrs{nrs_i}.sss{sss_i}.rrs{rrs_i}.beamlets_pruned * x)));
- case 'max'
-
- d_penalty = 1.0e0 * sum(max(0, ...
- (optGoal{goal_i}.nrs{nrs_i}.sss{sss_i}.rrs{rrs_i}.beamlets_pruned * x)-...
- (optGoal{goal_i}.nrs{nrs_i}.sss{sss_i}.rrs{rrs_i}.target)));
- case 'LeastSquare'
-
-
- d_penalty = 1.0e-1* sum(((...
- optGoal{goal_i}.nrs{nrs_i}.sss{sss_i}.rrs{rrs_i}.beamlets_pruned * x) - ...
- optGoal{goal_i}.nrs{nrs_i}.sss{sss_i}.rrs{rrs_i}.target).^2);
- end
- penalty = penalty + d_penalty * optGoal{goal_i}.opt_weight;
- end
- end
- function plot_DVH(dose, optGoal, optGoal_idx)
-
- nfrac=1;
-
- figure
- hold on
- for roi_idx=optGoal_idx
-
-
- [dvh dosebins] = get_dvh_data(dose,optGoal{roi_idx}.ROI_idx,nfrac);
- plot(dosebins, dvh,'Color', optGoal{roi_idx}.dvh_col,'LineStyle', '-','DisplayName', optGoal{roi_idx}.ROI_name);
- end
- hold off
-
- end
- function [dvh dosebins] = get_dvh_data(dose,roi_idx,nfrac);
-
- dosevec = dose(roi_idx);
-
-
- [pdf dosebins] = hist(dosevec, 999);
-
- pdf = pdf(dosebins >= 0);
- dosebins = dosebins(dosebins >= 0);
- dvh = fliplr(cumsum(fliplr(pdf))) / numel(dosevec) * 100;
-
- dosebins = [dosebins dosebins(end)+0.1];
- dvh = [dvh 0];
-
- end
- function optGoal = make_ROI_goals(Geometry, beamlets)
- optGoal={};
-
-
- goal_1.name = 'Target_min';
- goal_1.ROI_name = Geometry.ROIS{1, 2}.name;
- ROI_idx = Geometry.ROIS{1, 2}.ind;
- goal_1.ROI_idx = ROI_idx;
- goal_1.D_final = 60;
- goal_1.function = 'min';
- goal_1.beamlets_pruned = beamlets(ROI_idx, :);
- goal_1.target = ones(numel(ROI_idx), 1) * goal_1.D_final;
- goal_1.opt_weight = 1 / numel(ROI_idx);
- goal_1.dvh_col = [0.9, 0.2, 0.2];
-
- optGoal{end+1}=goal_1;
-
-
-
- goal_2.name = 'Target_max';
- goal_2.ROI_name = Geometry.ROIS{1, 2}.name;
- ROI_idx = Geometry.ROIS{1, 2}.ind;
- goal_2.ROI_idx = ROI_idx;
- goal_2.D_final = 65;
- goal_2.function = 'max';
- goal_2.beamlets_pruned = beamlets(ROI_idx, :);
- goal_2.target = ones(numel(ROI_idx), 1) * goal_2.D_final;
- goal_2.opt_weight = 1 / numel(ROI_idx);
- goal_2.dvh_col = [0.9, 0.2, 0.2];
-
- optGoal{end+1}=goal_2;
-
-
-
- goal_3.name = 'ROI 1_max';
- goal_3.ROI_name = Geometry.ROIS{1, 3}.name;
- ROI_idx = Geometry.ROIS{1, 3}.ind;
- goal_3.ROI_idx = ROI_idx;
- goal_3.D_final = 0;
- goal_3.function = 'LeastSquare';
- goal_3.beamlets_pruned = beamlets(ROI_idx, :);
- goal_3.target = ones(numel(ROI_idx), 1) * goal_3.D_final;
- goal_3.opt_weight = 1 / numel(ROI_idx);
- goal_3.dvh_col = [0.2, 0.9, 0.2];
-
- optGoal{end+1}=goal_3;
-
-
- end
- function optGoal = make_robust_optGoal(optGoal, RO_params);
-
-
-
-
-
- nrs_scene_list={[0,0,0]};
- sss_scene_list={[0,0,0], [5,5,5], [-5, -5, -5]};
- rrs_scene_list={[0,0,0]};
-
- for i = 1:numel(optGoal)
- optGoal{i}.NbrRandScenarios =numel(nrs_scene_list);
- optGoal{i}.NbrSystSetUpScenarios=numel(sss_scene_list);
- optGoal{i}.NbrRangeScenarios =numel(rrs_scene_list);
- end
-
- for goal_i = 1:numel(optGoal)
- out_01.beamlets_pruned = optGoal{goal_i}.beamlets_pruned;
- out_01.target = optGoal{goal_i}.target;
-
- for nrs_i = 1:optGoal{goal_i}.NbrRandScenarios
-
- out_02.beamlets_pruned = out_01.beamlets_pruned;
- out_02.target = out_01.target;
-
- for sss_i = 1:optGoal{goal_i}.NbrSystSetUpScenarios
-
- out_03 = get_RO_sss(out_02);
-
- for rrs_i = 1:optGoal{goal_i}.NbrRangeScenarios
-
- out_final.beamlets_pruned = out_03.beamlets_pruned;
- out_final.target = out_03.target;
-
- optGoal{goal_i}.nrs{nrs_i}.sss{sss_i}.rrs{rrs_i}.beamlets_pruned = optGoal{goal_i}.beamlets_pruned;
- optGoal{goal_i}.nrs{nrs_i}.sss{sss_i}.rrs{rrs_i}.target = optGoal{goal_i}.target;
- end
- end
- end
- end
- end
- function out_03 = get_RO_sss(out_02);
-
-
-
- out_03= out_02;
- end
|