Minimization with discrete variable

I have some discrete variables of motor torque and speed. Ex: motor 1 has 6 Nm torque and 42 rpm, motor 2 has 9 Nm torque and 62 rpm, etc. I have to find the correct motor specification when applied on some mechanism and load and I already had the linearized function of this mechanism. I have to use optimization tool on Matlab. How can I put the discrete variable into matlab and how can I do the optimization? Really need help.

3 Commenti

jgg
jgg il 12 Gen 2016
I'm unclear what you're trying to do here. Describe, if you can, clearly what the outcome variable you're trying to minimize, what the choice variables are, and how they're related.
sorry, let me make this simple, I have several discrete variable, like A=[1 2 3 4 5 6 7 8 9], B=[11 12 13 14 15 16]. Then I want to know which combination is the minimum of a function. Let's say the function is A+B, so the best is A=1, B=11. My question is how do I do this in matlab? Looking forward for your help.
jgg
jgg il 12 Gen 2016
I assume the secondary issue is that you have a very very large number of these so that enumeration is not feasible?

Accedi per commentare.

 Risposta accettata

xvec=[12.7 15.875 19.05 25.4 31.75 38.1 50.8 63.5];
yvec=[1.27 1.5875 2.54 3.175];
zvec=[0.05 0.1 0.15 0.2];
obj =[7.44 4.19 1.70 2.25 3.80 1.42 1.69 0.43 0.53 1.26 0.97 0.68 0.72 0.58 0.42 0.35];
fun=@(x,y,z) 3*x/2*(y+3.14*z*x)/(3.14*x-x*y);
minimum = Inf;
k1_ref = 0;
k2_ref = 0;
k3_ref = 0;
for k1 = 1:length(xvec)
x=xvec(k1)
for k2 = 1:length(yvec)
y=yvec(k2)
for k3 = 1:length(zvec)
z=zvec(k3)
result = fun(x,y,z);
if result < minimum & any(abs(obj-result)<1e-6)
minimum = result;
k1_ref = k1;
k2_ref = k2;
k3_ref = k3;
end
end
end
end
xvec(k1_ref),yvec(k2_ref) and zvec(k3_ref) should be combined to give the minimum value contained in the array "obj".
If no combination exists for which the value is contained in "obj", k1_ref, k2_ref and k3_ref will keep their starting values, namely 0.
Best wishes
Torsten.

4 Commenti

Can I make the combination result taking nearest greater value in "obj"?
So let's say xvec=12.7, yvec=1.27, zvec=0.05 combination value is 3.75, it will go to 0. Can I make it the value 3.8? Because it's greater than 3.75 and it's the nearest?
If you do this, which combination will be optimal in the end ?
The combination which produces the minimum function value ?
The combination which is nearest to a number in the list of the obj-array ?
Or a combination of both criteria ?
Best wishes
Torsten.
the optimal one is the minimum of x y z combination, I already calculate it with MS Excel and got 12.7 1.27 0.05 as the optimal combination.
It will result 2.578 as the minimum function value. But 2.578 didn't contained in "obj", so I wish 3.8 as the minimum function value, because it's the nearest and greater than 2.578.
Thank you.
xvec=[12.7 15.875 19.05 25.4 31.75 38.1 50.8 63.5];
yvec=[1.27 1.5875 2.54 3.175];
zvec=[0.05 0.1 0.15 0.2];
obj =[7.44 4.19 1.70 2.25 3.80 1.42 1.69 0.43 0.53 1.26 0.97 0.68 0.72 0.58 0.42 0.35];
fun=@(x,y,z) 3*x/2*(y+3.14*z*x)/(3.14*x-x*y);
minimum = Inf;
k1_ref = 0;
k2_ref = 0;
k3_ref = 0;
for k1 = 1:length(xvec)
x=xvec(k1)
for k2 = 1:length(yvec)
y=yvec(k2)
for k3 = 1:length(zvec)
z=zvec(k3)
result = fun(x,y,z);
if result < minimum
minimum = result;
k1_ref = k1;
k2_ref = k2;
k3_ref = k3;
end
end
end
end
value_from_obj = min(obj(obj-fun(xvec(k1_ref),yvec(k2_ref),zvec(k3_ref))>=0));
Best wishes
Torsten.

Accedi per commentare.

Più risposte (2)

Alan Weiss
Alan Weiss il 12 Gen 2016

0 voti

If you can specify your problem as a mixed-integer linear programming problem, use the intlinprog function. There are several examples on that page that can help you with problem formulation.
If it is impossible to put into the MILP framework, use the ga solver from Global Optimization Toolbox to solve your mixed integer optimization. You might find some help in this example.
Alan Weiss
MATLAB mathematical toolbox documentation
jgg
jgg il 12 Gen 2016
Modificato: jgg il 12 Gen 2016
It's a little bit unclear what your objective function is, but suppose it's something like f(A,B) where A and B are your discrete random variables. Provided that you have only a few levels of the random variables, you could make a nice program like this where:
A = [1,2,3,4]; B = [1,2,3,4];
Then, set something like:
a_val = @(x)(1*x(1)+2*x(2)+3*x(3)+4*x(4));
b_val = @(x)(1*x(1)+2*x(2)+3*x(3)+4*x(4));
Then, your objective function is f(a_val(x),b_val(y)) and you have the restriction your lower bound is zero and upper bound is one, and x and y sum to 1. You could then easily use ga to solve this by following the documentation.
An alternative if you have many levels is to make the variable the level:
f(A(la),B(lb))
where la and lb are the levels, and you restrict them to be between 1 and k where k is the number of levels. I think ga would work well here as well.
It's a little hard to give you more direct advice without more information.

6 Commenti

I have further question if you don't mind. What if the value of objective function have to picked from a discrete random variable too? So example:
A=[1 2 3 4]; B=[2 3 4 5]; C=[1 5 6 7]
Function is:
C=A+B
So the minimum combination is 5, because 3 isn't belong in the objective value. Can you help me with the coding?
Rudi Gunawan
Rudi Gunawan il 13 Gen 2016
Modificato: Rudi Gunawan il 13 Gen 2016
function:
x(1)=3*(x(3)/2)*(x(4)+(3.14*x(5)*x(3))/((3.14*x(3))-(x(3)*x(4))))
And these are the discrete variable:
x(1)=[7.44 4.19 1.70 2.25 3.80 1.42 1.69 0.43 0.53 1.26 0.97 0.68 0.72 0.58 0.42 0.35];
x(3)=[12.7 15.875 19.05 25.4 31.75 38.1 50.8 63.5];
x(4)=[1.27 1.5875 2.54 3.175];
x(5)=[0.05 0.1 0.15 0.2];
I am asked to minimize x(1) and I am thinking about branch and bound method, but I can't do it either. Looking forward for your help.
Torsten
Torsten il 13 Gen 2016
Modificato: Torsten il 13 Gen 2016
Just calculate x(1) for all combinations of x(3), x(4) and x(5) (these are 8*4*4 = 128 numbers), order them and choose the combination with minimum value which is contained in x(1).
Best wishes
Torsten.
Yes I know it can be done by that way Torsten, but my lecturer ask me to do it with Matlab.
This should be done in MATLAB, too.
It avoids that you have to calculate 128 values with a table calculator.
Best wishes
Torsten.
Can you help me with the coding, please?

Accedi per commentare.

Community Treasure Hunt

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

Start Hunting!

Translated by