From 067dcb75c493f68e19899484ee054b5da74071ee Mon Sep 17 00:00:00 2001 From: Yair M Date: Sat, 18 Apr 2020 18:54:45 +0300 Subject: [PATCH] Add LS analytical solution, and find_BFS function --- LP/find_BFS.m | 45 +++++++++++++++++++++++++++++++++++++++++++++ LS/basic_LS.m | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 83 insertions(+) create mode 100644 LP/find_BFS.m create mode 100644 LS/basic_LS.m diff --git a/LP/find_BFS.m b/LP/find_BFS.m new file mode 100644 index 0000000..6dd7c72 --- /dev/null +++ b/LP/find_BFS.m @@ -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 + diff --git a/LS/basic_LS.m b/LS/basic_LS.m new file mode 100644 index 0000000..6c60f29 --- /dev/null +++ b/LS/basic_LS.m @@ -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) \ No newline at end of file