我試圖在Haskell中使用automatic differentiation來處理非線性控制問題,但在使其運行時遇到一些問題。我基本上有一個cost
函數,應該在初始狀態下對其進行優化。該類型是:Numeric.AD - 類型變量轉義範圍
data Reference a = Reference a deriving Functor
data Plant a = Plant a deriving Functor
optimize :: (RealFloat a) => Reference a -> Plant a -> [a] -> [[a]]
optimize ref plant initialInputs = gradientDescent (cost ref plant) initialInputs
cost :: (RealFloat a) => Reference a -> Plant a -> [a] -> a
cost = ...
這將導致以下錯誤消息:
Couldn't match expected type `Reference
(Numeric.AD.Internal.Reverse.Reverse s a)'
with actual type `t'
because type variable `s' would escape its scope
This (rigid, skolem) type variable is bound by
a type expected by the context:
Data.Reflection.Reifies s Numeric.AD.Internal.Reverse.Tape =>
[Numeric.AD.Internal.Reverse.Reverse s a]
-> Numeric.AD.Internal.Reverse.Reverse s a
at test.hs:13:5-50
Relevant bindings include
initialInputs :: [a] (bound at test.hs:12:20)
ref :: t (bound at test.hs:12:10)
optimize :: t -> t1 -> [a] -> [[a]] (bound at test.hs:12:1)
In the first argument of `cost', namely `ref'
In the first argument of `gradientDescent', namely
`(cost ref plant)'
我甚至不知道我是否正確理解錯誤。難道,ref
和plant
的類型需要訪問s
,這是在gradientDescent
的第一個參數的範圍內嗎?
是否有可能使這項工作?雖然尋找一個解決方案,我試圖減少問題小例子,發現下面的定義會產生類似的錯誤消息:
optimize f inputs = gradientDescent f inputs
這似乎有點奇怪,因爲optimize = gradientDescent
不會產生任何錯誤。
什麼是'Reference'和'Plant'? – Cirdec
與http://stackoverflow.com/a/29393417/414413有關,這兩個問題都與'gradientdecent'的第一個參數的類型有關,它們基本上是相同的答案,但是提出問題的錯誤不同。 – Cirdec
'Reference'和'Plant'是Functors。更具體地說,'Reference'是一個簡單的代數數據類型,'Plant'是來自'netwire'庫的'Wire'的別名。 – bzn