Raja
Raja

Reputation: 31

Parallel optimization with extra parameters (fmincon)

I have a function f=p1+2*acp2+2*abp3+2*bcp4

where

p1=-sin(((x(2)-x(1))/2)+((x(4)-x(3))/2)+((x(6)-x(5))/2))+sin(((x(4)-   x(3))/2)+((x(6)-x(5))/2)-((x(2)-x(1))/2))+sin(((x(2)-x(1))/2)+((x(6)-x(5))/2)-((x(4)-x(3))/2))+sin(((x(2)-x(1))/2)+((x(4)-x(3))/2)-((x(6)-x(5))/2));
p2=sin(((x(2)-x(1))/2)+((x(4)-x(3))/2)+((x(6)-x(5))/2))+sin(((x(4)-x(3))/2)+  ((x(6)-x(5))/2)-((x(2)-x(1))/2))-sin(((x(2)-x(1))/2)+((x(6)-x(5))/2)-((x(4)-x(3))/2))+sin(((x(2)-x(1))/2)+((x(4)-x(3))/2)-((x(6)-x(5))/2));
p3=sin(((x(2)-x(1))/2)+((x(4)-x(3))/2)+((x(6)-x(5))/2))-sin(((x(4)-x(3))/2)+((x(6)-x(5))/2)-((x(2)-x(1))/2))+sin(((x(2)-x(1))/2)+((x(6)-x(5))/2)-((x(4)-x(3))/2))+sin(((x(2)-x(1))/2)+((x(4)-x(3))/2)-((x(6)-x(5))/2));

p4=sin(((x(2)-x(1))/2)+((x(4)-x(3))/2)+((x(6)-x(5))/2))+sin(((x(4)-x(3))/2)+((x(6)-x(5))/2)-((x(2)-x(1))/2))+sin(((x(2)-x(1))/2)+((x(6)-x(5))/2)-((x(4)-x(3))/2))-sin(((x(2)-x(1))/2)+((x(4)-x(3))/2)-((x(6)-x(5))/2));

and x1 until x6 are angles subject to the bound constraint from 0 to pi.

I want to minimize this function for a range of a=[0:0.01:1] and b=[0:0.01:1]. (That is, I want to minimize this function for each a,b=0, a,b=0.01,a,b=0.02 ... and so on).

so this my code

Step 1: Write a file objfun.m.

function f= objfun (x,a,b,c)

a=.57;

b=.57;    % i pick a number for a,b

c=sqrt(1-a*a-b*b);
p1=-sin(((x(2)-x(1))/2)+((x(4)-x(3))/2)+((x(6)-x(5))/2))+sin(((x(4)-  x(3))/2)+((x(6)-x(5))/2)-((x(2)-x(1))/2))+sin(((x(2)-x(1))/2)+((x(6)-x(5))/2)-  ((x(4)-x(3))/2))+sin(((x(2)-x(1))/2)+((x(4)-x(3))/2)-((x(6)-x(5))/2));
p2=sin(((x(2)-x(1))/2)+((x(4)-x(3))/2)+((x(6)-x(5))/2))+sin(((x(4)-x(3))/2)+((x(6)-x(5))/2)-((x(2)-x(1))/2))-sin(((x(2)-x(1))/2)+((x(6)-x(5))/2)-((x(4)-x(3))/2))+sin(((x(2)-x(1))/2)+((x(4)-x(3))/2)-((x(6)-x(5))/2));
p3=sin(((x(2)-x(1))/2)+((x(4)-x(3))/2)+((x(6)-x(5))/2))-sin(((x(4)-x(3))/2)+((x(6)-x(5))/2)-((x(2)-x(1))/2))+sin(((x(2)-x(1))/2)+((x(6)-x(5))/2)-((x(4)-x(3))/2))+sin(((x(2)-x(1))/2)+((x(4)-x(3))/2)-((x(6)-x(5))/2));
p4=sin(((x(2)-x(1))/2)+((x(4)-x(3))/2)+((x(6)-x(5))/2))+sin(((x(4)-x(3))/2)+((x(6)-x(5))/2)-((x(2)-x(1))/2))+sin(((x(2)-x(1))/2)+((x(6)-x(5))/2)-((x(4)-x(3))/2))-sin(((x(2)-x(1))/2)+((x(4)-x(3))/2)-((x(6)-x(5))/2));
f=p1+2*a*c*p2+2*a*b*p3+2*b*c*p4;

Step 2: Invoke constrained optimization routine.

x=[x(1),x(2),x(3),x(4),x(5),x(6)]; % angles;
lb=[0,0,0,0,0,0];
ub=[pi,pi,pi,pi,pi,pi];
x0=[pi/8;pi/3;0.7*pi;pi/2;.5;pi/4];
[x,fval]=fmincon(@objfun,x0,[],[],[],[],lb,ub)

the solution produced is

x=
   2.5530
   0.6431
   2.5305
   0.6195
   2.5531
   0.6421

   fval= -4.3546 

What would I have to do to run the optimization for a and b ranging from 0:0.01:1, and save the optimal values for each a,b?

Upvotes: 0

Views: 136

Answers (1)

lhcgeneva
lhcgeneva

Reputation: 1981

You can find extensive documentation on this topic at Mathworks.

Your problem consists of two parts. First, passing the additional parameters a and b, which can be done for example by using anonymous functions (see above link). This is done like so:

f = @(x)objfun(x,a,b);

Second, wrap the function into a for loop or (if you have the parallel computing toolbox, as I would assume from your title and comment) in a parfor loop.

a = 0:0.01:1;
b = 0:0.01:1;
xvals = zeros(length(a), length(b));

for i = 1 : length(a)
    for i = 1 : length(b)
        .
        . invoke your routine here
        .
         f = @(x)objfun(x,a(i),b(i));
         [x,fval]=fmincon(@objfun,x0,[],[],[],[],lb,ub);
         x_cell{i, j} = x;
         fvals(i, j) = fval;
    end
end

After that's done you can access the individual values and parameter sets for example by indexing x_cell{1, 2} for a = 0 and b = 0.01;, similar for fvals, but with round brackets.

Parallel processing: Use a parfor loop for the outer loop if that's what you want to do. Do not use it in the inner loop, as it will be slower.

Upvotes: 1

Related Questions