Substitute variable and find maximum of the function

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 件のコメント

David Hill
David Hill 2020 年 4 月 5 日
It seems obvious that to maximize z, that q1 and q2 should be zero.
enter
enter 2020 年 4 月 5 日
Yes, but what if I had more q's in some strange functions?

サインインしてコメントする。

 採用された回答

Ameer Hamza
Ameer Hamza 2020 年 4 月 5 日

2 投票

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 件のコメント

enter
enter 2020 年 4 月 5 日
What's the 4 and [3,4] in the last line and how to find it?
Ameer Hamza
Ameer Hamza 2020 年 4 月 5 日
first 4 specify the total number of optimization variables. [3 4] indicate which variables are integers.
enter
enter 2020 年 4 月 5 日
And the last question : x(1:2), x(3:4) - what's that?
Ameer Hamza
Ameer Hamza 2020 年 4 月 5 日
I though It would be a good idea to seperate continuous and discrete variables. Following is also equivalent to above
fun = @(x) (sin(x(1)).^2 + log(x(2)))./(x(1) + x(3) + x(4));
lb = [1;1;0;0]; % lower bounds
ub = [5;5;1;1]; % upper bounds
[sol, f] = ga(@(x) -fun(x), 4, [], [], [], [], lb, ub, [], [3 4]);
enter
enter 2020 年 4 月 6 日
Optimization terminated: average change in the penalty fitness value less than options.FunctionTolerance
and constraint violation is less than options.ConstraintTolerance.
I got this error
Ameer Hamza
Ameer Hamza 2020 年 4 月 6 日
This is not an error. This is the message from ga() that the optimal value is found.
enter
enter 2020 年 4 月 6 日
I forgot to ask : why is the result with minus?
Ameer Hamza
Ameer Hamza 2020 年 4 月 6 日
Actually, I used -fun as an objective function because ga() can handle the minimization problem, but your problem was about maximizing the function. I forgot to invert the sign later. You can write the following line at the end
f = -f;
enter
enter 2020 年 4 月 7 日
For substituting variables q_1,q_2 you used to fact that they can belong to internal [0,1] and then taking only integer values : 0 and 1. What if I wanted to substitute q_1,q_2 with 0 and 2 (only)?
enter
enter 2020 年 4 月 7 日
Also in some cases it divides by 0 and obtains max at inf
John D'Errico
John D'Errico 2020 年 4 月 7 日
But do you seriously want to optimize that specific function? You can see that it has a singularity in it, It is unbounded, going to both +inf and -inf in parts of the domain.
I expected this was just some random function you made up, as an example. That expectation came from the clear problems with the function, so that nobody would seriosusly want to minimize it.
enter
enter 2020 年 4 月 7 日
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 2020 年 4 月 7 日
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.

サインインしてコメントする。

その他の回答 (1 件)

John D'Errico
John D'Errico 2020 年 4 月 5 日

2 投票

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.

カテゴリ

タグ

質問済み:

2020 年 4 月 5 日

コメント済み:

2020 年 4 月 7 日

Community Treasure Hunt

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

Start Hunting!

Translated by