programming in hybrid constraint languages

33
Programming in hybrid constraint languages 上上上上上 M2 上上 上

Upload: georgette-demarion

Post on 01-Jan-2016

36 views

Category:

Documents


0 download

DESCRIPTION

Programming in hybrid constraint languages.    上田研究室  M2      中村 好一. The relations between the cc languages. cc cc Default cc Timed cc Default cc Timed cc = cc + defaults = cc + time - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Programming in hybrid constraint languages

Programming in hybrid constraint languages

   上田研究室  M2     中村 好一

Page 2: Programming in hybrid constraint languages

The relations between the cc languages

cc cc

Default cc Timed cc Default cc Timed cc

= cc + defaults = cc + time

Timed default ccHybrid cc Timed Default cc = cc + defaults = cc + defaults

+cont.time + discrete time Hybrid cc

Page 3: Programming in hybrid constraint languages

Hybrid cc

ハイブリッドシステムのモデリングと検証を行う言語として考案された

離散的・連続的な動作を表現できる

Example:  部屋の温度を管理するエアコン・部屋の温度は、連続的な微分方程式に従って変化する・誰かが窓を開ける、中にいる人が出て行くなどの行動

は 方程式を離散的に変化させる 

Page 4: Programming in hybrid constraint languages

The cc Programming Language

制約 store と Agent(tell,ask) から構成される計算モデルを持つ

Example program: x=10,x’=0,if x>0 then x’’=-10

X = 10X’ = 0

if x>0 then x’’=-10

if y=0 thenz=1

store

x’ = 0

X = 10 if x>0 then

x’’=-10

if y=0 thenz=1

storex’ = 0, x = 10

if x>0 then x’’=-10

if y=0 thenz=1

storex’ = 0, x = 10

x’’=-10if y=0 then

z=1

store

x’ = 0, x =10,x’’ = -10

if y=0 thenz=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).

Page 5: Programming in hybrid constraint languages

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

Page 6: Programming in hybrid constraint languages

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

Page 7: Programming in hybrid constraint languages

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)

Page 8: Programming in hybrid constraint languages

Prey&Predator

Figure:Prey&Predator

Page 9: Programming in hybrid constraint languages

Computational model

プログラムにおける処理順序

1. Point Phase( 離散的変化が起こる時点 ) 2. Interval Phase( 連続的変化が起こる時点 )

※ 1 , 2 を交互に繰り返し行う

Page 10: Programming in hybrid constraint languages

The Control Constructs and Execution of hcc

Hcc の実行順序

1. Point Phase から始まる

2. Point Phase におけるすべての計算を行う

3. Interval Phase に移る

4. プログラム中の微分方程式の積分を行う (4th order Runge-Kutta 法 )

5. 制約の状態が変化、制約の矛盾が発生、処理時間が経過した場合に

  積分を終える

6. 処理後、再び Point Phase に移る

同様に、 1 ~ 6 を繰り返す

Page 11: Programming in hybrid constraint languages

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

pointphase is started.

Page 12: Programming in hybrid constraint languages

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 nth derivative of Term(n is a natural number) .

Page 13: Programming in hybrid constraint languages

Ask Constraints(for Continuous Constraints)

AContConstr ::= Term ARelOp Term

ARelOp ::= = | >= |<= | < | > | !=

Page 14: Programming in hybrid constraint languages

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.

Page 15: Programming in hybrid constraint languages

Closure & Class definition

Closure definition: X = (A,x) AgentClass definition: X = (m,n)[A,m]

Agent

Page 16: Programming in hybrid constraint languages

Closure definition

X = (A,x)AgentExample: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)

Page 17: Programming in hybrid constraint languages

Class Definition

X = (m,n)[A,m] AgentExample: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) } }

Page 18: Programming in hybrid constraint languages

Boolean Constraints

BoolConstr ::= Atom | StrConstr | ContConstr |

Bool && BoolConstr | (BoolConstr)

ABoolConstr ::= Atom | StrConstr | AContConstr | ABool && ABoolConstr ABoolConstr || ABoolConstr | (ABoolConstr)

Page 19: Programming in hybrid constraint languages

Reduction Rules

Tell : <(Γ, c),σ,next, default → <Γ,σ U {c},next, default}

σ|- dAsk : <(Γ,if d then A),σ,next, default> → <(Γ,A),σ,next, default>

else : <(Γ,if d else A),σ,next,else> → <Γ,σ,next,(else, if d else A)>

Γ : 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.

Page 20: Programming in hybrid constraint languages

Reduction rules(for Hence)

The rule for hence A differs in point and interval phases

Hence Point : <(Γ,hence A), σ, next , default> → <Γ , σ, <next , hence A>, default>

Hence Interval : <(Γ, hence A), σ, next , default> → <(Γ, A), σ, (next , A , hence A), default> Γ : 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.

Page 21: Programming in hybrid constraint languages

The algorithm for interpreter

1. Run the reduction rules on the current <Γ,σ,next default>,

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.

Page 22: Programming in hybrid constraint languages

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.

Page 23: Programming in hybrid constraint languages

Nonlinear equations

Indexicals Interval splitting The Newton-Raphson method The Simplex method 上記の4つを組み合わせて、非線形型制約の充足処理を行

Page 24: Programming in hybrid constraint languages

Indexicals

与式 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].

Page 25: Programming in hybrid constraint languages

Interval splitting

区間 [a,b] を繰り返し分割する方法 a1:smallest number 0∈f([a1,b]) and a≦a1. Hence,if 0∈f([a,(a+b)/2]),then a1∈[a,(a+b)/2], otherwise a1∈[(a+b)/2,b] b1 も同様に行う

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.

Page 26: Programming in hybrid constraint languages

The Newton-Raphson method

方程式 f(x)=0 の近似解を求める方法 Interval が狭い範囲で効果を発揮する

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

Page 27: Programming in hybrid constraint languages

Splitting & Newton-Raphson method

Splitting と 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.

Page 28: Programming in hybrid constraint languages

The Control Constructs and Execution of hcc

Hcc の実行順序

1. Point Phase から始まる

2. Point Phase におけるすべての計算を行う

3. Interval Phase に移る

4. プログラム中の微分方程式の積分を行う (4th order Runge-Kutta 法 )

5. 制約の状態が変化、制約の矛盾が発生、処理時間が経過した場合に

  積分を終える

6. 処理後、再び Point Phase に移る

同様に、 1 ~ 6 を繰り返す

Page 29: Programming in hybrid constraint languages

Runge-Kutta algorithm

6

22 43211

kkkkyy ii

ii yxhfk ,1

2

,2

12

ky

hxhfk ii

2,

22

3

ky

hxhfk ii 34 , kyhxhfk ii

・ 4 次のルンゲクッタ法

Page 30: Programming in hybrid constraint languages

Runge-Kutta algorithm

(xi,yi)

(xi+1,yi+1)

xi xi+1/2 xi+1

k1

y(x)

x

k2

k3

k4

AB

C

D

h/2 h/2

1. A 点における傾きから  k1 を計算2. B 点 (xi+h/2,yi+k1/2) におけ  る傾きから k2 を計算3. C 点 (xi+h/2,yi+k2/2) におけ  る傾きから k3 を計算4. D 点 (xi+h,yi+k3) における  傾きから k4 を計算5. k1 ~k4 の重み付き平均を  増分として、これを yi に加える  ことにより yi+1 を求める

k1=h(xi,yi),k2=hf(xi+h/2,yi+k1/2), k3=hf(xi+h/2,yi+k2/2), k4=hf(ti+h,xi+k3) yi+1=yi+(k1+2k2+2k3+k4)/6

Page 31: Programming in hybrid constraint languages

Program Example

ball.hcc/* A simple example of a ball bouncing on the floor. */y = 10, y' = 0, // initial conditionshence { 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)

Page 32: Programming in hybrid constraint languages

0.00E+00

2.00E+00

4.00E+00

6.00E+00

8.00E+00

1.00E+01

1.20E+01

1系列

ボールの軌道 (5秒間 )

Page 33: Programming in hybrid constraint languages

参考文献

B.Carlson and V.Gupta.Hybrid cc with interval constraints.

V.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

V.Gupta,R.Jagadeesan,V.Saraswat,and D.Bobrow. Programming in hybrid constraint languages