#!/usr/local/bin/perl

use 5.042.0;

# la subsubversion .0 ha regresado a v.5 solo hay que esperar por la .1

# The problem says 'March' but we'll suppose it puts 'any month' one year :-)

#    T  O  T  O   T   O   T   O   T  O  sequence of origin - target
# my @d= (2, 6, 9, 12, 17, 20, 23, 25) x 12; # 12 meses
my @f =   (2, 6, 9, 12, 17, 20, 23, 25);   # 0 to 7; 1 year 4 month, explained later
my @t = qw(l  c  l   c   l   c   l   c);        # tipo de vuelo: ida o vuelta  

# my @s= (0, 1, 0,  1,  0,  1,  0,  1);     # 4 rounds ( 0 = leave or not , 1 = come back or not )
# my @s1=(0, 1, 1,  0,  1,  1,  1,  1);     # also 4 rounds
# my @s2=(0, 1, 0,  0,  1,  1,  1,  1);     # 3 rounds
# my @s3=(1, 1, 1,  1,  1,  1,  1,  1);     # 4 round too   

my $min = 9e99;
my ($cost, $counter, $Z);
my $t0 = time();
for (;;){
#    my @go = (0) x (30*16);    # un year de 360 dias, atajando
    # es probablemente suficiente ya que aunque todos los vuelos fuesen inter-mes, no intra-mes, durarian 16 meses
    # dia de vuelo yes or no, comenzamos con no
    
    # Comment over 'live in Dallas', is that nigths in San Diego could be inter-month or not, but not inter-year, supposing...
    
    $cost = $counter = $Z = 0;
    my $str="";
    my ($i, $j, $lastj) = (0, 0, 0);
    
    while (1){        # cada ronda son 2 viajes
	
#	if ($counter == 0){
	    $i = int (8*rand());        # Son 8 dias de posible vuelo, sea cual sea el mes
#	}else{
#	    $i = int (8*rand());
#           $i = $lastj + 1;            # NO HAY QUE DAR NADA POR SUPUESTO. Cada noche no cuenta, solo cuentan los vuelos
#	    if ($i > 7) { $i = 0; }
#	}
	$j = int (8*rand());
	
	next if ($i == $j || $t[$i] eq $t[$j]);
	next if ($counter == 0 && $t[$i] ne "l"); 
	next if ($counter >= 12 && $t[$j] ne "c");    
	
#	if ($i > $j ) { ($i,$j) = ($j,$i); }    # swap dudoso. Ahora es posible coger el vuelo un dia 23 y volver el 6
        
	$counter++;
	$str .= "$t[$i]$f[$i] => $t[$j]$f[$j]   ";

	if ($i < $j){
	    $Z = ( $f[$i] - $f[$j] );    # numero de noches, no puede ser 0
	}else{
	    $Z = (30 - $f[$i] + $f[$j])   # volver o ir en diferente mes 
	}
	
	if ( $Z <= 7 ) { $cost += 750 * (1-0.25); }        
	elsif ( $Z >= 10 && $Z <20 ) { $cost += 750 * (1-0.35); }
	elsif ($Z >= 20 ) { $cost += 750 * (1-0.45); }
	else { $cost += 750; }
	
	if ($cost <= $min && $t[$j] eq "c" && $counter >= 8){   # no se minimiza en $Zsum numero de noches
	    $min = $cost;
	    my $time = time() - $t0;
	    say "Time = $time secs.   $str   cost = $cost";
	    last;
	}
	
#	$lastj = $j;
    }
}

__END__
From gamo@rocketmail.com Sat Dec 18 15:00:04 2004
Date: 18 Dec 2004 06:00:04 -0800
From: gamo@rocketmail.com
Newsgroups: sci.op-research
Subject: Flight Problem. Can anybody help?

A consultant lives in Dallas but has a client in San Diego. So she has
to travel there often. Here is her flight schedule
Leave Dallas          Leave San Diego
March 2               March 6
March 9               March 12
March 17              March 20
March 23              March 25

The round-trip ticket costs $750 and if the ticket covers 7 nights or
less the airline offers 25% discount. A 35% discount for ticket
covering 10 or more night and 45% discount for ticket covering 20 or
more nights. The consultant needs to purchase 4 round-trip for the
least amount of money.
Can anyone help formulte the LP model for me?

Thanks,
Gamo.

