Minimization with discrete variable

9 views (last 30 days)
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 Comments
Rudi Gunawan
Rudi Gunawan on 12 Jan 2016
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 on 12 Jan 2016
I assume the secondary issue is that you have a very very large number of these so that enumeration is not feasible?

Sign in to comment.

Accepted Answer

Torsten
Torsten on 13 Jan 2016
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 Comments
Rudi Gunawan
Rudi Gunawan on 13 Jan 2016
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.
Torsten
Torsten on 13 Jan 2016
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.

Sign in to comment.

More Answers (2)

Alan Weiss
Alan Weiss on 12 Jan 2016
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 on 12 Jan 2016
Edited: jgg on 12 Jan 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 Comments
Torsten
Torsten on 13 Jan 2016
This should be done in MATLAB, too.
It avoids that you have to calculate 128 values with a table calculator.
Best wishes
Torsten.
Rudi Gunawan
Rudi Gunawan on 13 Jan 2016
Can you help me with the coding, please?

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!