If there aren't a very large number of values to sort, then you might write a simple sorting program as an AMPL script. It would depend somewhat on the specifics of your situation, however. Can you give a specific example of the AMPL variables that you have, and the variables or listings you would like to have when the sort is complete?
Bob Fourer
From: am...@googlegroups.com [mailto:am...@googlegroups.com]
On Behalf Of bethanyc
Sent: Thursday, January 24, 2013 9:51 PM
To: am...@googlegroups.com
Subject: [AMPL 6516] any way (or work around) to sort variable values?
I'd like to have a defined variable that is a function of the sorted results of a decision variable. More precisely, I'd like to do a 2-column sort (so, sort based on the 1st column, but keep the values from the 2nd column with their original "partner" from the 1st column). I know there is no built-in function for sorting the values of a variable, but I was wondering if there are any work arounds to accomplish the same thing, ideally where the program could keep running uninterrupted? Would a script of some kind work, and if so, how exactly would that work? Or maybe there's something else I could try instead?
This doesn't appear to be something that's particularly amenable to being done in AMPL. Perhaps a reader of this group can suggest a more appropriate application for working with percentiles, preferable one that can exchange data with AMPL if some optimization is needed.
On Behalf Of bethanyc
Sent: Monday, January 28, 2013 8:06 PM
To: am...@googlegroups.com
Cc: 4...@ampl.com
Subject: Re: [AMPL 6528] any way (or work around) to sort variable values?
I would need to sort 8759 values with 2 columns (sort based on 1 column, but keep original partner with it)....not sure if that's considered large or not. My values are hourly for an entire year (8760 values), but I actually need to sort the difference between each point, which is 8759 values. The second column is additional information that needs to be kept with these time-differenced values.
What I'd really like to do is TWO sorts. First, sort the data based on the 2nd column into deciles, then sort EACH decile group based on the 1st column to find the 99th (or 90th or whatever) percentile for each decile (I could just use a simple rank to find the percentile). The results (one 99th percentile value for each decile "bin") would essentially be a look-up table that is accessed (somehow...) in my main program. For example, if some variable (in my main program) falls within the 7th decile "bin", then a new variable (which would be solved for each hour of the year, so 8760 resulting values) is equal to the 99th percentile of that 7th decile bin.
I should also mention that my entire program must be linear. I was thinking that I could use a piecewise-linear formulation for this look-up table, to keep it linear. Would that work??
Thank you for your help!
I think the uncertainty here is over the nature of the "feedback into the model." Do you want to solve a model that incorporates decile and percentile functions of the variables within its objective and/or constraints? Then you cannot proceed directly, because those functions don't exist within AMPL, and even if they did, they are not the kinds of functions that general-purpose solvers can handle. You would have to consider some kind of reformulation, probably in terms of linear constraints and a combination of continuous and discrete variables, that might be solvable; this seems quite hard in general though possibly it could turn out to be easier for your particular application.
On the other hand, perhaps you only want to solve for the variables, compute the decile and percentile values from the optimal variable values, feed the decile and percentile values back into the model as parameters, and solve again -- maybe repeating this procedure a number of times. Then using some external package to compute the values externally would make sense. Once a result has been returned by the solver, the values of the variables are available for any calculations that you might want to make.
On Behalf Of bethanyc
Sent: Sunday, February 03, 2013 1:06 AM
To: am...@googlegroups.com
Cc: 4...@ampl.com
Subject: Re: [AMPL 6557] any way (or work around) to sort variable values?
Actually, the sorting, binning, and percentile calculations need to be done on variable values, so these need to occur within AMPL.
Two columns of values -- both of which are decision variables -- need to be sorted twice, once based on one column, and another time by the other column. The binning and percentile would just require picking values from pre-known positions within the sorted results. That is, once the values are sorted the first time (based on one column), I was just going to divide the results into 10 equal parts (bins). Then, I'd sort again within each bin (based on the other column) and use a simple rank for the percentile (so take the 0.95*N+0.5 value for the 95th percentile, where N is the number of values in each bin, which I would know ahead of time).
The values that would feedback into the model would be the decile values (bin "boundaries") and some use of the percentile values (probably delta-percentile divided by delta-bin to generate the correct rate value).
Let me know if this is still unclear. I'd be happy to send you more details/code directly. Thank you!!