September Morn Update
This commit is contained in:
parent
4e2d22a71d
commit
aac6731f2c
6856 changed files with 141342 additions and 21127 deletions
1
Task/Vogels-approximation-method/00META.yaml
Normal file
1
Task/Vogels-approximation-method/00META.yaml
Normal file
|
|
@ -0,0 +1 @@
|
|||
--- {}
|
||||
|
|
@ -63,7 +63,7 @@ void max_penalty(int len1, int len2, bool is_row, int res[4]) {
|
|||
void next_cell(int res[4]) {
|
||||
int i, res1[4], res2[4];
|
||||
max_penalty(N_ROWS, N_COLS, TRUE, res1);
|
||||
max_penalty(N_COLS, N_COLS, FALSE, res2);
|
||||
max_penalty(N_COLS, N_ROWS, FALSE, res2);
|
||||
|
||||
if (res1[3] == res2[3]) {
|
||||
if (res1[2] < res2[2])
|
||||
|
|
@ -0,0 +1,35 @@
|
|||
#include <stdio.h>
|
||||
#include <limits.h>
|
||||
|
||||
#define TRUE 1
|
||||
#define FALSE 0
|
||||
#define N_ROWS 5
|
||||
#define N_COLS 5
|
||||
|
||||
typedef int bool;
|
||||
|
||||
int supply[N_ROWS] = { 461, 277, 356, 488, 393 };
|
||||
int demand[N_COLS] = { 278, 60, 461, 116, 1060 };
|
||||
|
||||
int costs[N_ROWS][N_COLS] = {
|
||||
{ 46, 74, 9, 28, 99 },
|
||||
{ 12, 75, 6, 36, 48 },
|
||||
{ 35, 199, 4, 5, 71 },
|
||||
{ 61, 81, 44, 88, 9 },
|
||||
{ 85, 60, 14, 25, 79 }
|
||||
};
|
||||
|
||||
// etc
|
||||
|
||||
int main() {
|
||||
// etc
|
||||
|
||||
printf(" A B C D E\n");
|
||||
for (i = 0; i < N_ROWS; ++i) {
|
||||
printf("%c", 'V' + i);
|
||||
for (j = 0; j < N_COLS; ++j) printf(" %3d", results[i][j]);
|
||||
printf("\n");
|
||||
}
|
||||
printf("\nTotal cost = %d\n", total_cost);
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -0,0 +1,46 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math"
|
||||
)
|
||||
|
||||
var supply = []int{461, 277, 356, 488, 393}
|
||||
var demand = []int{278, 60, 461, 116, 1060}
|
||||
|
||||
var costs = make([][]int, nRows)
|
||||
|
||||
var nRows = len(supply)
|
||||
var nCols = len(demand)
|
||||
|
||||
var rowDone = make([]bool, nRows)
|
||||
var colDone = make([]bool, nCols)
|
||||
var results = make([][]int, nRows)
|
||||
|
||||
func init() {
|
||||
costs[0] = []int{46, 74, 9, 28, 99}
|
||||
costs[1] = []int{12, 75, 6, 36, 48}
|
||||
costs[2] = []int{35, 199, 4, 5, 71}
|
||||
costs[3] = []int{61, 81, 44, 88, 9}
|
||||
costs[4] = []int{85, 60, 14, 25, 79}
|
||||
|
||||
for i := 0; i < len(results); i++ {
|
||||
results[i] = make([]int, nCols)
|
||||
}
|
||||
}
|
||||
|
||||
// etc
|
||||
|
||||
func main() {
|
||||
// etc
|
||||
|
||||
fmt.Println(" A B C D E")
|
||||
for i, result := range results {
|
||||
fmt.Printf("%c", 'V'+i)
|
||||
for _, item := range result {
|
||||
fmt.Printf(" %3d", item)
|
||||
}
|
||||
fmt.Println()
|
||||
}
|
||||
fmt.Println("\nTotal cost =", totalCost)
|
||||
}
|
||||
|
|
@ -0,0 +1,61 @@
|
|||
my %costs =
|
||||
:W{:16A, :16B, :13C, :22D, :17E},
|
||||
:X{:14A, :14B, :13C, :19D, :15E},
|
||||
:Y{:19A, :19B, :20C, :23D, :50E},
|
||||
:Z{:50A, :12B, :50C, :15D, :11E};
|
||||
|
||||
my %demand = :30A, :20B, :70C, :30D, :60E;
|
||||
my %supply = :50W, :60X, :50Y, :50Z;
|
||||
|
||||
my @cols = %demand.keys.sort;
|
||||
|
||||
my %res;
|
||||
my %g = (|%supply.keys.map: -> $x { $x => [%costs{$x}.sort(*.value)».key]}),
|
||||
(|%demand.keys.map: -> $x { $x => [%costs.keys.sort({%costs{$_}{$x}})]});
|
||||
|
||||
while (+%g) {
|
||||
my @d = %demand.keys.map: -> $x
|
||||
{[$x, my $z = %costs{%g{$x}[0]}{$x},%g{$x}[1] ?? %costs{%g{$x}[1]}{$x} - $z !! $z]}
|
||||
|
||||
my @s = %supply.keys.map: -> $x
|
||||
{[$x, my $z = %costs{$x}{%g{$x}[0]},%g{$x}[1] ?? %costs{$x}{%g{$x}[1]} - $z !! $z]}
|
||||
|
||||
@d = |@d.grep({ (.[2] == max @d».[2]) }).&min: :by(*.[1]);
|
||||
@s = |@s.grep({ (.[2] == max @s».[2]) }).&min: :by(*.[1]);
|
||||
|
||||
my ($t, $f) = @d[2] == @s[2] ?? (@s[1],@d[1]) !! (@d[2],@s[2]);
|
||||
my ($d, $s) = $t > $f ?? (@d[0],%g{@d[0]}[0]) !! (%g{@s[0]}[0], @s[0]);
|
||||
|
||||
my $v = %supply{$s} min %demand{$d};
|
||||
|
||||
%res{$s}{$d} += $v;
|
||||
%demand{$d} -= $v;
|
||||
|
||||
if (%demand{$d} == 0) {
|
||||
%supply.grep( *.value != 0 )».key.map: -> $v
|
||||
{ %g{$v}.splice((%g{$v}.first: * eq $d, :k),1) };
|
||||
%g{$d}:delete;
|
||||
%demand{$d}:delete;
|
||||
}
|
||||
|
||||
%supply{$s} -= $v;
|
||||
|
||||
if (%supply{$s} == 0) {
|
||||
%demand.grep( *.value != 0 )».key.map: -> $v
|
||||
{ %g{$v}.splice((%g{$v}.first: * eq $s, :k),1) };
|
||||
%g{$s}:delete;
|
||||
%supply{$s}:delete;
|
||||
}
|
||||
}
|
||||
|
||||
say join "\t", flat '', @cols;
|
||||
my $total;
|
||||
for %costs.keys.sort -> $g {
|
||||
print "$g\t";
|
||||
for @cols -> $col {
|
||||
print %res{$g}{$col} // '-', "\t";
|
||||
$total += (%res{$g}{$col} // 0) * %costs{$g}{$col};
|
||||
}
|
||||
print "\n";
|
||||
}
|
||||
say "\nTotal cost: $total";
|
||||
|
|
@ -0,0 +1,68 @@
|
|||
sequence supply = {50,60,50,50},
|
||||
demand = {30,20,70,30,60},
|
||||
costs = {{16,16,13,22,17},
|
||||
{14,14,13,19,15},
|
||||
{19,19,20,23,50},
|
||||
{50,12,50,15,11}}
|
||||
|
||||
sequence row_done = repeat(false,length(supply)),
|
||||
col_done = repeat(false,length(demand))
|
||||
|
||||
function diff(integer j, leng, bool is_row)
|
||||
integer min1 = #3FFFFFFF, min2 = min1, min_p = -1
|
||||
for i=1 to leng do
|
||||
if not iff(is_row?col_done:row_done)[i] then
|
||||
integer c = iff(is_row?costs[j,i]:costs[i,j])
|
||||
if c<min1 then
|
||||
min2 = min1
|
||||
min1 = c
|
||||
min_p = i
|
||||
elsif c<min2 then
|
||||
min2 = c
|
||||
end if
|
||||
end if
|
||||
end for
|
||||
return {min2-min1,min1,min_p,j}
|
||||
end function
|
||||
|
||||
function max_penalty(integer len1, len2, bool is_row)
|
||||
integer pc = -1, pm = -1, mc = -1, md = -#3FFFFFFF
|
||||
for i=1 to len1 do
|
||||
if not iff(is_row?row_done:col_done)[i] then
|
||||
sequence res2 = diff(i, len2, is_row)
|
||||
if res2[1]>md then
|
||||
{md,mc,pc,pm} = res2
|
||||
end if
|
||||
end if
|
||||
end for
|
||||
return {md,mc}&iff(is_row?{pm,pc}:{pc,pm})
|
||||
end function
|
||||
|
||||
integer supply_left = sum(supply),
|
||||
total_cost = 0
|
||||
|
||||
sequence results = repeat(repeat(0,length(demand)),length(supply))
|
||||
|
||||
while supply_left>0 do
|
||||
sequence cell = min(max_penalty(length(supply), length(demand), true),
|
||||
max_penalty(length(demand), length(supply), false))
|
||||
integer {{},{},r,c} = cell,
|
||||
q = min(demand[c], supply[r])
|
||||
demand[c] -= q
|
||||
col_done[c] = (demand[c]==0)
|
||||
supply[r] -= q
|
||||
row_done[r] = (supply[r]==0)
|
||||
results[r, c] = q
|
||||
supply_left -= q
|
||||
total_cost += q * costs[r, c]
|
||||
end while
|
||||
|
||||
printf(1," A B C D E\n")
|
||||
for i=1 to length(supply) do
|
||||
printf(1,"%c ",'Z'-length(supply)+i)
|
||||
for j=1 to length(demand) do
|
||||
printf(1,"%4d",results[i,j])
|
||||
end for
|
||||
printf(1,"\n")
|
||||
end for
|
||||
printf(1,"\nTotal cost = %d\n", total_cost)
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
sequence supply = {461, 277, 356, 488, 393},
|
||||
demand = {278, 60, 461, 116, 1060},
|
||||
costs = {{46, 74, 9, 28, 99},
|
||||
{12, 75, 6, 36, 48},
|
||||
{35, 199, 4, 5, 71},
|
||||
{61, 81, 44, 88, 9},
|
||||
{85, 60, 14, 25, 79}}
|
||||
|
|
@ -9,10 +9,10 @@ var demand = :(A => 30, B => 20, C => 70, D => 30, E => 60)
|
|||
var supply = :(W => 50, X => 60, Y => 50, Z => 50)
|
||||
|
||||
var cols = demand.keys.sort
|
||||
var res = :(); costs.each {|k| res{k} = :() }
|
||||
var g = :(); supply.each {|x| g{x} = costs{x}.keys.sort_by{|g| costs{x}{g} }}
|
||||
|
||||
demand.each {|x| g{x} = costs.keys.sort_by{|g| costs{g}{x} }}
|
||||
var (:res, :g)
|
||||
supply.each {|x| g{x} = costs{x}.keys.sort_by{|g| costs{x}{g} }}
|
||||
demand.each {|x| g{x} = costs .keys.sort_by{|g| costs{g}{x} }}
|
||||
|
||||
while (g) {
|
||||
var d = demand.collect {|x|
|
||||
|
|
@ -23,13 +23,8 @@ while (g) {
|
|||
[x, var z = costs{x}{g{x}[0]}, g{x}[1] ? costs{x}{g{x}[1]}-z : z]
|
||||
}
|
||||
|
||||
with (d.max_by { .[2] }) { |dmax|
|
||||
d.grep! { .[2] == dmax[2] }.min_by! { .[1] }
|
||||
}
|
||||
|
||||
with (s.max_by { .[2] }) { |dmax|
|
||||
s.grep! { .[2] == dmax[2] }.min_by! { .[1] }
|
||||
}
|
||||
d.grep! { .[2] == d.max_by{ .[2] }[2] }.min_by! { .[1] }
|
||||
s.grep! { .[2] == s.max_by{ .[2] }[2] }.min_by! { .[1] }
|
||||
|
||||
var (t,f) = (d[2] == s[2] ? ((s[1], d[1])) : ((d[2], s[2])))
|
||||
(d,s) = (t > f ? ((d[0], g{d[0]}[0])) : ((g{s[0]}[0],s[0])))
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue