How to solve NLP Optimization problem ?
Show older comments
Hello everyone! I'm new in NLP solver, i'm wondering how to write appropriately my objective function also my variables and constraints, i have seen many videos in order to understand but i didn', so i want to share with you my optimization problem may some of volunteers could help me, and any information or suggestion will be appreciated. Thank you in advance,
63 Comments
Walter Roberson
on 19 Jan 2023
Is this a different project than your earlier one? You have been working on a vehicle optimization problem of some kind in https://www.mathworks.com/matlabcentral/answers/1891590-how-can-i-transform-condition-into-constraint-equation?s_tid=prof_contriblnk but now you want to work on a Natural Language Processing optimization problem of some kind ???
Maria
on 19 Jan 2023
Walter Roberson
on 19 Jan 2023
I cannot tell from the problem statement which variables are being optimized over. The X with the qu(t=1), theta, n_m, s_m notation is not clear to me.
Walter Roberson
on 19 Jan 2023
One thing I noticed is (C5) talks about
having to be exactly
.
You spent a lot of time before talking about how qu had to bounce off of L or 0 -- so for example if it was at 7 and v is 5 and L is 10, then you would want the total distance traveled to be 5, so you would want to pass through the values 8, 9, 10, bounce to reverse direction, 9, 8 .
But the magnitude calculation given in the equation is 1 dimensional, so if you are at 7 and velocity is 5 then the only valid outcomes to consider would be 7+5 = 12 or 7-5=2 and with being limited to L = 10 then the 7+5=12 would be prohibitted -- implying that if you are a location near 0 or L and you would bounce if you traveled distance v in your current direction, then you have to reverse direction immediately, rather than traveling through to the boundary and reflecting off of it. That would be an important change to what you were discussing before.
Maria
on 20 Jan 2023
Walter Roberson
on 20 Jan 2023
I do not know if there is a mistaken in that response, as I am left too confused to understand it.
The variables to be optimized is qu(t=1) and Teta.
Okay, that would be two variables. And you could constrain the qu(t=1) by using a simple lower and upper bound.
s_m is also a binary matrix but unknown
Is it input or output or temporary variable ???
it means s_m is n_m satisfying constraints
Most often constraints under-specify. For example you might say that a particular 2 x 3 matrix had to have certain row sums, and certain column sums, but that would give you (number of rows) plus (number of columns) constraints whereas you have (number of rows) times (number of columns) values in the matrix, 2+3 constraints for 2*3 entries... so the array would not be fully constrained.
Walter Roberson
on 20 Jan 2023
It looks to me as if your constraints (C1) to (C7) are all given as part of whatever problem you are trying to solve.
I still do not understand what is being maximized. max X ? But I do not see any X in your equations. And how do you maximize an array such as n_m ???
I am also baffled about what all this is for . What is being computed ??
What are your inputs? What is the meaning of each of them?
Torsten
on 20 Jan 2023
X = sum_m sum_t s(m,t )* n(m,t)
and s and n are binary matrices.
But the background of the problem would of course be helpful to understand what's going on.
Maria
on 20 Jan 2023
Torsten
on 21 Jan 2023
And what's the problem you are trying to optimize behind all this ?
Maria
on 21 Jan 2023
Maria
on 21 Jan 2023
The first thing you have to realize is that you cannot use "fmincon" to solve.
You have a nonlinear optimization problem with - among other variables - an unknown binary matrix S as you claim.
So the only MATLAB code you could try to use is "ga".
Make an attempt:
- Make a list of your unknowns (with dimensions) and put them all together in one long vector
- Classify your constraints as belonging to linear equalities, linear inequalities, bound constraints, integer constraints and nonlinear constraints
- Put the constraints in the adequate fields passed to "ga" (linear equalities: Aeq, beq, linear inequalities: A, b, bound constraints: lb, ub, integer constraints:intcon and nonlinear constraints :nonlcon)
Walter Roberson
on 22 Jan 2023
Please describe in words what kind of problem you are trying to solve.
You have a vehicle. The vehicle moves at a constant speed. Okay, we got that.
But we have no idea what you are talking about for "depositive" or "agent connected", or what the "controller" is in this context
We have no idea what the M or S matrices represent.
alpha means ?
Bm means ?
dm_ser means ?
dt is the time step, currently set to 1. Can this change? It is important to know if dt can ever be different than 1, because you are currently using (steps times dt) as an index for arrays
G0 means ?
H means ?
L is a length of something that the vehicle is traveling on. It seems to have to do with a line.
lambda means ?
M means ?
N0 means ?
Pm means ?
Q means ?
qm means ?
qu(t) is the current vehicle position at time t, not at index t.
qu_start is the initial vehicle position
S matrix means ?
S1, S2, S3, S4, S5 means ?
T means ? Current time maybe?
V is the constant velocity of the vehicle? Or is it the constant speed of the vehicle?
You should be able to create a list of every input constant and every input variable and describe each of them
Walter Roberson
on 22 Jan 2023
lambda % is the arrival of user nodes following poisson distribution
If there are any random number generations in the code then fmincon is not suitable. fmincon is only for consistent functions.
Some of the inputs appear to be integer valued or at least discrete valued. fmincon cannot be used for discrete inputs
Maria
on 22 Jan 2023
According to your problem formulation, your problem is nonlinear and contains binary unknowns.
As I cannot understand your answer concerning the binary matrices N and S, I'll assume that their values cannot be directy deduced from qu(t=1) and theta.
So summarizing the answer I already gave you: you'll have to use MATLAB's "ga".
Did you in the meantime follow the steps I suggested:
- Make a list of your unknowns (with dimensions) and put them all together in one long vector
- Classify your constraints as belonging to linear equalities, linear inequalities, bound constraints, integer constraints and nonlinear constraints
- Put the constraints in the adequate fields passed to "ga" (linear equalities: Aeq, beq, linear inequalities: A, b, bound constraints: lb, ub, integer constraints:intcon and nonlinear constraints :nonlcon)
?
Walter Roberson
on 22 Jan 2023
I suspect that you might want to use Problem Based Optimization
Walter Roberson
on 22 Jan 2023
qu_start
I cannot tell whether qu is intended to be strictly integer? If it is then fmincon() cannot be used
Your initial flight direction, teta, appears to be +1 or -1 -- discrete values. fmincon cannot handle that. However, what you can do is optimize twice, once with an initial direction of +1 and the other time with an initial direction of -1 (that is, initial teta would not be optimized over) and take the better of the two results.
Torsten
on 22 Jan 2023
Both S and N are binary, thus discrete. I think at least one of them should be optimized.
Walter Roberson
on 22 Jan 2023
N is randomly constructed ahead of time, not something being optimized over.
Earlier https://www.mathworks.com/matlabcentral/answers/1897285-how-to-solve-nlp-optimization-problem#comment_2572485 the OP indicated that the variables to be optimized are qu(t=1) and teta -- a list that does not include S.
That is repeated in https://www.mathworks.com/matlabcentral/answers/1897285-how-to-solve-nlp-optimization-problem#comment_2575755 -- and there it is indicated that S elements will be set if the corresponding N element was successfully serviced, so that is not an input either.
If we were to make the rule that S is only set to 1 if a node in N is successfully serviced, then I think the objective could be rewritten to sum(sum(S)) which is nnz(S) -- the total number of serviced nodes. But that would be an integer and fmincon can only optimize over continuous values.
Walter Roberson
on 22 Jan 2023
WIth there being only one input value being optimized over (qu_start), assuming that we have split runs with teta constant for each run, then if two different function calls returned the same number of nodes served, then fmincon() would conclude that the function was flat and would stop optiizing. So fmincon() is simply not suitable for this situation.
Walter Roberson
on 22 Jan 2023
Q %delay threshold that demanded by each user to receive their requests( it is the same for all)
Does that mean that when the drone services a request, that it takes time Q to do so? If so then each time it services the request, it can travel only (dt-Q) * v in one of the steps, rather than dt * v .
It would often make sense to say that it takes time for the drone to stop to do something; there are many fewer things that a drone can do effectively just by flying by at full speed.
It seems to me likely that the drone should not be expected to "bounce" off of the ends in a situation such as this. It does not make sense for a drone to fly in a direction that there is currently no known demand to meet -- not unless the drone cannot tell where the demand is coming from. The situation is not impossible, but is not especially likely.
One could imagine a situation in which each node sends up a randomized unique beacon when it wanted service (a different beacon each time it wanted service), and did so in a low power mode that made it impractical to triangulate the source of each beacon, so that at each step the drone has to fly over the next potential location to see if it needs service. And maybe the drone is "delivering" some kind of digital authenticator so it does not need to stop or slow down as it flies. Could be, not impossible, someone just might want to do such a thing. But in cases where the drone is able to observe the list of active requests and locations, it would make sense for the drone not to bother flying further in a direction than is needed to satisfy the furthest request in that direction.
Maria
on 22 Jan 2023
Walter Roberson
on 23 Jan 2023
The calling sequence for ga is very nearly the same as the calling sequence for fmincon()
fmincon(Function, x0, A, b, Aeq, beq, lb, ub, nonlcon, options)
compared to
ga(Function, nvars, A, b, Aeq, beq, lb, ub, nonlcon, options, intcons)
where nvars is the number of variables (not a starting point like in fmincon) and intcons is a list of indices of variables that are constrained to be integers.
ga() is a minimizer. You can use it for maximization by using
objective_to_minimize = @(x) -objective_to_maximize(x)
... just like for fmincon.
Walter Roberson
on 23 Jan 2023
qu1 = optimvar("qu1","LowerBound",0,"UpperBound",L);
%----------------------------
Aeq = zeros(1,T);
No, if you are using problem based optimization, then you do not use numeric arrays for the constraints; see https://www.mathworks.com/help/optim/ug/optim.problemdef.optimizationconstraint.html
Torsten
on 23 Jan 2023
Aeq = zeros(1,T);
beq = zeros(1,T);
for i = 1 : T-1
Aeq(i) = abs(qu(i+1)-qu(i));
beq (i)= velocity*dt;
end
As discussed earlier, the qu(i) for i>=2 don't need to be used as optimization variables or constraints. They can simply be deduced from your values for qu(1), velocity and dt.
So these constraints are unnecessary.
Further you did not yet decide if you assume that L-qu(1) is divisible by velocity*dt or not. And how you want to handle reflection at L if it is not divisible.
Yes, L-qu(1) is divisible by velocity*dt , and if it is not divisible i will define new L1, should i put this in Aeq?
What is L1 ? If it is qu(t=1), you cannot simply redefine it since it is an optimization variable. You will have to define an integer variable qint with 1<= qint <= floor(L/(velocity*dt)) and use q(t=1) = qint * velocity * dt.
Maria
on 23 Jan 2023
Maria
on 23 Jan 2023
Maria
on 23 Jan 2023
do you mean q(t=1) becomes 1<=qu(t=1)<=qint ?
No. Define an integer variable to be optimized named "qint" which can range between 1 and floor(L/(velocity*dt)).
This variable has to be defined as integer variable (thus intcon = 1) with lower bound 1 (lb) and upper bound floor(L/(velocity*dt)) (ub).
Then, in constraints where qu(t=1) is needed, insert qint * (velocity*dt) for this variable.
But I think the problem is too complicated for you to succeed.
Don't you have classmates that could help you ?
Torsten
on 23 Jan 2023
You mix elements of solver-based and problem-based optimization. That's not possible. Either you call "ga" directly with numerical input arguments (solver-based approach) or you formulate problem-based and call "solve".
Walter Roberson
on 23 Jan 2023
... or you formulate using problem-based approach, and then call routines that converts the problem based approach into array-based struct, and then call ga() on that...
Maria
on 23 Jan 2023
Walter Roberson
on 23 Jan 2023
sol.q_int
Maria
on 23 Jan 2023
Walter Roberson
on 24 Jan 2023
Please post your current version of the code
Walter Roberson
on 24 Jan 2023
t_vec = 0:dt:dt*T;
That is a vector.
for t = 1:t_vec-1
When you use a non-scalar as the bound in a : operator, the effect is the same as if you had used the first element of the array. So that code is equivalent to
for t = 1:t_vec(1)-1
but t_vec(1) is 0, and 0-1 is -1, so the loop is equivalent to
for t = 1:-1
which will not do any iterations.
Aeq2 = qu_start;
beq2 = q_int*UAV_Speed*dt ;
qu_start = 0, so Aeq2 = 0
q_int is an optimvar(). UAV_Speed = 20, dt = 1, so beq2 = 20*q_int .
If you were to activate Aeq2, beq2 (which you do not do), then that would be the constraint that 0 = q_int*20 . Which only has a single solution, q_int = 0. Which makes the existence of q_int and the constraint pointless.
for t = 1:t_vec-1
Aeq1 = norm(qu(t+1)-qu(t));
beq1 = UAV_Speed *dt ;
end
You overwrite all of Aeq1 and beq1 in every iteration of the loop. If the loop were being run at all (which it is not because 1:-1 as described above) then you would be forming a constraint only for one step. On the other hand, you do not activate the constraint so... shrug.
Considering the qu(t+1) in the loop, I would suggest to you that the proper limit for the loop should be for t = 1 : T-1
constraint = E_max-(E_prop +cumsum(sum(S.*Energy_Pr,2))) <= 0;
In the run I did, TaskMatrix generated a 273 x something array, so M = 273. Energy_Pr then became a 273 x 3600 array in which each row has exactly one non-zero value. In row 1, in the test run I am doing, it is Energy_Pr(1,18) that is non-zero, and it happens to have a value of 24. So when (S.*Energy_Pr,2) is done, the output for the first row is 24*S(1,18). cumsum() of all of those similar entries gives the same thing for its leading value, so the first cumsum output is 24*S(1,18) in my test run. E_max and E_prop are constants, so for the first row, the left side of the constraint comes out as -24*S(1, 18) + 11056 . And that must be <= 0 . Subtracting the constant from both sides, -24*S(1, 18) <= -11056 . Divide both sides by -24 to get S(1,18) >= 460.6667
But...
S = optimvar("S",[M,T],'Type','integer',"LowerBound",0,"UpperBound",1);
So S is constraint to be either 0 or 1. And there is no value in the set {0,1} such that the value is >= 450.6667
I would suggest to you that you might have wanted to code
constraint = (E_prop +cumsum(sum(S.*Energy_Pr,2))) <= E_max;
After making those changes to loop bounds and the constraints (but not activating the Aeq* constraints), and running again, I do get through the run, and all of the S entries are 1 on output. That would suggest that you have not designed your problem correctly.
Maria
on 24 Jan 2023
Maria
on 26 Jan 2023
Torsten
on 26 Jan 2023
As long as the solver you use does not tell you that it cannot solve your problem, the reason that the solution you get is wrong is caused by your problem formulation, not by a failure of the solver.
You still mix problem-based elements
q_int = optimvar("q_int","LowerBound",0,"UpperBound",a);
qu = optimvar("qu",[1,T]);
S = optimvar("S",[M,T],'Type','integer',"LowerBound",0,"UpperBound",1);
with solver-based elements
for t = 1:T-1
Aeq1 = norm(qu(t+1)-qu(t));
beq1 = UAV_Speed *dt ;
end
Aeq2 = qu_start;
beq2 = q_int*UAV_Speed*dt ;
lb = [];
ub = [];
That's not possible. Decide which optimization method you prefer.
And don't you see that you overwrite the Aeq1 and beq1 values for 1 <= t <= T-2 by those obtained for t = T-1 in the loop over t ?
Further you have to define q_int and qu_start as optimization variables. The link between them is the equality constraint
qu_start - q_int*UAV_Speed*dt == 0
Walter Roberson
on 26 Jan 2023
qu_start = 0; according to the posted code. UAV_speed and dt are constants according to the posted code. So you have the equality constraint 0 = q_int*constant and the only solution to that is q_int = 0
If qu_start were some other constant then q_int would be constrained to be exactly equal to qu_start / (UAV_speed * dt)
Torsten
on 27 Jan 2023
@Luca Ferro comment moved here:
Optimization problems have no method, property, or field named 'Equations and you tried to call it here:

Torsten
on 27 Jan 2023
Why do you initialize arg1 of dimension (1,T) (arg1 = optimexpr(1,T);) and fill it as arg1(N,T) (arg1(i,k) = ...) ?
Maybe the sum comes from the call to "norm". If the sum is taken over only one element, namely ((((q_int .* 20) .* 1) + 960) - 126.6667).^2, it shouldn't matter.
Maria
on 27 Jan 2023
Yes, you only need q_int and flight_direction as solution variables here.
The qu(t>1) can be computed within the function nonlcon depending on q_int, flight_direction and t.
Thus defining
qu = optimvar("qu",[1,T]);
is not necessary since you know that
qu(2) = q_int + flight_direction* dt*UAV_Speed
etc.
And if qm(t) is given externally, the norm expression can be evaluated.
Maria
on 27 Jan 2023
Maria
on 27 Jan 2023
I don't understand all the if and else here, but - at least according to my understanding so far - the following code should generate the qu-array depending on L, T, Flight_direction, q_int, UAV_Speed and dt:
L = 200;
T = 547;
Flight_direction = -1;
UAV_Speed = 4.0;
dt = 1.0;
a = floor(L/(UAV_Speed*dt));
q_int = randi(a+1,1)-1;
qu(1) = q_int*UAV_Speed*dt;
direction = Flight_direction;
if q_int == 0
direction = 1;
end
if q_int == L
direction == -1;
end
for k=2:T
qu(k) = qu(k-1) + direction*UAV_Speed*dt;
if qu(k)==0 || qu(k)==L
direction = -direction;
end
end
Maria
on 28 Jan 2023
Torsten
on 28 Jan 2023
Sorry, but I always work with the solver-based optimization approach. So I cannot give you advice on how to handle this problem.
Maria
on 29 Jan 2023
Answers (1)
Walter Roberson
on 1 Feb 2023
0 votes
Please leave the question open as the volunteers have put considerable effort into it.
Categories
Find more on Choose a Solver 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!