Using factorial in optimiztion toolbox

Hello everyone,i am using genetic algorithm in optimization toolbox, my variables are integers and in my constraint part factorial of those variables are calculated
(like factorial(x(1))
When variables take value < 140 then optimization is done but when values of variables reaches 150 or more optimization results are not obtained i suppose due to very large quantity.
When i formulated my objective function using toolbox which can calculate factorial > 150 following error is showing
##Optimization running. Error running optimization. Constraint function must return real value.##
so can anyone please give me a solution to this problem that how to calculate factorial of variables which take value > 150! .
Please reply It will be really helpful for me, Thanking you in advance!

 Accepted Answer

Do you really need to know the factorial of those large numbers? Or would it be sufficient to know the logarithm of the numbers? I suggest that you use Stirling's formula (or this discussion) to approximate the logarithm of factorials:

log(n!) = n*log(n/e) + 1/2*log (2*pi*n)

Alan Weiss

MATLAB mathematical toolbox documentation

3 Comments

Firstly, Alan, I have to say thank you for your reply.

i am attaching my problem of optimization as a image file kindly look into it and provide me a way to solve it

There is nothing in what you just posted that requires that you use double precision. You can use the symbolic toolbox.
With x1 and x2 positive and x1 + 2*x2 <= 100, then you can see trivially that x1 <= 100 and x2 <= 50: any larger would violate the A,b constraint that you should have imposed. There is therefore no need to take factorial(150).

Sign in to comment.

More Answers (1)

Thank you so much for your prompt reply walter
This 100 is actually a constant value and which i am changing upto 1000, in that case x(1)<= 1000 and x(2)<= 500 and so there is need to take factorials(>150)

3 Comments

Then you need to use the symbolic toolbox.
Alternately:
after n = 50, 50^n rises more slowly than n! does. At some point 50^n/factorial(n) becomes less than 1; at some point it becomes less than eps. The point it becomes less than 1 is 133; the point it becomes less than eps is 166. Even if you had indefinite precision before the decimal point, nothing beyond 166 can affect the sum if it is done numerically. So you could stop by 133 at latest.
Thank you so much for your reply walter, really helpful for me
One last question that i will be writing my objective function and constraint part using symbolic toolbox, and i should not need to set x(1) and x(2) as integers and i need to use custom mutation and crossover for ensuring integer variable, right??
That sound right.
The result emitted by your objective function needs to be numeric, but your objective function is just @(x) x(3). For your nonlinear constraint functions you could use something like
temp1 = symsum(...);
temp2 = symsum(...);
temp13 = double(temp1) - double(x3)
if temp1 < x(3)
if temp13 == 0
%do not emit temp13 because
%that would lose enough precision that it would
%consider them to be the same
out(1) = ... something negative
else
out(1) = temp13
end
elseif temp1 > x(3)
if temp13 == 0
%do not emit temp13 because
%that would lose enough precision that it would
%consider them to be the same
out(1) = ... something positive
else
out(1) = temp13
end
else
out(1) = 0;
end

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!