help-octave
[Top][All Lists]

## Re: Newbie question - sqp solver equality constraints function

 From: Juan Pablo Carbajal Subject: Re: Newbie question - sqp solver equality constraints function Date: Thu, 22 Jun 2017 15:37:06 +0200

```On Thu, Jun 22, 2017 at 12:41 PM, alexegin <address@hidden> wrote:
> Juan Pablo Carbajal-2 wrote
>> ... also try to avoid that I get your code in a single line, makes it hard
>
> Sorry, I format my code using *pre* html tags - I am just trying to make it
> easier to read and it looks fine in a browser. But if it makes it hard to
> read I will not use HTML format.
Yes, use plain text.

> I think I know what is the reason of the error - when I was checking
> intitial guess as you sad I realized that it contains *positive values
> only*. When at least one of the values is *negative* - everything runs
> without any errors. I do not know yet how to avoid it, but *thank you very
> much !!!*
The initial guess is something kind of arbitrary, just use a hand
crafted initial guess. the better the guess the faster you will get

> Can I ask you one more time to help me with the Octave syntax  - as far as
> you can see I am not familiar with it
Note:
1. If you are using objects with real values (i.e. no complex values)
use .' for transpose to avoid calling conj().
2. Think in linear algebra not in loops (you probably come from a
language that is good at loops), try to use operators whenever you
can.

> Here is my full script and I think *moo* function can be implemented in a
> much more easier way - /simplified/, as you said:
>
> clear all;
>
> function r = moo( A, B )
>
>   Tmp = zeros( 1, rows( B ) ); # Temp matrix
Put declarations close to the point where the object is used.
>
>   Bt = B'; # Transpose B
Use .' (unless there are complex values)

>
>   # Fill in temp matrix
>   for i = 1:columns( Bt )
>     for j = 1:rows( A )
>       Tmp( 1, i ) += A( j, i ) * Bt( 1, j );
>     endfor
>   endfor
>
Tmp is just
Tmp = B.' * A;
No need of allocation

>   # Compute result
>   #r = 0;
>   #for i = 1:columns( Tmp )
>   #  r = r + Tmp( 1, i ) * B( i, 1 );
>   #endfor
>
>   r = Tmp * B; # I think it is better, right?
This is correct so your whole function is just
r =@(A,b) b.' * A * b
or even better
h = @(x) f - x.' * A * x

>
> endfunction
>
> Y = dlmread( "y.csv", "\t" );
> Yt = Y';
>
> R = dlmread( "r.csv", "\t" );
>
> Q = dlmread( "q.csv", "\t" );
>
> f = dlmread( "f.csv", "\t" );
>
> Lb = dlmread( "lb.csv", "\t" ); # -1.0
> Ub = dlmread( "ub.csv", "\t" ); # 1.0
>
> phi = @( x ) - Yt * x;
>
> g = @( x ) [ sum( x( x < 0.0 ) ); sum( x( x >=0.0 ) ) ] - [ Lb; Ub ];
>
> h = @( x ) f - moo( Q, x );
>
> tic;
> [x obj info iter nf l] = sqp( R, phi, g, h, Lb, Ub );
> toc;
>
> Thank you very much again - you helped me a lot!
>
Your problem seems quite simple, check using QP (maybe you can render
it in that form) instead of SQP. Anyways SQP should work.

>
>
> --
> View this message in context:
> http://octave.1599824.n4.nabble.com/Newbie-question-sqp-solver-equality-constraints-function-tp4683834p4683861.html
> Sent from the Octave - General mailing list archive at Nabble.com.
>
> _______________________________________________
> Help-octave mailing list