JuliaMPC / NLOptControl.jl

nonlinear control optimization tool
Other
109 stars 26 forks source link

How to use the "saveData(n)" function #40

Open VladimirKolenov opened 10 months ago

VladimirKolenov commented 10 months ago

This is my code. The first task from the tutorial "Brachistochrona". using NLOptControl using DataFrames, IndexedTables using StatsPlots using CSV X0=[0.0,0.0,0.0] XF=[2.,-2.,NaN] n=define(numStates=3,numControls=1,X0=X0,XF=XF,XL=[0.0,-2.,NaN],XU=[2.,0.,NaN]) states!(n,[:x,:y,:v],descriptions=["x(t)","y(t)","v(t)"]); controls!(n,[:u],descriptions=["u(t)"]); typeof(n.r.ocp.x) typeof(n.r.ocp.u) dx=Array{Expr}(undef,3); dx[1] = :(v[j]sin(u[j])) dx[2] = :(-v[j]cos(u[j])) dx[3] = :(9.81cos(u[j])) dynamics!(n,dx) typeof(n.r.ocp.x) typeof(n.r.ocp.u) XX = 0.5 XY = -1.5 x_p = :(x[j]) y_p = :(y[j]) constraints!(n, [:((($x_p - $XX)^2 + ($y_p - $XY)^2) - 0.55 >= 0)]) configure!(n;(:Nck=>[100]),(:finalTimeDV=>true)); @NLobjective(n.ocp.mdl,Min,n.ocp.tf) n.s.ocp.evalCostates = true optimize!(n); using Plots allPlots(n) st=statePlot(n,1,1,2) df = n.r.ocp.dfs #записали путь до дата фрейма в переменную df df[1][:c] = @. (df[1][:x] ^2 + df[1][:y] ^2) #записали новый столбец "c" в наш дата фрейм @show df df_table = convert(Matrix, df[1][:1:6]) #перевели наш дата фрейм в массив и положили в новую переменную "df_table" df2 = convert(DataFrame, df_table) #перевели наш массив в дата фрейм, чтоб получить новую переменную с дата фреймом(будем получать доступ к дата фрейму из одной переменной, не используя путь n.r.ocp.dfs) @show df2 b = @df df2 plot(:x1, [:x6], color = :red, size = (2500,2000), linewidth = 4, thickness_scaling = 4) #отрисовали наш график, используя настройки: цвет, размер графика, размер линии, размер надписей на графике savefig("D:\Users\kolenov_va\Desktop\julia\Брахистохрона\5\results\c.png") #сохранили в нужную нам папку график в формате png df[1][:d] = @. ((9.81 df[1][:y]) + (df[1][:v] ^2 / 2)) @show df df[1][:c] = @. (((df[1][:x] - XX)^2 + (df[1][:y] - XY)^2) - 0.55) @show df m=JuMP.internalmodel(n.ocp.mdl); c=MathProgBase.getconstrduals(m)

I want to use saveData(n) to save state, control, and costate (if applicable) data (about the collocation points and the Lagrange polynomial that runs through them). And also save the variables in which my Data Frames are written.