Using factorial in optimiztion toolbox

7 visualizaciones (últimos 30 días)
Parikshit  Sharma
Parikshit Sharma el 14 de Abr. de 2018
Comentada: Walter Roberson el 17 de Abr. de 2018
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!

Respuesta aceptada

Alan Weiss
Alan Weiss el 16 de Abr. de 2018

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 comentarios
Walter Roberson
Walter Roberson el 17 de Abr. de 2018
There is nothing in what you just posted that requires that you use double precision. You can use the symbolic toolbox.
Walter Roberson
Walter Roberson el 17 de Abr. de 2018
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).

Iniciar sesión para comentar.

Más respuestas (1)

Parikshit  Sharma
Parikshit Sharma el 17 de Abr. de 2018
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 comentarios
Parikshit  Sharma
Parikshit Sharma el 17 de Abr. de 2018
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??
Walter Roberson
Walter Roberson el 17 de Abr. de 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

Iniciar sesión para comentar.

Categorías

Más información sobre Solver Outputs and Iterative Display en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by