Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/perl
- use Getopt::Std;
- # take teh output of either the julia or java CPLEX .lp models and
- # standardize the constraints part
- my %opts = ();
- getopts("hz", \%opts);
- my ($zFilter) = ($opts{'z'}) ? 1 : 0;
- my (@recs);
- while (<STDIN>) {
- chomp;
- if (/^\s*c\d+:/ || /^\s*obj:/) { # we need to push on the obj tho' unimportant
- push @recs, $_;
- } elsif (/^\s+[=<+-]/) {
- $recs[-1] .= $_;
- }
- }
- foreach $_ (@recs) {
- next if /obj:/;
- # print "Working on: $_\n";
- s/^\s+//;
- s/:/: /g; # ensure at least one space
- s/([y])\[\((\d+),\s*(\d+)\)\]/sprintf "%se_%d_%d", $1, $3-1, $2-1/eg; # reorder
- s/([l])\[(\d+)\]/sprintf "%su_%d", $1, $2-1/eg;
- s/([x])\[\((\d+),\s*(\d+)\)\]/sprintf "%suv_%d_%d", $1, $2-1, $3-1/eg;
- s/([z])\[(\d+),\s*(\d+)\]/sprintf "%suv_%d_%d", $1, $2-1, $3-1/eg;
- # now that we have the julia output in a similar format to java's
- # reorganise both
- s/([-+])\s+(\d+)\s+([lxyz])/$1$2$3/g; # ' 2 x' --> ' 2x'; ' -2 z' --> ' -2z'
- s/\s+-(\d+)\s+([lxyz])/ -$1$2/g;
- s/\s+(\d+)\s+([lxyz])/ $1$2/g;
- s/([+-]) ([lxyz])/$1$2/g; # - zuv_2_1 --> -zuv_2_1
- s/\b1([lxyz])/$1/g; # remove a sole 1 coeff
- s/ (\d+)([lxyz])/ +$1$2/g; # so that every term has a sign
- s/ ([lxyz])/ +$1/g; # so that every term has a sign
- #print "$_ -- translated\n";
- my($cid, @fields) = split /\s+/;
- $zFilter && zVarInv(@fields[0..$#fields-2]) && next;
- if ($fields[-1] !~ /\d+/ || $fields[-2] !~ /[<=]=?/) {
- print "***: $rec malformed\n";
- exit;
- }
- $fields[-2] =~ s/^=$/==/;
- my(@terms) = sort @fields[0..$#fields-2];
- print join(' ', (@terms, @fields[-2..-1])), "\n";
- }
- # is there an inverted order z term, e.g. zuv_10_0?
- sub zVarInv {
- my (@vars) = @_;
- foreach my $var (@vars) {
- if ($var =~ /zuv_(\d+)_(\d+)/) {
- my ($u, $v) = ($1, $2);
- ($u > $v) && return 1;
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement