2016-11-29 14 views
1

私は今、いくつかのコードで苦労しています。私は2D軌道のための凸最適化を実行しようとしています、そして、私は速度の差を最小にしようとしています。問題は、現在の速度の計算である(タイムステップで私の解ベクトルxを使用することにより、T-1とt + 1)が、私は凸ノルム法ベクター上のノルムを使用すると、CVXPYのエラーにつながる

import cvxpy as cvx 

#costFunction, sum integral 
def costFunction(x): 
    outputSum = 0.0 
    for i in range(1,amountSamplePoints): 
     outputSum = outputSum + (getJvel(i,x)) * 0.5 
    return outputSum 

#get velocity at time t 
def getVel(t,x): 
    x2=x[2*(t+1)] 
    y2=x[2*(t+1)+1] 
    x1=x[2*(t-1)] 
    y1=x[2*(t-1)+1] 
    #return cvx.norm(x[2*(t-1):2*t:1]) 
    return cvx.norm([x2-x1,y2-y1]) 

def getJvel(t,x): 
    return cvx.square(20.0-getVel(t,x)) 

# Problem data. 
x = cvx.Variable(12) 

# Construct the problem. 
objective = cvx.Minimize(costFunction(x)) 
constraints = [] 
prob = cvx.Problem(objective, constraints) 
prob.solve() 
を使用するための新しいベクトルを作成しようとすると問題が起こります

と私は得る:

>>> (executing file "sampleCode.py") 
Traceback (most recent call last): 
    File "/home/fornubuntu/Documents/Python Projects/sampleCode.py", line 26, in <module> 
    objective = cvx.Minimize(costFunction(x)) 
    File "/home/fornubuntu/Documents/Python Projects/sampleCode.py", line 7, in costFunction 
    outputSum = outputSum + (getJvel(i,x)) * 0.5 
    File "/home/fornubuntu/Documents/Python Projects/sampleCode.py", line 20, in getJvel 
    return cvx.square(20.0-getVel(t,x)) 
    File "/home/fornubuntu/Documents/Python Projects/sampleCode.py", line 17, in getVel 
    return cvx.norm([x2-x1,y2-y1]) 
    File "/home/fornubuntu/anaconda3/lib/python3.5/site-packages/cvxpy/atoms/norm.py", line 41, in norm 
    x = Expression.cast_to_const(x) 
    File "/home/fornubuntu/anaconda3/lib/python3.5/site-packages/cvxpy/expressions/expression.py", line 238, in cast_to_const 
    return expr if isinstance(expr, Expression) else cvxtypes.constant()(expr) 
    File "/home/fornubuntu/anaconda3/lib/python3.5/site-packages/cvxpy/expressions/constants/constant.py", line 42, in __init__ 
    self._value = intf.DEFAULT_INTF.const_to_matrix(value) 
    File "/home/fornubuntu/anaconda3/lib/python3.5/site-packages/cvxpy/interface/base_matrix_interface.py", line 47, in new_converter 
    if not convert_scalars and cvxpy.interface.matrix_utilities.is_scalar(value): 
    File "/home/fornubuntu/anaconda3/lib/python3.5/site-packages/cvxpy/interface/matrix_utilities.py", line 150, in is_scalar 
    return size(constant) == (1, 1) 
    File "/home/fornubuntu/anaconda3/lib/python3.5/site-packages/cvxpy/interface/matrix_utilities.py", line 131, in size 
    return (len(constant[0]), len(constant)) 
TypeError: object of type 'AddExpression' has no len() 

任意のアイデア/ヒント?事前

答えて

0

で おかげでたくさんの問題が行にある場合は、次のよう

return cvx.norm([x2-x1,y2-y1]) 

あなたはvstackを使用することができます。

return cvx.norm(cvx.vstack(x2-x1,y2-y1)) 

vstackはCVXを扱うことができるという配列を作成します。

関連する問題