1
0

NLP_beamlet_optimizer.m 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. % this is a simple example for a constrained NLP solver from matlabs
  2. % optimization toolbox
  3. function [D_full, w_fin] = NLP_beamlet_optimizer
  4. fprintf('starting NLP optimization process')
  5. % load the data
  6. patient_dir = 'C:\010-work\003_localGit\WiscPlan_v2\data\PatientData';
  7. load([patient_dir '\matlab_files\Geometry.mat'])
  8. beamlet_batch_filename = [patient_dir '\beamlet_batch_files\' 'beamletbatch0.bin'];
  9. beamlet_cell_array = read_ryan_beamlets(beamlet_batch_filename, 'ryan');
  10. fprintf('.')
  11. % set initial parameters
  12. targetROI_ind = 1;
  13. numVox = numel(Geometry.data);
  14. numBeam = size(beamlet_cell_array,2);
  15. ROI_idx=Geometry.ROIS{1, 2}.ind;
  16. fprintf('.')
  17. %% create input for optimization
  18. % ideal target values
  19. target = sparse(zeros(numVox,1));
  20. target(ROI_idx) = 60; % 60 units of dose to the target, 0 otherwise
  21. % create the map on how beamlets deliver the dose
  22. beamlets = sparse(zeros(numVox, numBeam));
  23. fprintf('.')
  24. wbar1 = waitbar(0, 'Creating beamlet array');
  25. for beam_i=1:numBeam
  26. % for each beam define how much dose it delivers on each voxel
  27. idx=beamlet_cell_array{1, beam_i}.non_zero_indices;
  28. beamlets(idx, beam_i) = 1000*beamlet_cell_array{1, beam_i}.non_zero_values;
  29. waitbar(beam_i/numBeam)
  30. end
  31. close(wbar1)
  32. fprintf('.')
  33. % prune the beamlets matrix only to areas of interest
  34. pruneIdx= find(target~=0);
  35. beamlets_pruned= beamlets(pruneIdx, :);
  36. target_pruned = full(target(pruneIdx));
  37. % initial beamlet weights
  38. w0 = ones(numBeam,1);
  39. w0 = mean(target_pruned./ (beamlets_pruned*w0)) * w0;
  40. % optimization function - least squares
  41. % fun = @(x) sum(((beamlets_pruned * x) - target_pruned).^2);
  42. fun = @(x) eval_f(x, beamlets_pruned, target_pruned);
  43. % all weights*-1 should be < 0 (b)
  44. A = [];
  45. b = [];
  46. Aeq = [];
  47. beq = [];
  48. lb = zeros(1, numBeam);
  49. ub = [];
  50. nonlcon = [];
  51. % define optimizer options
  52. options = optimoptions('fmincon');
  53. options.MaxFunctionEvaluations = 100000;
  54. options.Display = 'iter';
  55. fprintf('--')
  56. %% Run the optimization
  57. tic
  58. x = fmincon(fun,w0,A,b,Aeq,beq,lb,ub,nonlcon,options);
  59. t=toc;
  60. disp(['Optimization run time = ',num2str(t)]);
  61. %% evaluate the results
  62. D_full = reshape(beamlets * x, size(Geometry.data));
  63. w_fin=x;
  64. %% save outputs
  65. NLP_result.dose = D_full;
  66. NLP_result.weights = w_fin;
  67. % orthoslice(NLP_result.dose, [0,70])
  68. save('C:\010-work\003_localGit\WiscPlan_v2\data\PatientData\matlab_files\NLP_result.mat', 'NLP_result');
  69. end
  70. function penalty = eval_f(x, beamlets, target)
  71. % sum squares
  72. % penalty = sum(((beamlets * x) - target).^2);
  73. % at least>target
  74. penalty = 0;
  75. penalty = penalty + sum(max(0, target-(beamlets * x)));
  76. penalty = penalty + sum(max(0, (beamlets * x)-(target+5)));
  77. end