You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
How Can i transform condition into constraint equation?
2 views (last 30 days)
Show older comments
Hello everyone!
i'm asking if i can change a condition line command into equation in order to use it in my problem. The condition is related to trajectory of vehicle in segment line, so if it arrives at the last point of the segment , it will come back to the initial point and keep going back and forth until the time ends.
in my code i wrote the condition as follow :
qu(1) = 0; % qu the initial position of vehicule
speed = 50; % 50 is the speed of vehicule ( constant)
k = 1;
while k < length(qu)
xk = qu(k) + speed;
if xk > 10000 %10000 is the length of the segment
speed = -50;
elseif xk < 0
speed = 50;
else
k = k+1;
qu(k) = xk;
end
end
How can i transform this code into simple equation?
any help is appreciated!
3 Comments
Torsten
on 11 Jan 2023
Why do you add a velocity (speed) to a position (qu(k)) ? This makes no sense.
position_new = position_old + speed*(time_new - time_old)
is the correct formulation.
Walter Roberson
on 11 Jan 2023
I'm pretty sure they are using fixed timestep, and that the speed variable is distance per time step.
Maria
on 11 Jan 2023
@Walter Roberson exactly!
@Torsten i want to add the condition of the length of segment, it means that : when new position = length of the segment , the new position should return at the previous position.
Answers (1)
Walter Roberson
on 10 Jan 2023
length(qu) starts as 1, k starts at 1, 1 < 1 is false, so the loop body is never entered. Therefore the code can be reduced to just the first two lines.
26 Comments
Maria
on 10 Jan 2023
@Walter Roberson thank you for your reply! i want to write a simple equation so i can use as constraint in my optimization problem, so i want to reformulate the code above to a line equation.
if the position qu at time slot (t) is equal to the length of the segment, the position of the vehicle should go back.
Walter Roberson
on 10 Jan 2023
But you are modifying qu in your sample code, which is inconsistent with a simple constraint.
Switching direction of travel in the way you do is not differentiable, which rules out using most of the optimizers such as fminsearch and fmincon. You would have to create your own optimizer or use ga() or similar if your equations at not differentiable.
What exactly are the inputs and outputs for this proposed constraint equations?
Maria
on 10 Jan 2023
@Walter Roberson i can define qu(t=0) = 0 at the first time slot , also the length of the segment is fixed and the first direction of the vehicle.
what i need exactly is to put a condition on qu in order to not exceed the length of the segment, but in form of equation or inequation.
Walter Roberson
on 10 Jan 2023
What exactly are the inputs and outputs for this proposed constraint equations?
[WhatIsThisFirstOutput, WhatIsThisSecondOutput] = @(WhatIsThisFirstInput, WhatIsThisSecondInput, WhatIsThisThirdInput) SomeExpressionToBeDetermined
Walter Roberson
on 11 Jan 2023
Why does it have to be an anonymous function? Instead of a full function ?
For example are you trying to write this symbolically? Are you trying to write a constraint using Problem Based Optimization expressions?
Walter Roberson
on 12 Jan 2023
Is V signed or unsigned? Is Teta to be +1 if V remains positive (or becomes positive), and -1 if V remains negative (or becomes negative) ?
If a step happens to end exactly at L or exactly at 0, then should the change in direction be noted on that step or should it happen next step?
Could you confirm that the total distance traveled in a step is to V, even if that requires bouncing off an end? So for example if currently at position 8 and maximum is 11 and velocity is 5, then 8+5=13 which is 2 greater than the boundary, so the final position should be 2 before the boundary (= 9 in this example) ? (The formula when crossing the boundary in a positive direction is that the new position is 2*L - (qu + v) )
Does the calculation need to take into account that the velocity might be greater than the overall width, so that the object might need to "bounce" several times in one time step ?
Maria
on 12 Jan 2023
@Walter Roberson sorry for my late reply! thank you very much for your interest
V is unsigned, This is the whole formulation :
0<=qu(t)<= L
if qu(t) =0 ,qu(t+1) = qu(t) + V.Teta %here Teta=1
if qu(t) = L, qu(t+1)= qu(t) -V.Teta %here Teta = -1
all that i want is to write a constraint that related these two conditions.
for example
qu(t) =0 , qu(t+1) = 0+30 = 30
qu(t)= 500, qu(t+1) = 500-30 = 470 , %here L=500
i focus only on intial and final position.
Torsten
on 12 Jan 2023
As Walter already asked: What is the context in which you need this formulation ?
In problem-based optimization ? In a loop you independently created ?
Once you reach qu(t) = L, the values of qu(t+1), qu(t+2),... will oscillate between L and L-V. Is that wanted ?
Walter Roberson
on 12 Jan 2023
You know, if V is constant then it would be easier to express qu as a function of time, then as something that you build iteratively.
If V is not constant but is known ahead of time, perhaps V(t) is known, then the qu can be built ahead of the optimization run -- it does not have to be expressed as a constraint
You only need it as a constraint if velocity is varying in a way that is not obvious without the optimization calculations. For example if you were planning a trip and the V were speed limits on the roads between nodes, and the route to be taken was to be determined, then Yes, there might be need to determine qu dynamically.
Maria
on 13 Jan 2023
@Walter Roberson V (t) is constant , my optimization variables are qu(t) and Teta that's why i want to put a constraint for not exceed the boundary of the segment and Teta will be changed if it is the case.
i'm grateful for your support
Walter Roberson
on 13 Jan 2023
Edited: Walter Roberson
on 14 Jan 2023
You have a deterministic formula for the next qu given the current qu and the segment length and the current time and the velocity. There is no optimization of qu going on.
Imagine that L was 180. Now model the progress in terms of circular travel that wraps 180 to -180. 178+5 wrapping to become -177, then that becoming -172 after another step. Negative distance would correspond to travelling back towards the origin and the amount negative would correspond to how much distance is remaining to get back there. You would abs() the value to get the distance from origin and sign() would given you direction of travel. But clearly circular motion of this kind is just continuous motion that does not have to be expressed as a constraint, and clearly the location at any given time could be computed according to mod(V*t, 2*L) full cycles with a wrap at L. The exact value can be expressed with sine and arcsine and some multiplication and division
Maria
on 14 Jan 2023
@Walter Roberson okay i understand now,thank you very much for your clarification,
Maria
on 17 Jan 2023
Edited: Maria
on 17 Jan 2023
i'm trying to solve an optimization problem but the hard part is discussed above, i tell you that i want to optimize the first location of vehicule but i didn't find how to write the constraint. I found a method to write the different location points based on the first one qu(t=1) the point that i want to optimize,
here is the clarification ,
because in each time or iteration qu(t=1) will be known so, i noted a= (L-qu(1))/v (v is constant)
e.g for qu(1)=60, L=1000, v=20m/s so a=47 and a1= L/20=50
now for 0<t<a+1
qu(t)=qu(1)+(t-1)*v.teta.dt , dt=1 and teta =1
e.g qu(20)=60+(19*20)*1 =440 m
now for t>a+1
for e.g
qu(49)=qu(1)+(a*v*teta*dt)-(1*v*teta*dt)
=60+(47*20)-(20) = 980
here i substracted (49-(a+1))=48 so for the 47 i put it in the positive part and the 1 in the negative part
another eg
qu(100)=qu(1)+(47*v*teta*dt )-(50*v*teta*dt)+(2*v*teta*dt)
60+940-1000+40 =40
here i subtracted (100-(a+1))= 52 , grater than a1 ,so for the 47 i put it in the positive part and the 50 in the negative part and the 2 in the positive part.
so i'm asking if there is a way to write constraint based in a and a1 to successively determine the location.
thanks in advance
Maria
on 17 Jan 2023
No I means a1. I want to write qu(t) as constraint following the equation that I represented, qu(t=1) is known , but I want to complete the next location in each time,the problem is the position should not exceed 0 and L, I already mentioned how to get qu(t) based on a and a1,but i want constraints that can express clearly qu(t) as function of qu(1)
Walter Roberson
on 17 Jan 2023
A constraint is something that, if it is not met, would cause that proposed set of model inputs to be rejected.
You are not trying to reject a situation in which the vehicle would go off a side of the map: you are only wanting to redirect the direction of travel while continuing onward with the same model parameters.
Your qu(t) is not an input, and not an output either. It is an intermediate variable. You should not be constraining it.
Torsten
on 17 Jan 2023
So qu(1) has to be a multiple of 20 ? Your constraint at least would also be valid for t>=1 if qu(1) was arbitrary.
Maria
on 17 Jan 2023
@Walter Roberson yes i got it , but because i want to optimize the initial point so the next points are related to it, and i have expression in my problem where qu(t) is mentioned so that's why i would put qu(t) as function of qu(1) but the problem in the direction when the vehicle cross the total distance.
I wrote above equations that combine qu(t) and qu(1) but i don't know how to formulate it in order to apply it for each (t)
Torsten
on 17 Jan 2023
Thus my question remains.
Is qu(1) a multiple of 20 ?
If no: How to deal with the case that the opposite wall is not exactly hit at a time instant t ?
Walter Roberson
on 17 Jan 2023
Are you wanting the model to reject certain proposed values of qu(1), to say "Ah, this set of proposed parameters does not lead to a feasible model, try a different set of parameters" ?
If so, if qu(1) is one of the model parameters, then set a lower bound on it of 0, and set an upper bound of (L-NumberOfTimeSteps*v) . [If (L-NumberOfTimeSteps*v) would be negative, then that implies the equations are impossible to satisfy]
But I don't think that is what you want. You want the direction to reverse when you hit L or 0 and you want to keep going. That is not a constraint and cannot be handled by upper/lower bounds or by linear inequalities or by nonlinear equalities.
See Also
Categories
Find more on Linear Programming and Mixed-Integer Linear Programming 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!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom(English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)