How Can i transform condition into constraint equation?
2 次查看(过去 30 天)
显示 更早的评论
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 个评论
Torsten
2023-1-11
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
2023-1-11
I'm pretty sure they are using fixed timestep, and that the speed variable is distance per time step.
Maria
2023-1-11
@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.
回答(1 个)
Walter Roberson
2023-1-10
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 个评论
Maria
2023-1-10
@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
2023-1-10
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
2023-1-10
@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
2023-1-10
What exactly are the inputs and outputs for this proposed constraint equations?
[WhatIsThisFirstOutput, WhatIsThisSecondOutput] = @(WhatIsThisFirstInput, WhatIsThisSecondInput, WhatIsThisThirdInput) SomeExpressionToBeDetermined
Walter Roberson
2023-1-11
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
2023-1-12
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
2023-1-12
@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
2023-1-12
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
2023-1-12
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
2023-1-13
@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
2023-1-13
编辑:Walter Roberson
2023-1-14
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
2023-1-17
编辑:Maria
2023-1-17
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
2023-1-17
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
2023-1-17
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
2023-1-17
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
2023-1-17
@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
2023-1-17
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
2023-1-17
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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Linear Programming and Mixed-Integer Linear Programming 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!发生错误
由于页面发生更改,无法完成操作。请重新加载页面以查看其更新后的状态。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- 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)
亚太
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)