teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

2

誤字を修正しました

2019/03/25 15:05

投稿

naaahaaa
naaahaaa

スコア10

title CHANGED
File without changes
body CHANGED
@@ -53,7 +53,7 @@
53
53
  cons = NonlinearConstraint(cons_f, lb, ub, jac="2-point")
54
54
  result = minimize(func, x0,
55
55
  constraints = cons,
56
- bounds=((0, 400), (0, 90)),
56
+ bounds=((0, 500), (0, 100)),
57
57
  method="trust-constr",
58
58
  jac="2-point")
59
59
  print(result)
@@ -77,21 +77,19 @@
77
77
  拘束条件の記述を次のように変更しても同様のエラーが発生しました.
78
78
  ```Python 3.7.2
79
79
  cons = (
80
- {'type': 'ineq', 'fun': lambda x: 500.0 - A_sp(x[0],x[1])},
80
+ {'type': 'ineq', 'fun': lambda x: 50.0 - A_sp(x[0],x[1])},
81
81
  {'type': 'ineq', 'fun': lambda x: B_sp(x[0],x[1]) - 30}
82
82
  )
83
83
  ```
84
- 上記の記述に変更かつ`method="slsqp"`と変更すると, エラー
84
+ 上記の記述に変更かつ`method="slsqp"`と変更すると, 同様のエラーが発生します.
85
- ```
86
- all the input arrays must have same number of dimensions
87
- ```
88
- が発生します.
89
85
 
90
86
  以下の場合ではエラーは発生しません.
91
87
  ```Python 3.7.2
92
88
  cons = (
93
89
  {'type': 'ineq', 'fun': lambda x:np.array(x[0]**2-x[1])}
94
90
  )
91
+
92
+ method="slsqp"
95
93
  ```
96
94
 
97
95
 

1

ご指摘ありがとうございます. 定数が抜けていたため, 追記しました.

2019/03/25 15:05

投稿

naaahaaa
naaahaaa

スコア10

title CHANGED
File without changes
body CHANGED
@@ -13,22 +13,41 @@
13
13
  import numpy as np
14
14
  from scipy import interpolate
15
15
  from scipy.optimize import minimize, NonlinearConstraint
16
+ xl= np.array([[0.0,100.0,200.0,300.0,400.0]])
17
+ yl= np.array([[0.0,25.0,50.0,75.0,100.0]])
18
+ a = np.array([[60.0,60.0,60.0,60.0,60.0], \
19
+ [100.0,90.0,70.0,45.0,40.0], \
20
+ [120.0,110.0,80.0,35.0,15.0], \
21
+ [130.0,120.0,100.0,40.0,5.0], \
22
+ [140.0,130.0,110.0,50.0,20.0], \
23
+ ])
16
- A_sp = interpolate.RectBivariateSpline(x, y, a)
24
+ b = np.array([[0.0,0.0, 0.0, 0.0, 0.0], \
17
- B_sp = interpolate.RectBivariateSpline(x, y, b)
25
+ [60.0, 70.0, 60.0, 45.0, 2.0], \
26
+ [110.0,150.0,150.0,100.0,5.0], \
27
+ [160.0,210.0,230.0,170.0,8.0], \
18
- C_sp = interpolate.RectBivariateSpline(x, y, c)
28
+ [200.0,270.0,300.0,240.0,10.0], \
19
- D_sp = interpolate.RectBivariateSpline(x, y, d)
29
+ ])
20
- #(値の取りうる範囲) 0≦x≦500, 0≦y≦100, 0≦A_sp≦1000, 0≦B_sp,C_sp,D_sp≦∞
30
+ c = np.array([[100.0,100.0,100.0,100.0,100.0], \
31
+ [200.0,180.0,140.0,120.0,120.0], \
32
+ [300.0,250.0,200.0,180.0,170.0], \
33
+ [350.0,310.0,250.0,240.0,230.0], \
34
+ [400.0,360.0,300.0,320.0,310.0], \
35
+ ])
21
36
 
37
+ A_sp = interpolate.RectBivariateSpline(xl, yl, a)
38
+ B_sp = interpolate.RectBivariateSpline(xl, yl, b)
39
+ C_sp = interpolate.RectBivariateSpline(xl, yl, c)
40
+ #(値の取りうる範囲) 0≦x≦400, 0≦y≦100
22
41
  def A(x):
23
42
  return A_sp(x[0],x[1])
24
43
  def B(x):
25
44
  return B_sp(x[0],x[1])
26
45
  def func(x):
27
- return C_sp(x[0],x[1]) + D_sp(x[0],x[1])
46
+ return C_sp(x[0],x[1])
28
47
  def cons_f(x):
29
48
  return [A_sp(x[0],x[1]),B_sp(x[0],x[1])]
30
49
  lb = [-np.inf, 30.00]
31
- ub = [500.00, np.inf]
50
+ ub = [50.00, np.inf]
32
51
 
33
52
  x0 = np.array([300.0,70.0], dtype=float)
34
53
  cons = NonlinearConstraint(cons_f, lb, ub, jac="2-point")