이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
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월 14일
@Walter Roberson okay i understand now,thank you very much for your clarification,
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 (한국어)