質問編集履歴

4

コードの修正

2018/11/26 04:28

投稿

YYY9856
YYY9856

スコア13

test CHANGED
File without changes
test CHANGED
@@ -32,456 +32,586 @@
32
32
 
33
33
 
34
34
 
35
+ 0.01
36
+
35
37
  ---- iteration : 1 ----
36
38
 
39
+ [ 0. 0.01036504 0.03471855 0.07291493 0.12484478 0.19036281
40
+
41
+ 0.26928628 0.36139515 0.46643267 0.58410607 0.71408738 0.8560143
42
+
43
+ 1.00949126 1.17409048 1.34935321 1.53479094 1.72988684 1.93409712
44
+
45
+ 2.14685263 2.36756038 2.5956052 2.83035151 3.07114501 3.31731459
46
+
47
+ 3.5681741 3.82302438 4.08115509 4.34184679 4.60437288 4.86800166
48
+
49
+ 5.13199834 5.39562712 5.65815321 5.91884491 6.17697562 6.4318259
50
+
51
+ 6.68268541 6.92885499 7.16964849 7.4043948 7.63243962 7.85314737
52
+
53
+ 8.06590288 8.27011316 8.46520906 8.65064679 8.82590952 8.99050874
54
+
55
+ 9.1439857 9.28591262 9.41589393 9.53356733 9.63860485 9.73071372
56
+
57
+ 9.80963719 9.87515522 9.92708507 9.96528145 9.98963496 10. ]
58
+
59
+ <class 'numpy.ndarray'>
60
+
37
61
  Traceback (most recent call last):
38
62
 
39
- File "10_Low_Thrust_Orbit_Transfer16.py", line 259, in <module>
63
+ File "Low_Thrust_Orbit_Transfer17.py", line 222, in <module>
40
64
 
41
65
  prob.solve(obj, display_func, ftol=1e-12)
42
66
 
43
- File "C:\Users\AppData\Local\Programs\Python\Python36-32\lib\site-packages\OpenGoddard\optimize.py", line 683, in solve
67
+ File "C:\Users\yokotalab\AppData\Local\Programs\Python\Python36-32\lib\site-packages\OpenGoddard\optimize.py", line 683, in solve
44
68
 
45
69
  "ftol": ftol})
46
70
 
47
- File "C:\Users\AppData\Local\Programs\Python\Python36-32\lib\site-packages\scipy\optimize\_minimize.py", line 611, in minimize
71
+ File "C:\Users\yokotalab\AppData\Local\Programs\Python\Python36-32\lib\site-packages\scipy\optimize\_minimize.py", line 611, in minimize
48
72
 
49
73
  constraints, callback=callback, **options)
50
74
 
51
- File "C:\Users\AppData\Local\Programs\Python\Python36-32\lib\site-packages\scipy\optimize\slsqp.py", line 313, in _minimize_slsqp
75
+ File "C:\Users\yokotalab\AppData\Local\Programs\Python\Python36-32\lib\site-packages\scipy\optimize\slsqp.py", line 313, in _minimize_slsqp
52
76
 
53
77
  for c in cons['eq']]))
54
78
 
55
- File "C:\Users\AppData\Local\Programs\Python\Python36-32\lib\site-packages\scipy\optimize\slsqp.py", line 313, in <listcomp>
79
+ File "C:\Users\yokotalab\AppData\Local\Programs\Python\Python36-32\lib\site-packages\scipy\optimize\slsqp.py", line 313, in <listcomp>
56
80
 
57
81
  for c in cons['eq']]))
58
82
 
59
- File "C:\Users\AppData\Local\Programs\Python\Python36-32\lib\site-packages\OpenGoddard\optimize.py", line 649, in for_solver
83
+ File "C:\Users\yokotalab\AppData\Local\Programs\Python\Python36-32\lib\site-packages\OpenGoddard\optimize.py", line 649, in for_solver
60
84
 
61
85
  return func(arg0, arg1)
62
86
 
63
- File "C:\Users\AppData\Local\Programs\Python\Python36-32\lib\site-packages\OpenGoddard\optimize.py", line 620, in equality_add
87
+ File "C:\Users\yokotalab\AppData\Local\Programs\Python\Python36-32\lib\site-packages\OpenGoddard\optimize.py", line 620, in equality_add
64
88
 
65
89
  dx = self.dynamics[i](self, obj, i)
66
90
 
67
- File "10_Low_Thrust_Orbit_Transfer16.py", line 68, in dynamics
91
+ File "Low_Thrust_Orbit_Transfer17.py", line 56, in dynamics
68
92
 
69
93
  s = optimize.fsolve(h,0)
70
94
 
71
- File "C:\Users\AppData\Local\Programs\Python\Python36-32\lib\site-packages\scipy\optimize\minpack.py", line 148, in fsolve
95
+ File "C:\Users\yokotalab\AppData\Local\Programs\Python\Python36-32\lib\site-packages\scipy\optimize\minpack.py", line 148, in fsolve
72
96
 
73
97
  res = _root_hybr(func, x0, args, jac=fprime, **options)
74
98
 
75
- File "C:\Users\AppData\Local\Programs\Python\Python36-32\lib\site-packages\scipy\optimize\minpack.py", line 214, in _root_hybr
76
-
77
- shape, dtype = _check_func('fsolve', 'func', func, x0, args, n, (n,))
78
-
79
- File "C:\Users\AppData\Local\Programs\Python\Python36-32\lib\site-packages\scipy\optimize\minpack.py", line 27, in _check_func
80
-
81
- res = atleast_1d(thefunc(*((x0[:numinputs],) + args)))
82
-
83
- File "10_Low_Thrust_Orbit_Transfer16.py", line 65, in h
99
+ File "C:\Users\yokotalab\AppData\Local\Programs\Python\Python36-32\lib\site-packages\scipy\optimize\minpack.py", line 227, in _root_hybr
100
+
101
+ ml, mu, epsfcn, factor, diag)
102
+
103
+ ValueError: The array returned by a function changed size between calls
104
+
105
+
106
+
107
+
108
+
109
+ class Orbiter:
110
+
111
+ def __init__(self):
112
+
113
+
114
+
115
+ self.a0 = 1.0
116
+
117
+ self.vr0 = 0.0
118
+
119
+ self.vt0 = 1.0
120
+
121
+ self.af = 4.0
122
+
123
+ self.vrf = 0.0
124
+
125
+ self.vtf = 0.5
126
+
127
+ self.e_0 = 0.7
128
+
129
+ self.ef = 0.0
130
+
131
+ self.tf_max = 1000
132
+
133
+ self.mu = 3.98e14
134
+
135
+ self.R = 6678000
136
+
137
+ self.u_max = 0.01
138
+
139
+ print(self.u_max)
140
+
141
+
142
+
143
+
144
+
145
+ def dynamics(prob, obj, section):
146
+
147
+
148
+
149
+ a = prob.states(0, section)
150
+
151
+ vr = prob.states(1, section)
152
+
153
+ vt = prob.states(2, section)
154
+
155
+ beta = prob.states(3, section)
156
+
157
+ e = prob.states(4, section)
158
+
159
+ ur1 = prob.controls(0, section)
160
+
161
+ ur2 = prob.controls(1, section)
162
+
163
+ ut1 = prob.controls(2, section)
164
+
165
+ ut2 = prob.controls(3, section)
166
+
167
+
168
+
169
+
170
+
171
+
172
+
173
+ t = prob.time_update()#ここから
174
+
175
+
176
+
177
+ print(t)
178
+
179
+ print(type(t))
180
+
181
+
182
+
183
+ def f(E):
184
+
185
+ return E - np.cos(E)
186
+
187
+ def g(E):
188
+
189
+ return np.sqrt(obj.mu/obj.R**3)* t
190
+
191
+ def h(E):
192
+
193
+ return f(E)-g(E)
194
+
195
+
196
+
197
+
198
+
199
+ s = optimize.fsolve(h,0)
200
+
201
+
202
+
203
+
204
+
205
+ r = a * (1 -e * np.cos(s))#ここまで
206
+
207
+
208
+
209
+ dx[0] = vr
210
+
211
+ dx[1] = vt**2 / r - 1 / r**2 + (ur1 - ur2) -(ui1 - ui2) / np.cos(beta) * np.sin(beta)
212
+
213
+ dx[2] = - vr * vt / r + (ut1 - ut2)
214
+
215
+
216
+
217
+ return dx()
218
+
219
+
220
+
221
+
222
+
223
+ def equality(prob, obj):
224
+
225
+ a = prob.states_all_section(0)
226
+
227
+ vr = prob.states_all_section(1)
228
+
229
+ vt = prob.states_all_section(2)
230
+
231
+ e = prob.states_all_section(3)
232
+
233
+ ur1 = prob.controls_all_section(0)
234
+
235
+ ur2 = prob.controls_all_section(1)
236
+
237
+ ut1 = prob.controls_all_section(2)
238
+
239
+ ut2 = prob.controls_all_section(3)
240
+
241
+
242
+
243
+ tf = prob.time_final(-1)
244
+
245
+
246
+
247
+ result = Condition()
248
+
249
+
250
+
251
+ # event condition
252
+
253
+ result.equal(a[0], obj.a0)
254
+
255
+ result.equal(vr[0], obj.vr0)
256
+
257
+ result.equal(vt[0], obj.vt0)
258
+
259
+ result.equal(e[0], obj.e_0)
260
+
261
+ result.equal(a[-1], obj.af)
262
+
263
+ result.equal(vr[-1], obj.vrf)
264
+
265
+ result.equal(vt[-1], obj.vtf)
266
+
267
+ result.equal(e[-1], obj.ef)
268
+
269
+
270
+
271
+
272
+
273
+ return result()
274
+
275
+
276
+
277
+
278
+
279
+ def inequality(prob, obj):
280
+
281
+ a = prob.states_all_section(0)
282
+
283
+ vr = prob.states_all_section(1)
284
+
285
+ vt = prob.states_all_section(2)
286
+
287
+ e = prob.states_all_section(3)
288
+
289
+ ur1 = prob.controls_all_section(0)
290
+
291
+ ur2 = prob.controls_all_section(1)
292
+
293
+ ut1 = prob.controls_all_section(2)
294
+
295
+ ut2 = prob.controls_all_section(3)
296
+
297
+
298
+
299
+ tf = prob.time_final(-1)
300
+
301
+
302
+
303
+ result = Condition()
304
+
305
+
306
+
307
+ # lower bounds
308
+
309
+ result.lower_bound(a, obj.a0)
310
+
311
+ result.lower_bound(e, 0.0)
312
+
313
+ result.lower_bound(ur1, 0.0)
314
+
315
+ result.lower_bound(ut1, 0.0)
316
+
317
+ result.lower_bound(ur2, 0.0)
318
+
319
+ result.lower_bound(ut2, 0.0)
320
+
321
+ result.lower_bound(tf, 0.0)
322
+
323
+
324
+
325
+ # upper bounds
326
+
327
+ result.upper_bound(a, obj.af)
328
+
329
+ result.upper_bound(e, obj.e_0)
330
+
331
+ result.upper_bound(ur1, obj.u_max)
332
+
333
+ result.upper_bound(ut1, obj.u_max)
334
+
335
+ result.upper_bound(ur2, obj.u_max)
336
+
337
+ result.upper_bound(ut2, obj.u_max)
338
+
339
+
340
+
341
+
342
+
343
+
344
+
345
+ result.upper_bound(tf, obj.tf_max)
346
+
347
+
348
+
349
+ return result()
350
+
351
+
352
+
353
+
354
+
355
+ def cost(prob, obj):
356
+
357
+ return 0.0
358
+
359
+
360
+
361
+
362
+
363
+ def running_cost(prob, obj):
364
+
365
+
366
+
367
+ tf = prob.time_final(-1)
368
+
369
+
370
+
371
+ return tf
372
+
373
+
374
+
375
+
376
+
377
+
378
+
379
+ # ========================
380
+
381
+ plt.close("all")
382
+
383
+ plt.ion()
384
+
385
+ # Program Starting Point
386
+
387
+ time_init = [0.0, 10]
388
+
389
+ n = [60]
390
+
391
+ num_states = [5]
392
+
393
+ num_controls = [4]
394
+
395
+ max_iteration = 8
396
+
397
+
398
+
399
+ flag_savefig = True
400
+
401
+
402
+
403
+ savefig_dir = "10_Low_Thrust_Orbit_Transfer/"
404
+
405
+
406
+
407
+ # ------------------------
408
+
409
+ # set OpenGoddard class for algorithm determination
410
+
411
+ prob = Problem(time_init, n, num_states, num_controls, max_iteration)
412
+
413
+ obj = Orbiter()
414
+
415
+
416
+
417
+ # ========================
418
+
419
+ # Initial parameter guess
420
+
421
+ a_init = Guess.linear(prob.time_all_section, obj.a0, obj.af)
422
+
423
+ # Guess.plot(prob.time_all_section, r_init, "r", "time", "r")
424
+
425
+ # if(flag_savefig):plt.savefig(savefig_dir + "guess_r" + savefig_add + ".png")
426
+
427
+
428
+
429
+ vr_init = Guess.linear(prob.time_all_section, obj.vr0, obj.vrf)
430
+
431
+ # Guess.plot(prob.time_all_section, vr_init, "vr", "time", "vr")
432
+
433
+ # if(flag_savefig):plt.savefig(savefig_dir + "guess_vr" + savefig_add + ".png")
434
+
435
+
436
+
437
+ vt_init = Guess.linear(prob.time_all_section, obj.vt0, obj.vtf)
438
+
439
+ # Guess.plot(prob.time_all_section, theta_init, "vt", "time", "vt")
440
+
441
+ # if(flag_savefig):plt.savefig(savefig_dir + "guess_vt" + savefig_add + ".png")
442
+
443
+
444
+
445
+
446
+
447
+ ur1_init = Guess.linear(prob.time_all_section, obj.u_max, obj.u_max)
448
+
449
+ # Guess.plot(prob.time_all_section, ur1_init, "ur1", "time", "ur1")
450
+
451
+ # if(flag_savefig):plt.savefig(savefig_dir + "guess_ur1" + savefig_add + ".png")
452
+
453
+
454
+
455
+ ut1_init = Guess.linear(prob.time_all_section, obj.u_max, obj.u_max)
456
+
457
+ # Guess.plot(prob.time_all_section, ut1_init, "ut1", "time", "ut1")
458
+
459
+ # if(flag_savefig):plt.savefig(savefig_dir + "guess_ut1" + savefig_add + ".png")
460
+
461
+
462
+
463
+
464
+
465
+ e_init = Guess.linear(prob.time_all_section, obj.e_0, obj.ef)
466
+
467
+
468
+
469
+
470
+
471
+
472
+
473
+
474
+
475
+
476
+
477
+ prob.set_states_all_section(0, a_init)
478
+
479
+ prob.set_states_all_section(1, vr_init)
480
+
481
+ prob.set_states_all_section(2, vt_init)
482
+
483
+ prob.set_states_all_section(3, e_init)
484
+
485
+
486
+
487
+
488
+
489
+
490
+
491
+ prob.set_controls_all_section(0, ur1_init)
492
+
493
+ prob.set_controls_all_section(2, ut1_init)
494
+
495
+
496
+
497
+
498
+
499
+ # ========================
500
+
501
+ # Main Process
502
+
503
+ # Assign problem to SQP solver
504
+
505
+ prob.dynamics = [dynamics]
506
+
507
+ prob.knot_states_smooth = []
508
+
509
+ prob.cost = cost
510
+
511
+ prob.running_cost = running_cost
512
+
513
+ prob.equality = equality
514
+
515
+ prob.inequality = inequality
516
+
517
+
518
+
519
+
520
+
521
+ def display_func():
522
+
523
+ tf = prob.time_final(-1)
524
+
525
+ print("tf: {0:.5f}".format(tf))
526
+
527
+
528
+
529
+
530
+
531
+ prob.solve(obj, display_func, ftol=1e-12)
532
+
533
+
534
+
535
+ # ========================
536
+
537
+ # Post Process
538
+
539
+ # ------------------------
540
+
541
+ # Convert parameter vector to variable
542
+
543
+ a = prob.states_all_section(0)
544
+
545
+ vr = prob.states_all_section(1)
546
+
547
+ vt = prob.states_all_section(2)
548
+
549
+ beta = prob.states_all_section(3)
550
+
551
+ e = prob.states_all_section(4)
552
+
553
+
554
+
555
+
556
+
557
+
558
+
559
+ ur1 = prob.controls_all_section(0)
560
+
561
+ ur2 = prob.controls_all_section(1)
562
+
563
+ ut1 = prob.controls_all_section(2)
564
+
565
+ ut2 = prob.controls_all_section(3)
566
+
567
+
568
+
569
+ time = prob.time_update()
570
+
571
+
572
+
573
+ def f(E):
574
+
575
+ return E - np.cos(E)
576
+
577
+ def g(E):
578
+
579
+ return np.sqrt(obj.mu/obj.R**3)* time
580
+
581
+ def h(E):
84
582
 
85
583
  return f(E)-g(E)
86
584
 
87
- File "10_Low_Thrust_Orbit_Transfer16.py", line 63, in g
88
-
89
- return np.sqrt(obj.R**3/a**3)* t
90
-
91
- AttributeError: 'float' object has no attribute 'sqrt'
92
-
93
-
94
-
95
- ### 該当のソースコード
96
-
97
-
98
-
99
- python
100
-
101
-
102
-
103
- from __future__ import print_function
104
-
105
- import numpy as np
106
-
107
- import matplotlib.pyplot as plt
108
-
109
- from OpenGoddard.optimize import Problem, Guess, Condition, Dynamics
110
-
111
-
112
-
113
-
114
-
115
- class Orbiter:
116
-
117
- def __init__(self):
118
-
119
- self.u_max = 0.01
120
-
121
- self.r0 = 1.0
122
-
123
- self.vr0 = 0.0
124
-
125
- self.vt0 = 1.0
126
-
127
- self.rf = 4.0
128
-
129
- self.vrf = 0.0
130
-
131
- self.vtf = 0.5
132
-
133
- self.tf_max = 55
134
-
135
-
136
-
137
-
138
-
139
- def dynamics(prob, obj, section):
140
-
141
- r = prob.states(0, section)
142
-
143
- vr = prob.states(1, section)
144
-
145
- vt = prob.states(2, section)
146
-
147
- ur1 = prob.controls(0, section)
148
-
149
- ur2 = prob.controls(1, section)
150
-
151
- ut1 = prob.controls(2, section)
152
-
153
- ut2 = prob.controls(3, section)
154
-
155
-
156
-
157
- t = prob.time_knots()#ここから
158
-
159
-
160
-
161
- def f(E):
162
-
163
- return E - np.cos(E)
164
-
165
- def g(E):
166
-
167
- return np.sqrt(obj.R**3/a**3)* t
168
-
169
- def h(E):
170
-
171
- return f(E)-g(E)
172
-
173
-
174
-
175
-
176
-
177
- s = optimize.fsolve(h,0)
178
-
179
-
180
-
181
-
182
-
183
- r = a * (1 -e * np.cos(s))#ここまで
184
-
185
-
186
-
187
- dx = Dynamics(prob, section)
188
-
189
- dx[0] = vr
190
-
191
- dx[1] = vt**2 / r - 1 / r**2 + (ur1 - ur2)
192
-
193
- dx[2] = - vr * vt / r + (ut1 - ut2)
194
-
195
- return dx()
196
-
197
-
198
-
199
-
200
-
201
- def equality(prob, obj):
202
-
203
- r = prob.states_all_section(0)
204
-
205
- vr = prob.states_all_section(1)
206
-
207
- vt = prob.states_all_section(2)
208
-
209
- ur1 = prob.controls_all_section(0)
210
-
211
- ur2 = prob.controls_all_section(1)
212
-
213
- ut1 = prob.controls_all_section(2)
214
-
215
- ut2 = prob.controls_all_section(3)
216
-
217
- tf = prob.time_final(-1)
218
-
219
-
220
-
221
- result = Condition()
222
-
223
-
224
-
225
- # event condition
226
-
227
- result.equal(r[0], obj.r0)
228
-
229
- result.equal(vr[0], obj.vr0)
230
-
231
- result.equal(vt[0], obj.vt0)
232
-
233
- result.equal(r[-1], obj.rf)
234
-
235
- result.equal(vr[-1], obj.vrf)
236
-
237
- result.equal(vt[-1], obj.vtf)
238
-
239
-
240
-
241
- return result()
242
-
243
-
244
-
245
-
246
-
247
- def inequality(prob, obj):
248
-
249
- r = prob.states_all_section(0)
250
-
251
- vr = prob.states_all_section(1)
252
-
253
- vt = prob.states_all_section(2)
254
-
255
- ur1 = prob.controls_all_section(0)
256
-
257
- ur2 = prob.controls_all_section(1)
258
-
259
- ut1 = prob.controls_all_section(2)
260
-
261
- ut2 = prob.controls_all_section(3)
262
-
263
- tf = prob.time_final(-1)
264
-
265
-
266
-
267
- result = Condition()
268
-
269
-
270
-
271
- # lower bounds
272
-
273
- result.lower_bound(r, obj.r0)
274
-
275
- result.lower_bound(ur1, 0.0)
276
-
277
- result.lower_bound(ut1, 0.0)
278
-
279
- result.lower_bound(ur2, 0.0)
280
-
281
- result.lower_bound(ut2, 0.0)
282
-
283
- result.lower_bound(tf, 0.0)
284
-
285
-
286
-
287
- # upper bounds
288
-
289
- result.upper_bound(r, obj.rf)
290
-
291
- result.upper_bound(ur1, obj.u_max)
292
-
293
- result.upper_bound(ut1, obj.u_max)
294
-
295
- result.upper_bound(ur2, obj.u_max)
296
-
297
- result.upper_bound(ut2, obj.u_max)
298
-
299
- result.upper_bound(tf, obj.tf_max)
300
-
301
-
302
-
303
- return result()
304
-
305
-
306
-
307
-
308
-
309
- def cost(prob, obj):
310
-
311
- return 0.0
312
-
313
-
314
-
315
-
316
-
317
-
318
-
319
- def running_cost(prob, obj):
320
-
321
-
322
-
323
- tf = prob.time_final(-1)
324
-
325
-
326
-
327
- return tf
328
-
329
-
330
-
331
-
332
-
333
- # ========================
334
-
335
- plt.close("all")
336
-
337
- plt.ion()
338
-
339
- # Program Starting Point
340
-
341
- time_init = [0.0, 10]
342
-
343
- n = [60]
344
-
345
- num_states = [3]
346
-
347
- num_controls = [4]
348
-
349
- max_iteration = 10
350
-
351
-
352
-
353
- flag_savefig = True
354
-
355
-
356
-
357
- savefig_dir = "10_Low_Thrust_Orbit_Transfer/"
358
-
359
-
360
-
361
- # ------------------------
362
-
363
- # set OpenGoddard class for algorithm determination
364
-
365
- prob = Problem(time_init, n, num_states, num_controls, max_iteration)
366
-
367
- obj = Orbiter()
368
-
369
-
370
-
371
- # ========================
372
-
373
- # Initial parameter guess
374
-
375
- r_init = Guess.linear(prob.time_all_section, obj.r0, obj.rf)
376
-
377
- # Guess.plot(prob.time_all_section, r_init, "r", "time", "r")
378
-
379
- # if(flag_savefig):plt.savefig(savefig_dir + "guess_r" + savefig_add + ".png")
380
-
381
-
382
-
383
- vr_init = Guess.linear(prob.time_all_section, obj.vr0, obj.vrf)
384
-
385
- # Guess.plot(prob.time_all_section, vr_init, "vr", "time", "vr")
386
-
387
- # if(flag_savefig):plt.savefig(savefig_dir + "guess_vr" + savefig_add + ".png")
388
-
389
-
390
-
391
- vt_init = Guess.linear(prob.time_all_section, obj.vt0, obj.vtf)
392
-
393
- # Guess.plot(prob.time_all_section, theta_init, "vt", "time", "vt")
394
-
395
- # if(flag_savefig):plt.savefig(savefig_dir + "guess_vt" + savefig_add + ".png")
396
-
397
-
398
-
399
- ur1_init = Guess.linear(prob.time_all_section, obj.u_max, obj.u_max)
400
-
401
- # Guess.plot(prob.time_all_section, ur1_init, "ur1", "time", "ur1")
402
-
403
- # if(flag_savefig):plt.savefig(savefig_dir + "guess_ur1" + savefig_add + ".png")
404
-
405
-
406
-
407
- ut1_init = Guess.linear(prob.time_all_section, obj.u_max, obj.u_max)
408
-
409
- # Guess.plot(prob.time_all_section, ut1_init, "ut1", "time", "ut1")
410
-
411
- # if(flag_savefig):plt.savefig(savefig_dir + "guess_ut1" + savefig_add + ".png")
412
-
413
-
414
-
415
- prob.set_states_all_section(0, r_init)
416
-
417
- prob.set_states_all_section(1, vr_init)
418
-
419
- prob.set_states_all_section(2, vt_init)
420
-
421
- prob.set_controls_all_section(0, ur1_init)
422
-
423
- prob.set_controls_all_section(2, ut1_init)
424
-
425
-
426
-
427
-
428
-
429
- # ========================
430
-
431
- # Main Process
432
-
433
- # Assign problem to SQP solver
434
-
435
- prob.dynamics = [dynamics]
436
-
437
- prob.knot_states_smooth = []
438
-
439
- prob.cost = cost
440
-
441
- prob.running_cost = running_cost
442
-
443
- prob.equality = equality
444
-
445
- prob.inequality = inequality
446
-
447
-
448
-
449
-
450
-
451
- def display_func():
452
-
453
- tf = prob.time_final(-1)
454
-
455
- print("tf: {0:.5f}".format(tf))
456
-
457
-
458
-
459
-
460
-
461
- prob.solve(obj, display_func, ftol=1e-12)
462
-
463
-
464
-
465
- # ========================
466
-
467
- # Post Process
468
-
469
- # ------------------------
470
-
471
- # Convert parameter vector to variable
472
-
473
- r = prob.states_all_section(0)
474
-
475
- vr = prob.states_all_section(1)
476
-
477
- vt = prob.states_all_section(2)
478
-
479
- ur1 = prob.controls_all_section(0)
480
-
481
- ur2 = prob.controls_all_section(1)
482
-
483
- ut1 = prob.controls_all_section(2)
484
-
485
- ut2 = prob.controls_all_section(3)
486
-
487
- time = prob.time_update()
585
+
586
+
587
+
588
+
589
+ s = optimize.fsolve(h,0)
590
+
591
+ print(s)
592
+
593
+ r = a * (1 -e * np.cos(s))
594
+
595
+
596
+
597
+
598
+
599
+ aa= sum(np.abs(ut1)+np.abs(ut2))
600
+
601
+ b= sum(np.abs(ur1)+np.abs(ur2))
602
+
603
+ c= sum(np.abs(ui1)+np.abs(ui2))
604
+
605
+
606
+
607
+ print(aa*0.001*obj.mu / obj.R**2)
608
+
609
+ print((b+c)*0.001*obj.mu / obj.R**2)
610
+
611
+
612
+
613
+
614
+
615
+
616
+
617
+ plt.show()

3

ソースの追加

2018/11/26 04:28

投稿

YYY9856
YYY9856

スコア13

test CHANGED
File without changes
test CHANGED
@@ -14,12 +14,20 @@
14
14
 
15
15
  エラーメッセージの読み方が分かりません。
16
16
 
17
- つまりどのファイルのどの行に何が起こっていると言われているのか
17
+ つまりどのファイルのどの行に何が起こっていると言われているのか
18
18
 
19
19
  教えていただきたいです。
20
20
 
21
21
 
22
22
 
23
+ ソースコードが長いですが、コード内の#ここから #ここまでを追加するとこのエラーが出るようになりました。
24
+
25
+ OpenGoddard.optimizeは以下のコードです。
26
+
27
+ https://github.com/istellartech/OpenGoddard/blob/master/OpenGoddard/optimize.py
28
+
29
+
30
+
23
31
  エラーメッセージ
24
32
 
25
33
 
@@ -89,3 +97,391 @@
89
97
 
90
98
 
91
99
  python
100
+
101
+
102
+
103
+ from __future__ import print_function
104
+
105
+ import numpy as np
106
+
107
+ import matplotlib.pyplot as plt
108
+
109
+ from OpenGoddard.optimize import Problem, Guess, Condition, Dynamics
110
+
111
+
112
+
113
+
114
+
115
+ class Orbiter:
116
+
117
+ def __init__(self):
118
+
119
+ self.u_max = 0.01
120
+
121
+ self.r0 = 1.0
122
+
123
+ self.vr0 = 0.0
124
+
125
+ self.vt0 = 1.0
126
+
127
+ self.rf = 4.0
128
+
129
+ self.vrf = 0.0
130
+
131
+ self.vtf = 0.5
132
+
133
+ self.tf_max = 55
134
+
135
+
136
+
137
+
138
+
139
+ def dynamics(prob, obj, section):
140
+
141
+ r = prob.states(0, section)
142
+
143
+ vr = prob.states(1, section)
144
+
145
+ vt = prob.states(2, section)
146
+
147
+ ur1 = prob.controls(0, section)
148
+
149
+ ur2 = prob.controls(1, section)
150
+
151
+ ut1 = prob.controls(2, section)
152
+
153
+ ut2 = prob.controls(3, section)
154
+
155
+
156
+
157
+ t = prob.time_knots()#ここから
158
+
159
+
160
+
161
+ def f(E):
162
+
163
+ return E - np.cos(E)
164
+
165
+ def g(E):
166
+
167
+ return np.sqrt(obj.R**3/a**3)* t
168
+
169
+ def h(E):
170
+
171
+ return f(E)-g(E)
172
+
173
+
174
+
175
+
176
+
177
+ s = optimize.fsolve(h,0)
178
+
179
+
180
+
181
+
182
+
183
+ r = a * (1 -e * np.cos(s))#ここまで
184
+
185
+
186
+
187
+ dx = Dynamics(prob, section)
188
+
189
+ dx[0] = vr
190
+
191
+ dx[1] = vt**2 / r - 1 / r**2 + (ur1 - ur2)
192
+
193
+ dx[2] = - vr * vt / r + (ut1 - ut2)
194
+
195
+ return dx()
196
+
197
+
198
+
199
+
200
+
201
+ def equality(prob, obj):
202
+
203
+ r = prob.states_all_section(0)
204
+
205
+ vr = prob.states_all_section(1)
206
+
207
+ vt = prob.states_all_section(2)
208
+
209
+ ur1 = prob.controls_all_section(0)
210
+
211
+ ur2 = prob.controls_all_section(1)
212
+
213
+ ut1 = prob.controls_all_section(2)
214
+
215
+ ut2 = prob.controls_all_section(3)
216
+
217
+ tf = prob.time_final(-1)
218
+
219
+
220
+
221
+ result = Condition()
222
+
223
+
224
+
225
+ # event condition
226
+
227
+ result.equal(r[0], obj.r0)
228
+
229
+ result.equal(vr[0], obj.vr0)
230
+
231
+ result.equal(vt[0], obj.vt0)
232
+
233
+ result.equal(r[-1], obj.rf)
234
+
235
+ result.equal(vr[-1], obj.vrf)
236
+
237
+ result.equal(vt[-1], obj.vtf)
238
+
239
+
240
+
241
+ return result()
242
+
243
+
244
+
245
+
246
+
247
+ def inequality(prob, obj):
248
+
249
+ r = prob.states_all_section(0)
250
+
251
+ vr = prob.states_all_section(1)
252
+
253
+ vt = prob.states_all_section(2)
254
+
255
+ ur1 = prob.controls_all_section(0)
256
+
257
+ ur2 = prob.controls_all_section(1)
258
+
259
+ ut1 = prob.controls_all_section(2)
260
+
261
+ ut2 = prob.controls_all_section(3)
262
+
263
+ tf = prob.time_final(-1)
264
+
265
+
266
+
267
+ result = Condition()
268
+
269
+
270
+
271
+ # lower bounds
272
+
273
+ result.lower_bound(r, obj.r0)
274
+
275
+ result.lower_bound(ur1, 0.0)
276
+
277
+ result.lower_bound(ut1, 0.0)
278
+
279
+ result.lower_bound(ur2, 0.0)
280
+
281
+ result.lower_bound(ut2, 0.0)
282
+
283
+ result.lower_bound(tf, 0.0)
284
+
285
+
286
+
287
+ # upper bounds
288
+
289
+ result.upper_bound(r, obj.rf)
290
+
291
+ result.upper_bound(ur1, obj.u_max)
292
+
293
+ result.upper_bound(ut1, obj.u_max)
294
+
295
+ result.upper_bound(ur2, obj.u_max)
296
+
297
+ result.upper_bound(ut2, obj.u_max)
298
+
299
+ result.upper_bound(tf, obj.tf_max)
300
+
301
+
302
+
303
+ return result()
304
+
305
+
306
+
307
+
308
+
309
+ def cost(prob, obj):
310
+
311
+ return 0.0
312
+
313
+
314
+
315
+
316
+
317
+
318
+
319
+ def running_cost(prob, obj):
320
+
321
+
322
+
323
+ tf = prob.time_final(-1)
324
+
325
+
326
+
327
+ return tf
328
+
329
+
330
+
331
+
332
+
333
+ # ========================
334
+
335
+ plt.close("all")
336
+
337
+ plt.ion()
338
+
339
+ # Program Starting Point
340
+
341
+ time_init = [0.0, 10]
342
+
343
+ n = [60]
344
+
345
+ num_states = [3]
346
+
347
+ num_controls = [4]
348
+
349
+ max_iteration = 10
350
+
351
+
352
+
353
+ flag_savefig = True
354
+
355
+
356
+
357
+ savefig_dir = "10_Low_Thrust_Orbit_Transfer/"
358
+
359
+
360
+
361
+ # ------------------------
362
+
363
+ # set OpenGoddard class for algorithm determination
364
+
365
+ prob = Problem(time_init, n, num_states, num_controls, max_iteration)
366
+
367
+ obj = Orbiter()
368
+
369
+
370
+
371
+ # ========================
372
+
373
+ # Initial parameter guess
374
+
375
+ r_init = Guess.linear(prob.time_all_section, obj.r0, obj.rf)
376
+
377
+ # Guess.plot(prob.time_all_section, r_init, "r", "time", "r")
378
+
379
+ # if(flag_savefig):plt.savefig(savefig_dir + "guess_r" + savefig_add + ".png")
380
+
381
+
382
+
383
+ vr_init = Guess.linear(prob.time_all_section, obj.vr0, obj.vrf)
384
+
385
+ # Guess.plot(prob.time_all_section, vr_init, "vr", "time", "vr")
386
+
387
+ # if(flag_savefig):plt.savefig(savefig_dir + "guess_vr" + savefig_add + ".png")
388
+
389
+
390
+
391
+ vt_init = Guess.linear(prob.time_all_section, obj.vt0, obj.vtf)
392
+
393
+ # Guess.plot(prob.time_all_section, theta_init, "vt", "time", "vt")
394
+
395
+ # if(flag_savefig):plt.savefig(savefig_dir + "guess_vt" + savefig_add + ".png")
396
+
397
+
398
+
399
+ ur1_init = Guess.linear(prob.time_all_section, obj.u_max, obj.u_max)
400
+
401
+ # Guess.plot(prob.time_all_section, ur1_init, "ur1", "time", "ur1")
402
+
403
+ # if(flag_savefig):plt.savefig(savefig_dir + "guess_ur1" + savefig_add + ".png")
404
+
405
+
406
+
407
+ ut1_init = Guess.linear(prob.time_all_section, obj.u_max, obj.u_max)
408
+
409
+ # Guess.plot(prob.time_all_section, ut1_init, "ut1", "time", "ut1")
410
+
411
+ # if(flag_savefig):plt.savefig(savefig_dir + "guess_ut1" + savefig_add + ".png")
412
+
413
+
414
+
415
+ prob.set_states_all_section(0, r_init)
416
+
417
+ prob.set_states_all_section(1, vr_init)
418
+
419
+ prob.set_states_all_section(2, vt_init)
420
+
421
+ prob.set_controls_all_section(0, ur1_init)
422
+
423
+ prob.set_controls_all_section(2, ut1_init)
424
+
425
+
426
+
427
+
428
+
429
+ # ========================
430
+
431
+ # Main Process
432
+
433
+ # Assign problem to SQP solver
434
+
435
+ prob.dynamics = [dynamics]
436
+
437
+ prob.knot_states_smooth = []
438
+
439
+ prob.cost = cost
440
+
441
+ prob.running_cost = running_cost
442
+
443
+ prob.equality = equality
444
+
445
+ prob.inequality = inequality
446
+
447
+
448
+
449
+
450
+
451
+ def display_func():
452
+
453
+ tf = prob.time_final(-1)
454
+
455
+ print("tf: {0:.5f}".format(tf))
456
+
457
+
458
+
459
+
460
+
461
+ prob.solve(obj, display_func, ftol=1e-12)
462
+
463
+
464
+
465
+ # ========================
466
+
467
+ # Post Process
468
+
469
+ # ------------------------
470
+
471
+ # Convert parameter vector to variable
472
+
473
+ r = prob.states_all_section(0)
474
+
475
+ vr = prob.states_all_section(1)
476
+
477
+ vt = prob.states_all_section(2)
478
+
479
+ ur1 = prob.controls_all_section(0)
480
+
481
+ ur2 = prob.controls_all_section(1)
482
+
483
+ ut1 = prob.controls_all_section(2)
484
+
485
+ ut2 = prob.controls_all_section(3)
486
+
487
+ time = prob.time_update()

2

説明文の変更

2018/11/22 07:44

投稿

YYY9856
YYY9856

スコア13

test CHANGED
@@ -1 +1 @@
1
- pythonを使った最適化計算のエラーについて
1
+ pythonのエラーコードの読み方について
test CHANGED
@@ -8,13 +8,15 @@
8
8
 
9
9
  超越方程式の解を変数で出し(一般解)、他の式の変数に入れる機能を実装中に以下のエラーメッセージが発生しました。
10
10
 
11
- aとeとtは定数です。
12
-
13
11
 
14
12
 
15
13
  ### 発生している問題・エラーメッセージ
16
14
 
17
- エラーの解決かりません。
15
+ エラーメッセージ読み方がかりません。
16
+
17
+ つまりどのファイルのどの行に何が起こっていると言われているのかを
18
+
19
+ 教えていただきたいです。
18
20
 
19
21
 
20
22
 
@@ -22,7 +24,63 @@
22
24
 
23
25
 
24
26
 
27
+ ---- iteration : 1 ----
28
+
29
+ Traceback (most recent call last):
30
+
31
+ File "10_Low_Thrust_Orbit_Transfer16.py", line 259, in <module>
32
+
33
+ prob.solve(obj, display_func, ftol=1e-12)
34
+
35
+ File "C:\Users\AppData\Local\Programs\Python\Python36-32\lib\site-packages\OpenGoddard\optimize.py", line 683, in solve
36
+
37
+ "ftol": ftol})
38
+
39
+ File "C:\Users\AppData\Local\Programs\Python\Python36-32\lib\site-packages\scipy\optimize\_minimize.py", line 611, in minimize
40
+
41
+ constraints, callback=callback, **options)
42
+
43
+ File "C:\Users\AppData\Local\Programs\Python\Python36-32\lib\site-packages\scipy\optimize\slsqp.py", line 313, in _minimize_slsqp
44
+
45
+ for c in cons['eq']]))
46
+
47
+ File "C:\Users\AppData\Local\Programs\Python\Python36-32\lib\site-packages\scipy\optimize\slsqp.py", line 313, in <listcomp>
48
+
49
+ for c in cons['eq']]))
50
+
51
+ File "C:\Users\AppData\Local\Programs\Python\Python36-32\lib\site-packages\OpenGoddard\optimize.py", line 649, in for_solver
52
+
53
+ return func(arg0, arg1)
54
+
55
+ File "C:\Users\AppData\Local\Programs\Python\Python36-32\lib\site-packages\OpenGoddard\optimize.py", line 620, in equality_add
56
+
57
+ dx = self.dynamics[i](self, obj, i)
58
+
25
- ValueError: The array returned by a function changed size between calls
59
+ File "10_Low_Thrust_Orbit_Transfer16.py", line 68, in dynamics
60
+
61
+ s = optimize.fsolve(h,0)
62
+
63
+ File "C:\Users\AppData\Local\Programs\Python\Python36-32\lib\site-packages\scipy\optimize\minpack.py", line 148, in fsolve
64
+
65
+ res = _root_hybr(func, x0, args, jac=fprime, **options)
66
+
67
+ File "C:\Users\AppData\Local\Programs\Python\Python36-32\lib\site-packages\scipy\optimize\minpack.py", line 214, in _root_hybr
68
+
69
+ shape, dtype = _check_func('fsolve', 'func', func, x0, args, n, (n,))
70
+
71
+ File "C:\Users\AppData\Local\Programs\Python\Python36-32\lib\site-packages\scipy\optimize\minpack.py", line 27, in _check_func
72
+
73
+ res = atleast_1d(thefunc(*((x0[:numinputs],) + args)))
74
+
75
+ File "10_Low_Thrust_Orbit_Transfer16.py", line 65, in h
76
+
77
+ return f(E)-g(E)
78
+
79
+ File "10_Low_Thrust_Orbit_Transfer16.py", line 63, in g
80
+
81
+ return np.sqrt(obj.R**3/a**3)* t
82
+
83
+ AttributeError: 'float' object has no attribute 'sqrt'
26
84
 
27
85
 
28
86
 
@@ -31,65 +89,3 @@
31
89
 
32
90
 
33
91
  python
34
-
35
-
36
-
37
- ソースコード
38
-
39
-
40
-
41
- a = prob.states(0, section)
42
-
43
- e = prob.states(4, section)
44
-
45
-
46
-
47
- t = prob.time_update()
48
-
49
-
50
-
51
- def f(E):
52
-
53
- return E - np.sin(E)
54
-
55
- def g(E):
56
-
57
- return t
58
-
59
- def h(E):
60
-
61
- return f(E)-g(E)
62
-
63
-
64
-
65
-
66
-
67
- s = optimize.fsolve(h,0)
68
-
69
-
70
-
71
-
72
-
73
- r = a * (1 -e * np.cos(s))
74
-
75
-
76
-
77
- ### 試したこと
78
-
79
-
80
-
81
- ここに問題に対して試したことを記載してください。
82
-
83
-
84
-
85
- ### 補足情報(FW/ツールのバージョンなど)
86
-
87
-
88
-
89
- ここにより詳細な情報を記載してください。
90
-
91
- 下記のHPのコードを修正しようとしています。
92
-
93
- 質問自体がわかりづらいかもしれませんがどうぞよろしくお願いします。
94
-
95
- https://qiita.com/ina111/items/60f75d90e5376d34a0ef

1

誤記

2018/11/22 06:50

投稿

YYY9856
YYY9856

スコア13

test CHANGED
File without changes
test CHANGED
@@ -20,9 +20,9 @@
20
20
 
21
21
  エラーメッセージ
22
22
 
23
- return np.sqrt(obj.mu / obj.R**3) * t
24
23
 
24
+
25
- TypeError: 'numpy.float64' object cannot be interpreted as an integer
25
+ ValueError: The array returned by a function changed size between calls
26
26
 
27
27
 
28
28