Allocation problem using fmincon

5 views (last 30 days)
Kyle Zarmair
Kyle Zarmair on 15 Jul 2014
Answered: Kyle Zarmair on 15 Jul 2014
I am having a hard time with the following allocation problem. The problem requires the user to distribute, as efficiently as possible, 100 instruments among 60 different sectors. There is a target amount for each sector, however due to a granularity problem; the optimal amount may not always be achieved. I thought of using fmincon, and set up the following codes:
Linear constraints:
Aeq =
100 200 0 0 0 0
0 0 300 400 0 0
0 0 0 0 500 600
beq =
300 0 0
0 800 0
0 0 1100
Lower bounds:
lb =
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
Upper bounds:
Ub =
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
objective function:
m =
yy =
My fmincon formula is
WW= fmincon(F,ones(6,3),[],[],m',sum(m),zeros(6,3),ones(6,3));
however I keep on getting the following error message:
Error using fmincon (line 286)
Aeq must have 18 column(s).
Can anyone provide me with a little insight?
Thank you.

Accepted Answer

Brian B
Brian B on 15 Jul 2014
The syntax for fmincon is
WW = fmincon(F,x0,A,b,Aeq,beq,lb,ub)
In your code, the initial guess x0 is a 6x3 matrix, but fmincon expects the optimization variable x to be a vecto. You could form x instead by stacking the columns of the original matrix.
Then there is some confusion about your equality constraints. Did you mean to use Aeq and beq defined earlier? If so, it appears that you want to define 9 equality constraints. These can be written in terms of the vector x as Aeq2 * x = beq2, where
Aeq2 = blkdiag(Aeq, Aeq, Aeq);
beq2 = beq(:); % stack columns of beq to form a vector
and Aeq is defined in your original question. But the actual call uses m' and sum(m) in place of Aeq and beq. These do not make sense mathematically (even if x is 6x3), as m'*x would be a 1x3 row vector and sum(m) is a scalar.
The upper and lower bounds are easily made into vectors.
Finally, the dimensions in your objective function do not make sense mathematically.

More Answers (2)

Kyle Zarmair
Kyle Zarmair on 15 Jul 2014
Thank you Brien. However I am faced with a problem where I need to allocated financial instruments, each of which have a predetermined size, to specific sectors. For example, I need to allocated 34 000$ to the agriculture sector. To reach this allocation I have to use the instruments at my disposal (example: stock #1 2000$, stock #2 3000, etc.). I must "fill up" thee sectors as closest as possible to the required weight, and I can only use every instrument only once. Do you still recommend me using the fmincon function?
Also, from my understanding, fmincon can only optimize a vector of unknown variables and not a matrix of unknown variables?
  1 Comment
Brian B
Brian B on 15 Jul 2014
Correct: fmincon will only optimize a vector, not a matrix. But the difference between optimizing a vector and optimizing a matrix is only syntactic; it is just a matter of expressing the problem in a form that fmincon expects.
Whether fmincon is the right tool or not depends on what the problem formulation looks like, which in turn depends on a correctly-written cost function. But it looks like you may end up with a linear program, or possibly a mixed-integer linear program. See or, if some of the variables must be integers,
Here is what I suggest:
  1. Carefully formulate the problem on paper, defining the optimization variables as a vector x.
  2. Decide which elements of x (if any) must be integers.
  3. Make sure the objective function is a scalar (if it is not, see
  4. If the cost function involves a constant offset (such as F = @(x) m'*x - c), you can discard the constant offset and add it to the optimal value.
  5. If, after discarding any constant offset, the objective is linear and there are no nonlinear constraints, DO NOT USE FMINCON.
  6. Try to work any equality or inequality constraints into one of the forms
  • A * x <= b
  • Aeq * x = beq
  • x <= ub
  • lb <= x

Sign in to comment.

Kyle Zarmair
Kyle Zarmair on 15 Jul 2014
Is it possible to optimize not only a single number but an entire matrix? here is a smaller scale version of my problem.
As you can see my matrix of x's is a kinda Arrow Debreu Asset matrix, where every x can only be used once in our matrix. I don't think I can do this with the intlinprog function you previously mentioned.


Find more on Get Started with Optimization Toolbox in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!