MatthewPeterKelly / MatthewPeterKelly/OptimTraj

How to add a terminal cost to the objective function

Open
#48 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
MATLAB
Stars
703
Forks
219
PR merge metrics
No merged PRs in 30d

Description

Hi, I am trying to add a terminal cost to the objective function that is only state dependent, I tried doing this:

function objectiveFunctions()
    R = [0, 0, 0, 0, 0];
    Q = [10, 10, 10, 10, 10, 10, 10, 10, 10, 10]
    tar = [...] % Some Value
    F = u1*R(1)*u1 + u2*R(2)*u2 + u3*R(3)*u3 + u4*R(4)*u4 + u5*R(5)*u5;
    F = F + (q1-tar(1))*Q(1)*(q1-tar(1)) + (q2-tar(2))*Q(2)*(q2-tar(2)) + (q3-tar(3))*Q(3)*(q3-tar(3)) + (q4-tar(4))*Q(4)*(q4-tar(4)) + (q5-tar(5))*Q(5)*(q5-tar(5));
    F = F + (dq1-tar(6))*Q(6)*(dq1-tar(6)) + (dq2-tar(7))*Q(7)*(dq2-tar(7)) + (dq3-tar(8))*Q(8)*(dq3-tar(8)) + (dq4-tar(9))*Q(9)*(dq4-tar(9)) + (dq5-tar(10))*Q(10)*(dq5-tar(10));
    [f, ~, fz, fzi, ~]  = computeGradients(F,z,empty);

    matlabFunction(f,fz,fzi,...
        'file','autoGen_obj_quadraticCost2.m',...
        'vars',{'q1','q2','q3','q4','q5','dq1','dq2','dq3','dq4','dq5','u1','u2','u3','u4','u5'});        
end

and in the objective function I gave to the problem I did this

dObj_end = autoGen_obj_quadraticCost2(x(1,end),x(2,end),x(3,end),x(4,end),x(5,end),x(6,end),x(7,end),x(8,end),x(9,end),x(10,end),u(1,end),u(2,end),u(3,end),u(4,end),u(5,end));
dObj_pre = autoGen_obj_quadraticCost(x(1,1:end-1),x(2,1:end-1),x(3,1:end-1),x(4,1:end-1),x(5,1:end-1),x(6,1:end-1),x(7,1:end-1),x(8,1:end-1),x(9,1:end-1),x(10,1:end-1),u(1,1:end-1),u(2,1:end-1),u(3,1:end-1),u(4,1:end-1),u(5,1:end-1));
dObj = [dObj_pre, dObj_end];

Where autoGen_obj_quadraticCost is the similar to autoGen_obj_quadraticCost2 with the only difference being the R matrix is not zeros. I tried this but at the end the control just blows up since I suppose the cost function defined in autoGen_obj_quadraticCost2 also is used to calculate the cost on states other than the last state.

Can anyone help me on this matter?

Thanks in advance!

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

No repository file or test is named. Start by tracing how the supplied objective function and autoGen_obj_quadraticCost2 are consumed at the terminal and earlier states, then inspect the objective-function API for separate terminal-cost handling. Done means a state-only terminal cost is applied only at the final state without causing the control solution to blow up.

Written by the indexing model from the issue text.

Assessment

Tech stack
matlab
Domain
tooling
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.