Piecewise linear function But Decreasing and having negative part

51 views
Skip to first unread message

Syed Taha Munir

unread,
Feb 24, 2015, 10:57:41 AM2/24/15
to am...@googlegroups.com
Dear AMPL users

I am trying to implement a Piecewise linear function in AMPL but almost all the examples that i read in the book are about implementation of increasing piecewise function where as i have decreasing function, My curve also contain a negative portion . Can someone please help me in this regard.
I have attached the file which displays my piecewise curve,
X-axis Yaxis Slope
-3195.2 40819.8618 38294.5175
0 38307.2929 35636.0698
3600 35646.7108 32938.3676
8000 32946.4691 27341.2882
22000 27343.6415

Thanking you all in anticipation. Response is eagerly awaited.

AMPLquestion.jpg

Syed Taha Munir

unread,
Feb 24, 2015, 11:20:52 AM2/24/15
to am...@googlegroups.com
If anyone could kindly share his implemented code of such function that would be a big help for me.
Eagerly waiting for the replies. 
Message has been deleted

Syed Taha Munir

unread,
Feb 24, 2015, 3:28:20 PM2/24/15
to am...@googlegroups.com
I have used transport problem from book to formulate my problem but it is giving error that my slopes are not increasing and my f(0) is not equals zero 
param npiece {Prod,Hours} integer >= 1;

param rate {i in Prod,j in Hours,p in 1..npiece[i,j]} 
  >= if p = 1 then 0 else rate[i,j,p-1];

param limit {i in Prod,j in Hours,p in 1..npiece[i,j]-1} 
  > if p = 1 then 0 else limit[i,j,p-1];
               param npiece: Hour1 Hour2 Hour3 Hour4 Hour5 Hour6 Hour7 Hour8 Hour9 :=
                    LOX 4  4     4   4      4       4        4   4    4
param rate :=
  [LOX,Hour1,*] 1 -38295  2 35636   3 32938  4 27341 [LOX,Hour2,*] 1 -38295  2 35636   3 32938  4 27341
  [LOX,Hour3,*] 1 -38295  2 35636   3 32938  4 27341 [LOX,Hour4,*] 1 -38295  2 35636   3 32938  4 27341
  [LOX,Hour5,*] 1 -38295  2 35636   3 32938  4 27341 [LOX,Hour6,*] 1 -38295  2 35636   3 32938  4 27341
  [LOX,Hour7,*] 1 -38295  2 35636   3 32938  4 27341  [LOX,Hour8,*] 1 -38295  2 35636   3 32938  4 27341
  [LOX,Hour9,*] 1 -38295  2 35636   3 32938  4 27341 ;

param limit :=
  [LOX,*,*]  Hour1 1  0000  Hour1 2 3600  Hour1 3  8000  
        Hour2 1  0000  Hour2 2 3600  Hour2 3  8000  
        Hour3 1  0000  Hour3 2 3600  Hour3 3  8000 
        Hour4 1  0000  Hour4 2 3600  Hour4 3  8000  
        Hour5 1  0000  Hour5 2 3600  Hour5 3  8000  
        Hour6 1  0000  Hour6 2 3600  Hour6 3  8000 
        Hour7 1  0000  Hour7 2 3600  Hour7 3  8000  
        Hour8 1  0000  Hour8 2 3600  Hour8 3  8000  
        Hour9 1  0000  Hour9 2 3600  Hour9 3  8000  ;
 

Robert Fourer

unread,
Feb 25, 2015, 10:32:14 AM2/25/15
to am...@googlegroups.com
I can't see anything wrong based on the information that you have provided. It would help if you could provide (1) the whole AMPL statement in which the piecewise-linear function appears, and (2) the complete text of the error message that you are seeing. It is best to copy the entire error message and paste it into your email, rather than trying to describe the message.

Bob Fourer
am...@googlegroups.com

=======

From: am...@googlegroups.com [mailto:am...@googlegroups.com] On Behalf Of Syed Taha Munir
Sent: Tuesday, February 24, 2015 2:26 PM
To: am...@googlegroups.com
Subject: [AMPL 10072] Re: Piecewise linear function But Decreasing and having negative part

I have used transport problem from book to formulate my problem but it is giving error that my slopes are not increasing and my f(0) is not equals zero

param npiece {Prod,Hours} integer >= 1;

param rate {i in Prod,j in Hours,p in 1..npiece[i,j]}
>= if p = 1 then 0 else rate[i,j,p-1];

param limit {i in Prod,j in Hours,p in 1..npiece[i,j]-1}
> if p = 1 then 0 else limit[i,j,p-1];

Syed Taha Munir

unread,
Feb 25, 2015, 12:38:26 PM2/25/15
to am...@googlegroups.com, 4...@ampl.com
I would like to thank you for your reply BOB. 
In my Last picture i calculated the slope wrong in this picture slopes are right.
I am totally new in AMPL. I had been trying for two days and managed to went a little further but still stuck in this situation.

minimize TC:
sum {i in Prod,j in Hours} <<-3195.2,-1697.45,0,3600,8000,22000;0,-0.8372131,-0.7414846,-0.7390506,-0.6136911,-0.400202,0>>make[i,j];

I used this statement to represent my piece wise curve but as you can see that there is no zero crossing in my curve. How should i add this information in AMPL that my curve has no zero crossing and f(0)=38307.2929. 
I am not receiving any error messages but my objective function is generating false values because ample is giving f(0)=0 where as my function has f(0)=38307.2929.

<<-3195.2,-1697.45,0,3600,8000,22000;0,-0.8372131,-0.7414846,-0.7390506,-0.6136911,-0.400202,0>>0 should generate =38307.2929
but right now it is generating
<<-3195.2,-1697.45,0,3600,8000,22000;0,-0.8372131,-0.7414846,-0.7390506,-0.6136911,-0.400202,0>>0 ==> 0

Thanking you in anticipation.
ampl.png

Robert Fourer

unread,
Feb 25, 2015, 11:12:56 PM2/25/15
to am...@googlegroups.com
I suggest that you write your function like this:

var make {i in Prod, j in Hours} >= -3195.2, <= 22000;

minimize TC:
sum {i in Prod, j in Hours}
(38307.2929 + <<-1697.45,0,3600,8000;
-0.8372131,-0.7414846,-0.7390506,-0.6136911,-0.400202>> make[i,j]);

Since your piecewise-linear function is zero at zero, adding 38307.2929 makes it have the value 38307.2929 at zero.

Bob Fourer
am...@googlegroups.com

=======

From: am...@googlegroups.com [mailto:am...@googlegroups.com] On Behalf Of Syed Taha Munir

Syed Taha Munir

unread,
Feb 26, 2015, 3:22:35 AM2/26/15
to am...@googlegroups.com, 4...@ampl.com
I would like to thank you again for the reply .

I had tried the strategy that you suggested . But when i expand  TC it shows something like that

ampl: expand TC;
minimize TC:
<<-1697.45, 0, 3600, 8000; -0.837213, -0.741485, -0.739051, -0.613691, -0.400202>> make['LOX','Hour1'] 
        + <<-1697.45, 0, 3600, 8000; -0.837213, -0.741485, -0.739051, -0.613691, -0.400202>> make['LOX','Hour2']
        + <<-1697.45, 0, 3600, 8000; -0.837213, -0.741485, -0.739051, -0.613691, -0.400202>> make['LOX','Hour3'] + 114922;

 I need to add 38307.2929 to every term but what i observe it is adding in the end (38307.2929*3)=114922. Because I will have to multiply every term after adding 38307.2929 with the Price.

minimize TC: 
    sum {i in Prod, j in Hours} 
      (38307.2929 + <<-1697.45,0,3600,8000; 
        -0.8372131,-0.7414846,-0.7390506,-0.6136911,-0.400202>> make[i,j]*price[j]); 

So i would like to have expand TC like that :

(<<-1697.45, 0, 3600, 8000; -0.837213, -0.741485, -0.739051, -0.613691, -0.400202>> make['LOX','Hour1']+38307.2929)*Price[Hour1]) + (<<-1697.45, 0, 3600, 8000; -0.837213, -0.741485, -0.739051, -0.613691, -0.400202>> make['LOX','Hour1']+38307.2929)*Price[Hour2])+(<<-1697.45, 0, 3600, 8000; -0.837213, -0.741485, -0.739051, -0.613691, -0.400202>> make['LOX','Hour1']+38307.2929)*Price[Hour3])

Thanking you in anticipation.
Kind regards
Taha

Syed Taha Munir

unread,
Feb 26, 2015, 5:12:28 AM2/26/15
to am...@googlegroups.com, 4...@ampl.com
It would also be very kind of you BOB if you can tell me how can i write my piece-wise function in the form of explicit SOS1 format .
Using suffix sosno integer IN; 
         suffix ref integer IN; 
I know that AMPL will generate it self it into SOS form for solvers but i want to learn how can i write it my self in my situation.

Best regards
Taha
On Thursday, February 26, 2015 at 5:12:56 AM UTC+1, AMPL Optimization wrote:

Robert Fourer

unread,
Feb 27, 2015, 12:50:22 PM2/27/15
to am...@googlegroups.com
You didn't mention Price before, and it is hard to say what you should do now because you do not state whether Price is a parameter or a variable. Also the expansion that you say you would like to have has twice as many right parentheses as left ones, so something must be wrong with it. The best that I can guess is that you want

minimize TC:
sum {i in Prod, j in Hours}
((38307.2929 + <<-1697.45,0,3600,8000;
-0.8372131,-0.7414846,-0.7390506,-0.6136911,-0.400202>> make[i,j]) * price[j]);

which will work fine if price is a parameter but will give you a very difficult nondifferentiable function to optimize if price is a variable.
Reply all
Reply to author
Forward
0 new messages