Substitute variable and find maximum of the function

4 vues (au cours des 30 derniers jours)
enter
enter le 5 Avr 2020
Commenté : Ameer Hamza le 7 Avr 2020
Let's denote a function , where x and y belong to interval [1,5]. I would like to find maximum of this function, knowing that can have only two values : 0 or 1. So the code should check for what values of we can possibly get maximum and then calculate it within given intervat. At the end maximum should be printed.
My approach was to first substitute with permutations of 0 and 1, it's 4 combinations in total. Knowing we could find max within given intervals simply by creating a linspace
x = linspace(1,10);
for x and y, then meshgriding x and y, and in the end finding max using max(max(z)). The problem is, I don't know how to make a solution that will work fast enough to substitute 8 variables, for a more complicated function. I asked a similar question on this forum and this is the answer :
syms z(x,y) q1 q2
z(x,y)=(((sin(x).*(1+q1))./(3))+(1+q2))./(log10(y+1)-10);
A=perms([-1 1]);
c = linspace(1,5,100);
d=c;
[C,D] = meshgrid(c,d);
for i=1:size(A,1)
Z(x,y)=subs(z,{q1,q2},{A(i,1),A(i,2)})
Z_max(i) = max(max(double(vpa(Z(C,D),2))));
end
It works fine for , but it takes too much time to compute the result for 8 variables (,). The only thing that's needed is maximum.
  2 commentaires
David Hill
David Hill le 5 Avr 2020
It seems obvious that to maximize z, that q1 and q2 should be zero.
enter
enter le 5 Avr 2020
Yes, but what if I had more q's in some strange functions?

Connectez-vous pour commenter.

Réponse acceptée

Ameer Hamza
Ameer Hamza le 5 Avr 2020
The only function provided by Mathworks, of which I am aware, which can efficiently handle such mixed integer nonlinear programming problems is ga(): https://www.mathworks.com/help/gads/ga.html. However, you need to have global optimization toolbox to use this function
fun = @(x, q) (sin(x(1)).^2 + log(x(2)))./(x(1) + q(1) + q(2));
lb = [1;1;0;0]; % lower bounds
ub = [5;5;1;1]; % upper bounds
[sol, f] = ga(@(x) -fun(x(1:2), x(3:4)), 4, [], [], [], [], lb, ub, [], [3 4]);
Result:
>> sol
sol =
1.0000 5.0000 0 0
>> f
f =
-2.3175
  13 commentaires
enter
enter le 7 Avr 2020
You're right - I made up this function. I simply wonder how would it look if I had a different function.
Ameer Hamza
Ameer Hamza le 7 Avr 2020
John, I think the objective function still makes sense in the restricted domain, as mentioned in the question. The singularity is excluded from the domain of optimization. Also, the range of (-inf, inf) is the property of the objective function of every linear programming problem, but still, we are happy to find its solution on the constrained domain.

Connectez-vous pour commenter.

Plus de réponses (1)

John D'Errico
John D'Errico le 5 Avr 2020
Yes. It is probably best, IF you have some completely general function where you won't tell us the function, to use GA. Ameer is correct, and I have added a +1 vote for that answer because it is the most general solution. The goal of my answer is to discuss a different approach.
First, don't create numbered named unknowns like that. It will just cause problems in your code when you have many of them.
The simplest way to generate all combinations of such a binary variable is to use a binary representation of the variables.
nq = 2;
Q = dec2bin(0:2^nq-1) - '0'
Q =
0 0
0 1
1 0
1 1
So rather than using meshgrid, which will get messy for 3 or 6 or 8 variables, just use dec2bin.
fun = @(x,y,q) (sin(x).^2 + log(y))./(x + q(1) + q(2));
Now, you might substitute each combination as the rows of Q into this function, using a loop. Perhaps:
for i = 1:size(Q,1)
funi = @(x,y) fun(x,y,Q(i,:));
% now just find the optimum value of funi, for this combination of the variables q.
...
end
You might decide to use fminsearch, or perhaps use a better, more efficient optimizer. Using linspace on one of the variables is not a good idea. Learn to use optimizers properly, and you will find your code becomes much more efficient.
I won't write any serious code for this, since that objective is clearly just random garbage, not a serious function that you might be hoping to work with.

Community Treasure Hunt

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

Start Hunting!

Translated by