Presentation is loading. Please wait.

Presentation is loading. Please wait.

Programming in hybrid constraint languages 上田研究室 M2 中村 好一.

Similar presentations


Presentation on theme: "Programming in hybrid constraint languages 上田研究室 M2 中村 好一."— Presentation transcript:

1 Programming in hybrid constraint languages 上田研究室 M2 中村 好一

2 The relations between the cc languages cc cc Default cc Timed cc = cc + defaults = cc + time Timed default cc Hybrid cc Timed Default cc = cc + defaults = cc + defaults +cont.time + discrete time Hybrid cc

3 Hybrid cc z ハイブリッドシステムのモデリングと検証を行う言語と して考案された z 離散的・連続的な動作を表現できる Example: 部屋の温度を管理するエアコン ・部屋の温度は、連続的な微分方程式に従って変化する ・誰かが窓を開ける、中にいる人が出て行くなどの行動は 方程式を離散的に変化させる

4 The cc Programming Language 制約 store と Agent(tell,ask) から構成される計算モデルを持 つ Example program: x=10,x’=0,if x>0 then x’’=-10 X = 10 X’ = 0 if x>0 then x’’=-10 if y=0 then z=1 store x’ = 0 X = 10 if x>0 then x’’=-10 if y=0 then z=1 store x’ = 0, x = 10 if x>0 then x’’=-10 if y=0 then z=1 store x’ = 0, x = 10 x’’=-10 if y=0 then z=1 store x’ = 0, x =10, x’’ = -10 if y=0 then z=1 Basic combinators: c add constraint c to the store. if c then A if c is entailed by the store, execute subprogram A, otherwise wait. A, B execute subprograms A and B in independently. unless c then A if c will not be entailed in the current phase, execute A (default cc).

5 Hybrid cc – the language and its use c : tell the constraint c if d then A : if d holds,reduce to A if d else A : reduce to A unless d holds A,B : Parallel Composition new V in A : V is local to A hence A : execute A at every instant after now

6 t=0 t=0+ t=1.414- t=1.414 x=10,x'=0, hence{if x>0 then x''=-10, if x=0 then x' = -0.5*x'} Hcc Example1

7 Hcc Example2 #Prey & Predator(in Hcc)# py=8, //prey pd=2, //predator pd’=0.2, always py’=0.08*py – 0.04*py*pd //prey growth always { cont(pd) if(pd>=0.5*py) then pd’=-0.1*pd+0.02*py*pd else pd’=-0.06*pd+0.02*py*pd }, sample(pd), sample(py)

8 Prey&Predator Figure:Prey&Predator

9 Computational model プログラムにおける処理順序 1. Point Phase( 離散的変化が起こる時点 ) 2. Interval Phase( 連続的変化が起こる時点 ) ※ 1, 2 を交互に繰り返し行う

10 The Control Constructs and Execution of hcc zHcc の実行順序 1. Point Phase から始まる 2. Point Phase におけるすべての計算を行う 3. Interval Phase に移る 4. プログラム中の微分方程式の積分を行う (4th order Runge-Kutta 法 ) 5. 制約の状態が変化、制約の矛盾が発生、処理時間が経過した場合 に 積分を終える 6. 処理後、再び Point Phase に移る 同様に、 1 ~ 6 を繰り返す

11 Execution of hcc Example; X =0,hence{x’=1,if(x=2) then y=1} In the interval phase following x=0,x evolves continuously according to x’=1,through the interval(0,2) until x=2 is about to become true. At this point the set of constraints may change,so the next point phase is started.

12 Continuous Constraints ContConstr ::= Term RelOp Term | cont(LVariable)| lcont(LVariable) RelOp ::= = | >= | <= | := = Term ::= LVariableExpr | Constant | Term BinOp Term | UnOp(Term) | dot(Term,Num) | Term’| (Term) LVariableExpr ::= Lvariable | Uvariable.LVariableExpr BinOp ::= + | - | ※ | / |^ UnOp ::= - |log | exp | prev ・ Lvariables are strings which start with a lowercase character. ・ Constants are floating point numbers. ・ exp(x) is the exponential function e^x. ・ Term’ denotes the derivative of Term. ・ dot(Term, n ) denotes the n th derivative of Term( n is a natural number).

13 Ask Constraints (for Continuous Constraints) AContConstr ::= Term ARelOp Term ARelOp ::= = | >= | | !=

14 Non-arithmetic Constraints Dconstr ::= UVariableExpr | UVarExpr = Dexpr UVariableExpr ::= UVariable | UVariableExpr.UVariableExpr Dexpr ::= UVariableExpr | prev(UVariableExpr) | String |(VarList)HccAgent |(VarList)[VarList]HccAgent |UVariableExpr(VarList) [VarList]HccAgent VarList ::= Uvariable | Lvariable | VarList,VarLIst ・ Uvariable is a string starting with an uppercase character. ・ HccAgent is hcc Agent.

15 Closure & Class definition zClosure definition: X = (A,x) Agent zClass definition: X = (m,n)[A,m] Agent

16 Closure definition X = (A,x)Agent Example: P = (n,m,Q){ if n > 0 then new m1 in {Q(n-1,m1,Q), m:= m1*n} else m = 1 }, Fact = (n,m) {P(n,m,P)}, Fact(10,m)

17 Class Definition X = (m,n)[A,m] Agent Example: Planet = (initvx,initvy,initpx,initpy,m)[px,py,mass]{ px = initpx, py = initpy, px’= initvx, py’ = initvy, always{ mass=m, px’’:=sum(F.x,Force(F),F.Target = Self), py’’:=sum(F.x,Force(F),F.Target = Self) }

18 Boolean Constraints BoolConstr ::= Atom | StrConstr | ContConstr | Bool && BoolConstr | (BoolConstr) ABoolConstr ::= Atom | StrConstr | AContConstr | ABool && ABoolConstr ABoolConstr || ABoolConstr | (ABoolConstr)

19 Reduction Rules Tell : <(Γ, c),σ,next, default → <Γ,σ U {c},next, default} σ|- d Ask : → else : → Γ : denotes a set of Hybrid cc program fragments. σ : denotes the store. next : the set of program fragments to be run in the next phase. default : a set of suspended else statements. σ |- c : denotes entailment checking.

20 Reduction rules(for Hence) The rule for hence A differs in point and interval phases Hence Point : →, default> Hence Interval : → Γ : denotes a set of Hybrid cc program fragments. σ : denotes the store. next : the set of program fragments to be run in the next phase. default : a set of suspended else statements. σ |- c : denotes entailment checking.

21 The algorithm for interpreter 1. Run the reduction rules on the current, till no further reductions can take place. 2. If σ is inconsistent, return 0 (false). 3. If default is empty, return 1 (true). 4. Remove one statement from default – if c else A. If σ |- c, go to step 3. 5. Add A to Γ. Run the interpreter on the current state. If the result is 1 and σ |-/ c, return 1. 6. Undo the effects of the previous step by backtracking. Run the interpreter on the current state. If the result is 1 and σ |-/ c, return 1. Otherwise return 0.

22 A Hybrid cc program A is run as follows. 1. Run interpreter with Γ = A, and empty σ, next and default in the point phase. If the result is 0, abort. 2. Run the interval phase with Γ= next, as returned by the point phase. σ, next and default are again empty. If the result is 0, abort. Record all the tells, and also the ask constraints that were checked during the phase. 3. Integrate the arithmetic constraints that were told in the previous step,until one of the ask constraints changes status(i.e.goes from false to true or unknown, etc.). Go to step 1 with Γ= next.

23 Nonlinear equations zIndexicals zInterval splitting zThe Newton-Raphson method zThe Simplex method 上記の4つを組み合わせて、非線形型制約の充足処理を行 う

24 Indexicals z 与式 f(x,y)=0 を x=g(y) と書き換える方法 Example : x + y =0, x  [0,3], and y  [-1, -2]. Then the indexical x =-y is used to set to [1, 2].

25 Interval splitting z 区間 [a,b] を繰り返し分割する方法 za 1 :smallest number 0 ∈ f([a 1,b]) and a ≦ a 1. Hence,if 0 ∈ f([a,(a+b)/2]),then a 1 ∈ [a,(a+b)/2], otherwise a 1 ∈ [(a+b)/2,b] b 1 も同様に行う Example: x^2=1 and x  [-∞, ∞]. It follows that 0  [-∞, 0]. 0  / [-∞, -100]. a1 is determined to be in [-100, 0]. Eventually, a1 is determined to be –1.

26 The Newton-Raphson method z 方程式 f(x)=0 の近似解を求める方法 zInterval が狭い範囲で効果を発揮する 任意の関数 f(x) について、 f(x)=0 なる点 x を求める。 図のように適当な初期値 x0 において f(x) に接線を引けば、 接線の方程式は y-f(x 0 )=f’(x 0 )(x-x 0 ) である。したがって、 この接線と X 軸との交点 x 1 は y=0 とおいて、 x 1 =x 0 -f(x 0 )/f’(x 0 ) で与えられる。 次に x 1 での f(x) への接線と x 軸との交点を x 2 とする、という操作 を繰り返すと、交点は f(x)=0 の解に近づく。 i 番目の繰り返しで、 x i+1 =x i -f(x i )/f’(x i ) になるので、適当な ε を決めておき、 |x i+1 -x i |<ε になったら、 x i+1 を解とみなす。

27 Splitting & Newton-Raphson method zSplitting と Newton-Raphson method を組み合わせる (clp Newton) 。 Example: x^2=1 and x ∈ I=[-∞, ∞] 1. Split I recursively until I is split down to [-2,-1]. 2. Applying the N-R method=[-1,-1] is produced. 3. Similarly,[1,1] is produced. 4. Only –1 and 1 are solutions to the equation.

28 The Control Constructs and Execution of hcc zHcc の実行順序 1. Point Phase から始まる 2. Point Phase におけるすべての計算を行う 3. Interval Phase に移る 4. プログラム中の微分方程式の積分を行う (4th order Runge-Kutta 法 ) 5. 制約の状態が変化、制約の矛盾が発生、処理時間が経過した場合 に 積分を終える 6. 処理後、再び Point Phase に移る 同様に、 1 ~ 6 を繰り返す

29 Runge-Kutta algorithm ・ 4 次のルンゲクッタ法

30 Runge-Kutta algorithm (x i,y i ) (x i+1,y i+1 ) xixi x i+1/2 x i+1 k1k1 y(x) x k2k2 k3k3 k4k4 A B C D h/2 1. A 点における傾きから k 1 を計算 2. B 点 (x i +h/2,y i +k 1 /2) におけ る傾きから k 2 を計算 3. C 点 (x i +h/2,y i +k 2 /2) におけ る傾きから k 3 を計算 4. D 点 (x i +h,y i +k 3 ) における 傾きから k 4 を計算 5. k 1 ~k 4 の重み付き平均を 増分として、これを y i に加える ことにより y i+1 を求める k 1 =h(x i,y i ),k 2 =hf(x i +h/2,y i +k 1 /2), k 3 =hf(x i +h/2,y i +k 2 /2), k 4 =hf(t i +h,x i +k 3 ) y i +1=y i +(k 1 +2k 2 +2k 3 +k 4 )/6

31 Program Example ball.hcc /* A simple example of a ball bouncing on the floor. */ y = 10, y' = 0, // initial conditions hence { if y > 0 then y'' = -10, // free fall if y = 0 then // bounce if (prev(y') > -0.000001) then always y' = 0 // end of bouncing else y' = -0.5 * prev(y') }, sample(y)

32 ボールの軌道 (5 秒間 )

33 参考文献 zB.Carlson and V.Gupta.Hybrid cc with interval constraints. zV.Gupta,R.Jagadeesan,V.A.Saraswat,and D.Bobrow. Programming in hybrid concurrent constraint languages.In Panos Antsakis,Wolf Kohn,Anil Nerode,and Sankar Sastry,editors,Hybrid Systems Ⅱ,volume999 of Lecture Notes in Computer Science,1995 zV.Gupta,R.Jagadeesan,V.Saraswat,and D.Bobrow. Programming in hybrid constraint languages


Download ppt "Programming in hybrid constraint languages 上田研究室 M2 中村 好一."

Similar presentations


Ads by Google