-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add LS analytical solution, and find_BFS function
- Loading branch information
Showing
2 changed files
with
83 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
function [optimal_sol,optimal_cost,feasible_solutions] = find_BFS(A,b, c) | ||
%find_BFS Finds basic feasible soltions of standard form LP problem The function receives constraints of the form Ax=b, | ||
%and a column vector of representing the cost function. | ||
% - A - a matrix of equality constraints | ||
% - b - a column vetor of constants for the equation | ||
% - c - a column vetor of costs | ||
|
||
%choose columns subsets | ||
[subset_size, col_num] = size(A); | ||
subsets = nchoosek(1:col_num,subset_size); | ||
%find basic solutions | ||
basic_solutions = []; | ||
for ii = 1:size(subsets,1) %iterate over all subsets | ||
A_beta = A(:,subsets(ii,:)); | ||
if rank(A_beta) == subset_size %% if A_beta is invertible | ||
tmp = zeros(col_num,1); | ||
tmp(subsets(ii,:)) = A_beta\b; | ||
basic_solutions = [basic_solutions tmp]; | ||
end | ||
end | ||
if isempty(basic_solutions) | ||
error('no basic solutions found, check for errors in your constraints equation') | ||
end | ||
%feasability of basic solutions | ||
%verify no typos in A, by verfying equality constraints | ||
if max(max(abs(A*basic_solutions - repmat(b,1,size(basic_solutions,2))))) > 1e-5 | ||
warning('equality constraints violated') | ||
else | ||
disp('equality constraints satisfied') | ||
end | ||
%feasible solutions require only positive entries | ||
feasible_solutions = []; | ||
for ii = 1:size(basic_solutions,2) %iterate over all columns | ||
if min(basic_solutions(:,ii))>=0 | ||
feasible_solutions = [feasible_solutions basic_solutions(:,ii)]; | ||
end | ||
end | ||
if isempty(feasible_solutions) | ||
error('no feasible solutions found') | ||
end | ||
costs = c'*feasible_solutions; | ||
[optimal_cost, optimal_idx] = min(costs); | ||
optimal_sol = feasible_solutions(:,optimal_idx); | ||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
%% reference and blurring | ||
clear | ||
close all | ||
|
||
pic = imread('tree.jpg'); | ||
[A,y,X] = blurring(pic); | ||
|
||
[n,m] = size(X); | ||
blurred_pic = reshape(y, n, m); | ||
|
||
figure(1) | ||
imshow(X) | ||
title('Reference picture') | ||
%saveas(gcf,'Reference.eps','epsc') | ||
%% blurred picture | ||
|
||
figure(2) | ||
imshow(blurred_pic) | ||
title('Blurred picture') | ||
%saveas(gcf,'Blurred.eps','epsc') | ||
%% LS | ||
x_LS = (A'* A)\(A'*y); | ||
|
||
figure(3) | ||
imshow(reshape(x_LS, n, m)) | ||
title('LS reconstructed picture') | ||
%saveas(gcf,'LS.eps','epsc') | ||
%% RLS | ||
lambda = 5*1e-4; %required some trial and error | ||
x_RLS = (A'* A + lambda*speye(size(A'* A,1)) )\(A'*y); | ||
|
||
figure(4) | ||
imshow(reshape(x_RLS, n, m)) | ||
title('RLS reconstructed picture, \lambda = 5*1e-4') | ||
%saveas(gcf,'RLS.eps','epsc') | ||
%% MSE | ||
LS_MSE = norm(x_LS - X(:))^2 / length(x_LS) | ||
RLS_MSE = norm(x_RLS - X(:))^2 / length(x_RLS) |