function [xopt, fopt, stat] = mc_optimize(fitnessfct, N, lb, ub, stopeval, epsilon, param)
% [xopt, fopt, stat] = mc_method(fitnessfct, N, lb, ub, stopeval, param)
%
% mc_method:
%   implementation of a simple Monte-Carlo search for
%   N-dimensional continuous minimization problems
%
% Input:
%   fitnessfct     - a handle to the fitness function
%   N              - the number of dimensions
%   lb             - the lower bounds of the box constraints
%                    this is a vector of length N
%   ub             - the upper bounds of the box constraints
%                    this is a vector of length N
%   stopeval       - the number of function evaluations used
%                    by the optimization algorithm
%   epsilon        - not applicable here
%   param          - no specific parameters here
%
% Output:
%   xopt           - a vector containing the location of the optimum
%   fopt           - the objective function value of the optimum
%   stat           - a MATLAB struct containing algorithm
%                    specific output statistics, in this case
%                    these are:
%                      stat.f           - the current best f
%                                         of each iteration
%                      stat.x           - the current best solution
%                                         of each iteration
%

xopt = lb + (ub - lb) .* rand(N,1);
fopt = feval(fitnessfct, xopt');

for i=2:stopeval

	% Pick new sample point and evaluate
	x = lb + (ub - lb) .* rand(N,1);
	f = feval(fitnessfct, x');

	% If new point better than the old point then replace
	if (f <= fopt)
		xopt = x;
		fopt = f;
	end

	stat.f(i) = fopt;
	stat.x(:,i) = xopt;

	plot(stat.f)
	drawnow()
end
