(!****************************************************** Mosel Example Problems ====================== file d1wagon.mos ```````````````` Load balancing of train wagons (c) 2002 Dash Associates author: S. Heipcke, Mar. 2002 *******************************************************!) model "D-1 Wagon load balancing" uses "mmxprs" forward function solve_heur:real forward procedure shell_sort(A:array(range) of integer) declarations BOXES = 1..16 ! Set of boxes WAGONS = 1..3 ! Set of wagons WEIGHT: array(BOXES) of integer ! Box weights WMAX: integer ! Weight limit per wagon load: array(BOXES,WAGONS) of mpvar ! 1 if box loaded on wagon, 0 otherwise maxweight: mpvar ! Weight of the heaviest wagon load end-declarations initializations from 'd1wagon.dat' WEIGHT WMAX end-initializations ! Solve the problem heuristically and terminate the program if the ! heuristic solution is good enough if solve_heur<=WMAX then writeln("Heuristic solution fits capacity limits") exit(0) end-if ! Every box into one wagon forall(b in BOXES) sum(w in WAGONS) load(b,w) = 1 ! Limit the weight loaded into every wagon forall(w in WAGONS) sum(b in BOXES) WEIGHT(b)*load(b,w) <= maxweight ! Bounds on maximum weight maxweight <= WMAX maxweight >= ceil((sum(b in BOXES) WEIGHT(b))/3) forall(b in BOXES,w in WAGONS) load(b,w) is_binary ! Alternative to lower bound on maxweight: adapt the optimizer cutoff value ! setparam("XPRS_MIPADDCUTOFF",-0.99999) ! Uncomment the following line to see the optimizer log ! setparam("XPRS_VERBOSE",true) ! Minimize the heaviest load minimize(maxweight) ! Solution printing writeln("Optimal solution:\n Max weight: ", getobjval) forall(w in WAGONS) do write(" ", w, ":") forall(b in BOXES) write(if(getsol(load(b,w))=1, " "+b, "")) writeln(" (total weight: ", getsol(sum(b in BOXES) WEIGHT(b)*load(b,w)), ")") end-do !----------------------------------------------------------------- (! LPT (Longest processing time) heuristic: One at a time place the heaviest unassigned box onto the wagon with the least load !) function solve_heur:real declarations ORDERW: array(BOXES) of integer ! Box weights in decreasing order Load: array(WAGONS,range) of integer ! Boxes loaded onto the wagons CurWeight: array(WAGONS) of integer ! Current weight of wagon loads CurNum: array(WAGONS) of integer ! Current number of boxes per wagon end-declarations ! Copy the box weights into array ORDERW and sort it in decreasing order forall(b in BOXES) ORDERW(b):=WEIGHT(b) shell_sort(ORDERW) ! Distribute the loads to the wagons using the LPT heuristic forall(b in BOXES) do v:=1 ! Find wagon with the smallest load forall(w in WAGONS) v:=if(CurWeight(v)N) repeat ! Loop over the partial sorts inc:=inc div 3 forall(i in inc+1..N) do ! Outer loop of straight insertion v:=A(i) j:=i while (A(j-inc)