Advertisement
hpatro

massage.pl

Jul 23rd, 2019
620
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 1.94 KB | None | 0 0
  1. #!/usr/bin/perl
  2.  
  3. use Getopt::Std;
  4.  
  5. # take teh output of either the julia or java CPLEX .lp models and
  6. #   standardize the constraints part
  7.  
  8. my %opts = ();
  9. getopts("hz", \%opts);
  10. my ($zFilter) = ($opts{'z'}) ? 1 : 0;
  11.  
  12. my (@recs);
  13. while (<STDIN>) {
  14.     chomp;
  15.     if (/^\s*c\d+:/ || /^\s*obj:/) { # we need to push on the obj tho' unimportant
  16.     push @recs, $_;
  17.     } elsif (/^\s+[=<+-]/) {
  18.     $recs[-1] .= $_;
  19.     }
  20. }
  21.  
  22. foreach $_ (@recs) {
  23.     next if /obj:/;
  24.     # print "Working on: $_\n";
  25.     s/^\s+//;
  26.     s/:/: /g;           # ensure at least one space
  27.     s/([y])\[\((\d+),\s*(\d+)\)\]/sprintf "%se_%d_%d", $1, $3-1, $2-1/eg; # reorder
  28.     s/([l])\[(\d+)\]/sprintf "%su_%d", $1, $2-1/eg;
  29.     s/([x])\[\((\d+),\s*(\d+)\)\]/sprintf "%suv_%d_%d", $1, $2-1, $3-1/eg;
  30.     s/([z])\[(\d+),\s*(\d+)\]/sprintf "%suv_%d_%d", $1, $2-1, $3-1/eg;
  31.  
  32.     # now that we have the julia output in a similar format to java's
  33.     #  reorganise both
  34.     s/([-+])\s+(\d+)\s+([lxyz])/$1$2$3/g;   # ' 2 x' --> ' 2x'; ' -2 z' --> ' -2z'
  35.     s/\s+-(\d+)\s+([lxyz])/ -$1$2/g;
  36.     s/\s+(\d+)\s+([lxyz])/ $1$2/g;
  37.     s/([+-]) ([lxyz])/$1$2/g;   # - zuv_2_1 --> -zuv_2_1
  38.     s/\b1([lxyz])/$1/g;     # remove a sole 1 coeff
  39.     s/ (\d+)([lxyz])/ +$1$2/g;         #  so that every term has a sign
  40.     s/ ([lxyz])/ +$1/g;        #  so that every term has a sign
  41.     #print "$_ -- translated\n";
  42.  
  43.     my($cid, @fields) = split /\s+/;
  44.     $zFilter && zVarInv(@fields[0..$#fields-2]) && next;
  45.     if ($fields[-1] !~ /\d+/ || $fields[-2] !~ /[<=]=?/) {
  46.     print "***: $rec malformed\n";
  47.     exit;
  48.     }
  49.     $fields[-2] =~ s/^=$/==/;
  50.  
  51.     my(@terms) = sort @fields[0..$#fields-2];
  52.  
  53.     print join(' ', (@terms, @fields[-2..-1])), "\n";
  54. }
  55.  
  56.  
  57. # is there an inverted order z term, e.g. zuv_10_0?
  58. sub zVarInv {
  59.     my (@vars) = @_;
  60.     foreach my $var (@vars) {
  61.     if ($var =~ /zuv_(\d+)_(\d+)/) {
  62.         my ($u, $v) = ($1, $2);
  63.         ($u > $v) && return 1;
  64.     }
  65.     }
  66.     return 0;
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement