Using factorial in optimiztion toolbox
Show older comments
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
More Answers (1)
Parikshit Sharma
on 17 Apr 2018
0 votes
3 Comments
Walter Roberson
on 17 Apr 2018
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.
Parikshit Sharma
on 17 Apr 2018
Walter Roberson
on 17 Apr 2018
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
Categories
Find more on Solver Outputs and Iterative Display in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!