Optimization Toolbox - Add up weights as a constraint
Show older comments
Question I'm using the optimization toolbox to calibrate a parameter x. This parameter x is used then to calculate weights based on distances. I have n store locations and m towns with k customers per town. k(m) customers are the total customers of all stores in town m. I would like that the weights of all the stores for each of the towns sum to one. So my weight constraint doesn't entirely depend on x and I struggle to model this.
Minimization problem
function f = Catchment_OptP(x,Distances, TotCustomersByTown, ActCustByStore)
[R,C] = size(Distances);
NCustom = ActCustByStore * ones(size(ActCustByStore,2),size(ActCustByStore,1));
Weights = exp(-(Distances./(repmat(ActCustByStore,R,1)/NCustom.*x)).^2);
EstimCustByTown = TotCustomersByTown' *Weights;
Differences = ((ActCustByStore - EstimCustByTown).^2);
f = Differences * ones(size(ActCustByStore,2),size(ActCustByStore,1));
I call this function the following way:
f = @(x)Catchment_OptP(x,Distances, TotCustomersByTown, ActCustByStore);
[x, fval, output] = fmincon(f,x0,[],[],[],[],0,100,@(x)WeightConstraint(x,Distances, TotCustomersByTown, ActCustByStore),options)
Non linear constraint
I programmed the constraint that the weights of all the stores should add up to one as nonlinear constraint as the weight calculation function is nonlinear, not sure if that is correct. I wrote the following code for the nonlinear constraint. It's the same code as in the minimization, there is probably a shortcut but I definately missed it.
function [c, ceq] = WeightConstraint(x,Distances, TotCustomersByTown, ActCustByStore)
[R,C] = size(Distances);
NCustom = ActCustByStore * ones(size(ActCustByStore,2),size(ActCustByStore,1));
Weights = exp(-(Distances./(repmat(ActCustByStore,R,1)/NCustom.*x)).^2);
c = Weights * ones(size(ActCustByStore,2),size(ActCustByStore,1)); ceq = ones(size(Distances,1),1);
The matrices I use:
- Distances is a (m:n) matrix (m = number of towns, n = number of stores) and holds the distances between towns and stores
- TotCustomersByTown (m:1) vector (m = number of towns) holds total customers of all stores for each town
- ActCustByStore is a (n:1) vector (n = number of stores) holds total customers per store
Attached is the actual optimization problem.
Answers (0)
Categories
Find more on Nonlinear Optimization 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!