# Newbie question - sqp solver equality constraints function

14 messages
Open this post in threaded view
|

## Newbie question - sqp solver equality constraints function

 Hello all! I am trying to use sqp solver to find optimal vector X. All of the vector elements must meet the following constraints: ```# 1. All of the elements must be more than -1.0 and less than 1.0 -1.0 < X(i) < 1.0 # 2.1 Sum of all elements those are less than zero must be equal to -1.0 # 2.2 Sum of all the elements those are more than zero must be equal to 1.0 dMinuses = 0.0; dPluses = 0.0; for i = 1:rows( X ) if ( X( i ) < 0.0 ) dMinuses += X( i ); else dPluses += X( i ); endif; endfor; bResult = ( ( dMinuses + 1.0 ) == 0.0 ) && ( ( dPluses - 1.0 ) == 0.0 ); ``` The first constraint `-1.0 < X(i) < 1.0` can be easily passed to sqp solver by upper and lower bounds. But I do not know how to pass the second ones simultaneously: ```SUM( X( i ) ) - 1.0 = 0.0, X( i ) >= 0.0 SUM( X( i ) ) + 1.0 = 0.0, X( i ) < 0.0 ``` Thank you very much in advance.
Open this post in threaded view
|

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

 On Wed, Jun 21, 2017 at 6:40 PM, alexegin <[hidden email]> wrote: > Hello all! > I am trying to use *sqp* solver to find optimal vector *X*. All of the > vector elements /must/ meet the following constraints: > # 1. All of the elements must be more than -1.0 and less than 1.0-1.0 < X(i) > < 1.0# 2.1 Sum of all elements those are _less than zero_ must be equal to > -1.0# 2.2 Sum of all the elements those are _more than zero_ must be equal > to 1.0dMinuses = 0.0;dPluses   = 0.0;for i = 1:rows( X )  if ( X( i ) < 0.0 > )    dMinuses += X( i );  else    dPluses += X( i );  endif;endfor;bResult = > ( ( dMinuses + 1.0 ) == 0.0 ) && ( ( dPluses - 1.0 ) == 0.0 ); > The first constraint -1.0 < X(i) < 1.0 can be easily passed to *sqp* solver > by upper and lower bounds.But I do not know how to pass the second ones > *simultaneously*: > SUM( X( i ) ) - 1.0 = 0.0, X( i ) >= 0.0SUM( X( i ) ) + 1.0 = 0.0, X( i ) < > 0.0 > Thank you very much in advance. > Hi, As you said the range of X should be passed using UB, LB arguments. The others you can pass using the equality constraints function E = eq_cons (X)  E = [sum(X(X<0)); sum(X(X>0))] - [-1; 1]; endfunction Note that this function is problematic because of the jump introduced when an element changes signs, but give it a try. I am not sure but this alternative might be more suited for sqp (but problably slower due to the copy of X) function E = eq_cons (X)  Y = X;  Y(Y<0) *= -1;  E = sum([X Y]).' - [0; 2]; endfunction I get the same results for dim(x) = 10,..,100 with little difference sin perfomrnace, but maybe your cost is more complex than my test: g =@(X)  [sum(X(X<0)); sum(X(X>0))] - [-1; 1]; c =@(x) sumsq(x); x0 = 2*rand(100,1)-1; tic; [x obj info iter nf l] = sqp(x0,c,g,[],-1,1); toc _______________________________________________ Help-octave mailing list [hidden email] https://lists.gnu.org/mailman/listinfo/help-octave
Open this post in threaded view
|

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

 This post was updated on . Hi, Juan Pablo Carbajal-2! Thank you very much for answering me. Your test is working perfectly: ```clear all; g =@( x ) [ sum( x( x < 0.0 ) ); sum( x( x >= 0.0 ) ) ] - [ -1.0; 1.0 ]; c =@( x ) sumsq( x ); x0 = 2.0 * rand( 10, 1 ) - 1.0; tic; [ x obj info iter nf l ] = sqp( x0, c, g, [], -1.0, 1.0 ); toc; ``` But when I am trying to use constraint function in my script I get the following error: ```error: qp: equality constraint matrix must be full row rank ``` Here is my script: ```clear all; global R = dlmread( "r.csv", "\t" ); # double [ 10x1 ] global Q = dlmread( "q.csv", "\t" ); # double [ 10x10 ] global Y = dlmread( "y.csv", "\t" ); # double [ 10x1 ] global lb = dlmread( "lb.csv", "\t" ); # double [ 10x1 ] global ub = dlmread( "ub.csv", "\t" ); # double [ 10x1 ] g = @( x ) [ sum( x( x < 0.0 ) ); sum( x( x >= 0.0 ) ) ] - [ -1.0; 1.0 ]; function z = foo( m, n ) z = 0; for i = 1:rows( m ) z = z + m( i, 1 ) * n( i, 1 ); endfor; endfunction; function y = phi( x ) global Y; y = foo( Y, x ) * -1; endfunction tic; [ x obj info iter nf l ] = sqp( R, @phi, g, [], lb, ub ); toc; ``` What am I doing wrong? Please do not tell me everything
Open this post in threaded view
|

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

Open this post in threaded view
|

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

 Hi, "Juan Pablo Carbajal-2"
Open this post in threaded view
|

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

Open this post in threaded view
|

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

 Hi, Juan Pablo Carbajal-2 Juan Pablo Carbajal-2 wrote ... or even better h = @(x) f - x.' * A * x I tried to implement non-equality constraint function that way and the result is incredible - it is a hundred times faster than the one with the loop! One hundred, man! It took me so much time to answer because it was very hard to believe and I was checking the results. Thank you very, very much!!! Juan Pablo Carbajal-2 wrote Your problem seems quite simple, check using QP (maybe you can render it in that form) instead of SQP. Anyways SQP should work. Maybe I would, but now it seems to me that sqp is doing quite good - I mean the speed :) Thank you very much!
Open this post in threaded view
|

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

 > Thank you very, very much!!! I am glad to help! If you ever had the chance, drop a donation to Octave (https://www.gnu.org/software/octave/doc/interpreter/How-You-Can-Contribute-to-Octave.htmlor directly to John W. Eaton http://jweaton.org/?page_id=48), are the developers who give us this excellent piece of software. Good luck and come back soon! _______________________________________________ Help-octave mailing list [hidden email] https://lists.gnu.org/mailman/listinfo/help-octave
Open this post in threaded view
|

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

 This post was updated on . Juan Pablo Carbajal-2 wrote Good luck and come back soon! Hello again :) I have the following code working: clear all; Y = 2.0 * rand( 5, 1 ) - 1.0; f = 0.0025; Lb = -1.0; # Lower bound Ub = 1.0; # Upper bound # Initial guess R = ones( rows( Y ), 1 ); R( 2:2:end ) = R( 2:2:end ) * ( 2.0 / rows( Y ) ) * Ub; R( 1:2:end ) = R( 1:2:end ) * ( 2.0 / rows( Y ) ) * Lb; # Objective function phi = @( x ) sumsq( x ); # Equality constraint function g = @( x ) [ sum( x( x <= 0.0 ) ); sum( x( x >= 0.0 ) ) ] - [ Lb; Ub ]; # Non-equality constraint function h = @( x ) f - x.' * Y; # Solver [ x, obj, info, iter, nf, l ] = sqp( R, phi, g, h, Lb, Ub ); But if I set lower bound to zero or above ( Lb = 0.0 or Lb = 0.5 ) I have the following error: error: qp: equality constraint matrix must be full row rankWhat am I doing wrong? I am stuck ...
Open this post in threaded view
|

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

 > # Equality constraint function > g = @( x ) [ sum( x( x <= 0.0 ) ); sum( x( x >= 0.0 ) ) ] - [ Lb; Ub ]; > # Solver > [ x, obj, info, iter, nf, l ] = sqp( R, phi, g, h, Lb, Ub ); > > But if I set lower bound to zero ( *Lb = 0.0* ) I have the following error: > > *error: qp: equality constraint matrix must be full row rank* > > What am I doing wrong? I am stuck ... My guess is that, one of the components of your Equality constraint is empty and hence its derivative is zero, giving zero eigenvalue in the matrix assembled for QP. Nevertheless the function g is not meaningful when Lb or Ub are 0. You are saying that the sum of non-positive (non-negative) components should be zero, if the components are not zero, this cannot be fulfilled (sum of nonzero numbers of equal sign cannot be zero), hence the only solution is all components to be zero. Do you set the initial guess with this property? What are you trying to achieve? _______________________________________________ Help-octave mailing list [hidden email] https://lists.gnu.org/mailman/listinfo/help-octave
Open this post in threaded view
|

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

 Juan Pablo Carbajal-2 wrote Nevertheless the function g is not meaningful when Lb or Ub are 0  ... Do you set the initial guess with this property? What are you trying to achieve? You are right - when Lb ( Ub ) is zero, equality constraint function becomes just: g = @( x ) sum( x ) - Ub; I am trying to achieve a kind of universal solution - I run Octave script from another process, prepearing data for it (see dlmread above). I would like to have an opportunity to set Lb to -1.0 or to 0.0 while runnig the same script, somewhat: #  I am not sure the following syntax is possible if ( Lb < 0.0 )     g = @( x ) [ sum( x( x <= 0.0 ) ); sum( x( x >= 0.0 ) ) ] - [ Lb; Ub ]; else     g = @( x ) sum( x ) - Ub; endif As for initial guess, I set it this way (assume that Lb = -1.0): R = ones( rows( Y ), 1 ); R( 2:2:end ) = R( 2:2:end ) * ( 2.0 / rows( Y ) ) * Ub; # sum( R( R >= 0.0 ) ) = 1.0 R( 1:2:end ) = R( 1:2:end ) * ( 2.0 / rows( Y ) ) * Lb; # sum( R( R <= 0.0 ) ) = -1.0
Open this post in threaded view
|

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

 > if ( Lb < 0.0 ) >     g = @( x ) [ sum( x( x <= 0.0 ) ); sum( x( x >= 0.0 ) ) ] - [ Lb; Ub ]; > else >     g = @( x ) sum( x ) - Ub; > endif > Yeah, that's fine. Make sure your initial guess is also compatible with Lb and Ub. _______________________________________________ Help-octave mailing list [hidden email] https://lists.gnu.org/mailman/listinfo/help-octave
Open this post in threaded view
|

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

 Juan Pablo Carbajal-2 wrote ... Yeah, that's fine. Make sure your initial guess is also compatible with Lb and Ub. It is amazing, but  - yes, it is fine! After answering you I put it that way and - it works! Now I have the following code with the initial guess setting: R = ones( rows( Y ), 1 ); if ( Lb < 0.0 )   R( 2:2:end ) = R( 2:2:end ) * ( ( Ub - Lb ) / rows( Y ) ) * Ub;   R( 1:2:end ) = R( 1:2:end ) * ( ( Ub - Lb ) / rows( Y ) ) * Lb;   R( 2, 1 ) += 1.0 - sum( R( R >= 0.0 ) );   R( 1, 1 ) -= sum( R( R <= 0.0 ) ) + 1.0; else   R = R * ( Ub + Lb ) / rows( Y ); endif if ( Lb < 0.0 )   g = @( x ) [ sum( x( x <= 0.0 ) ); sum( x( x >= 0.0 ) ) ] - [ Lb; Ub ]; else   g = @( x ) sum( x ) - Ub; endif Everithing works, but ... one more thing - although initial guess is met equality constraints exactly, i.e. sum( R( R <= 0.0 ) ) = -1.0 and sum( R( R >= 0.0 ) ) = 1.0, the result of sqp solver is not - sum( x( x <= 0.0 ) ) is a bit less than -1.0 and sum( x( x >= 0.0 ) ) is a bit more than 1.0. Do you have any ideas why it is so - maybe tune the tolerance of sqp?
Open this post in threaded view
|

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

 On Thu, Jun 29, 2017 at 2:12 PM, alexegin <[hidden email]> wrote: > Do you have any ideas why it is so - maybe tune the tolerance of sqp? Yes, this is the tolerances, but I d not think you can change them independently, there is a global tolerance parameter. The output of sqp will give you information on how well the constraints are respected, check the help. Also, always try your ideas before asking. Do not abuse our time! :D _______________________________________________ Help-octave mailing list [hidden email] https://lists.gnu.org/mailman/listinfo/help-octave