Skip to content

Commit

Permalink
Add LS analytical solution, and find_BFS function
Browse files Browse the repository at this point in the history
  • Loading branch information
YairMZ committed Apr 18, 2020
1 parent 829e961 commit 067dcb7
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 0 deletions.
45 changes: 45 additions & 0 deletions LP/find_BFS.m
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

38 changes: 38 additions & 0 deletions LS/basic_LS.m
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)

0 comments on commit 067dcb7

Please sign in to comment.