0

I would like to maximize this function in MatLab - http://goo.gl/C6pYP

maximize | function | 3x+6y+9z domain | 12546975x+525x^2+25314000y+6000y^2+47891250z+33750z^2<=4000000000 | for | x y z

But variables x, y and z have to be nonnegative integers only. Any ideas how to achieve it in MatLab?

user2267971
  • 373
  • 1
  • 4
  • 12
  • As you are already given the location of the global optimum in the question on wolframalpha, it should be easy to deduce that Z is probably zero or close to it, and that the globabl optimal points for x and y are (close to?) upper bounds for the integer constrained optimum. – Dennis Jaheruddin Jul 12 '13 at 10:09

4 Answers4

0

The fact that you want integers makes this problem very difficult to solve (i.e. unsolvable in a reasonable time).

You will have to use some general optimizer and just try many starting conditions by brute force. You will not be guaranteed to find a global maximum. See Matlab's optimization package for further possible optimizers.

Bitwise
  • 7,577
  • 6
  • 33
  • 50
0

You have to formulate the problem as an ILP ( integer linear program). To solve an ILP, you need to make few changes to the input to matlab LP solver . You can also get the solution from the LP solver and then round the solution to integer. The solution may not be optimal but will be close.

You can also use the mixed-integer liner programing solver at file exchange site that in turn uses the LP solver. For binary variables you can use the matlab binary integer programing solver.

Shan
  • 5,054
  • 12
  • 44
  • 58
  • I think this can't be formulated as an ILP because the constraints are nonlinear. – Bitwise Apr 11 '13 at 12:09
  • if we define x1=x, x2=x^2, x3=y, x4=y^2, x5=z, x6=z^2 etc i.e the square terms are made as new variable that the objective become 3*x1+0*x2+6*x3+0*x4+9*x5+0*x6 and also the constraints become linear. So an ILP can be applied. – Shan Apr 11 '13 at 15:49
  • the constraints will become linear, but then the function itself becomes non-linear. This won't help. – Bitwise Apr 11 '13 at 16:18
  • the function is not nonlinear over the new variables. Can u be more clear where you see the non-linearity of the objective using the new variables? – Shan Apr 11 '13 at 23:30
  • This won't help because now x1^2==x2 has become a new constraint. – Dennis Jaheruddin Jul 12 '13 at 09:51
0

Well, fortunately the problem size is tiny so we can just brute force it.

First get some upper limits, here is how to do it for x:

xmax= 0;
while 12546975*xmax+525*xmax^2<=4000000000
    xmax=xmax+1;
end

This gives us upper limits for all three variables. Now we can see that the product of these limits is not a lot so we can just try all solutions.

bestval = 0;
for x = 0:xmax
    for y = 0:ymax
        for z = 0:zmax
            val = 3*x+6*y+9*z;
            if val> bestval && 12546975*x+525*x^2+25314000*y+6000*y^2+47891250*z+33750*z^2<=4000000000
                bestval = val;
                best = [x y z];
            end
        end
    end
end
best, bestval

This is probably not the most efficient way to do it, but it should be very easy to read.

Dennis Jaheruddin
  • 21,208
  • 8
  • 66
  • 122
0

The max of y and z(152,79) is not very high,so we can just check one by one to find the solution quickly(just 0.040252 seconds in my notebook computer).

My matlab code:

function [MAX,x_star,y_star,z_star]=stackoverflow1
%maximize 3x+6y+9z
% s.t.    12546975x+525x^2+25314000y+6000y^2+47891250z+33750z^2<=4000000000
MAX=0;
y_max=solver(6000,25314000,-4000000000);
z_max=solver(33750,47891250,-4000000000);
for y=0:floor(y_max)
    for z=0:floor(z_max)
        x=solver(525,12546975,+25314000*y+6000*y^2+47891250*z+33750*z^2-4000000000);
        x=floor(x);
        if isnan(x) || x<0
            break;
        end
        if 3*x+6*y+9*z>MAX
            MAX=3*x+6*y+9*z;
            x_star=x;
            y_star=y;
            z_star=z;
        end
    end
end
end
function val=solver(a,b,c)
% this function solve equation a*x^2+b*x+c=0.
% this equation should have two answers,this function returns the bigger one only.
if b*b-4*a*c>=0
    val=(-b+sqrt(b*b-4*a*c))/(2*a);
else
    val=nan; % have no real number answer.
end
end

The solution is:

MAX =

945

x_star =

287

y_star =

14

z_star =

 0
lihaitao
  • 155
  • 1
  • 8