-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathrd_prob_da.f
More file actions
1510 lines (1358 loc) · 51.3 KB
/
rd_prob_da.f
File metadata and controls
1510 lines (1358 loc) · 51.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
subroutine rd_prob_da(n, m, sn, user, iuser, sz_user, sz_iuser,
& vr_reg, bdscl, oldbnd, ws, nws, lws, nlws, bns, n_bns,
& max_cs_terms, SPD, DAT, ifail, NTGRESULT,NRESULTS)
c
c rd_prob_da needs ws[ 2+3*nu + max{2*nu, 2*(rm+mi), 2*nrst} ]
c lws[ mi+max(de, rm)+
c max{2*nrst+rm, nurat_p, nurat_m} ]
c
c PARAMETERS:
c bdscl: array of lower/upper bounds and scale factors for all
c variables and constraints:
c 1 - n+m : lb (first vars then cons)
c n+m+1 - 2*(n+m): lu (first vars then cons)
c 2*(n+m)+1 - 3*(n+m): scale (first vars then cons)
c
c RETURN VALUES:
c ifail = 0: read ok
c = 1: out of memory in reg_cs
c = 2: infeasible in presolve (tgt_bnd_cs)
c = 3: exceeded hardwired limit on number of c/s terms
c = 99: fatal: bail out with error message and
c global_ifail set here
c
c
c
c
c ------------------------------------------------------------------
c NEGATIVE WEIGHTS:
c
c Raw materials into bins and raw mats into products (straights)
c are allowed to have negative weights
c
c All negative weights below -limnwt (-2.d0) are rejected
cHM? All negative weights below -limnwt (-9.d0) are rejected
c Bins cannot have negative costs
c
c ------------------------------------------------------------------
c
c The model uses the following variables:
c TYPE_M (nu,mi+de) nutrient content of bins/demands
c TYPE_MU(rm+mi, mi+de) weight of rm/bin in bin/demand
c TYPE_U (rm+mi) total flow through rm/bin
c (sparse, only when needed)
c TYPE_NU(rm, mi+de) total weight of rm in mi/de
c (sparse: only when needed, i.e mi/de
c is in source path of de and mi is not
c first order bin (no bin sources))
c TYPE_C(mi+de) price of bin/demand
c
c and the following constraints:
c CTYPE_M(nu,mi+de):
c m_ij = sum_{k \in sources} mu_jk*m_ik
c CTYPE_C(mi+de):
c c_j = sum_{k \in sources} mu_jk*c_k
c CTYPE_SUM(mi+de):
c sum_{k \in sources} mu_jk = 1 \forall j
c CTYPE_NURAT(l, k):
c nutrient ratio constraint #l on mi/de k
c l is couted separately for NURATM/NURATP
c CTYPE_U(rm+mi):
c define U: sparse only generated when needed
c CTYPE_NU
c define NU: sparse only generated when needed
c
c objective is (CTYPE_OBJ):
c sum_{k \in demands} tons_k*C_k
cFIXME: check sizes of user, iuser array
use interface_mod
use types_module
use nonlin_types
implicit none
INCLUDE 'SLPCOMM.INC'
include 'pusr.inc'
include 'msg.inc'
chm
include 'return.inc'
include 'ntgres.inc'
type(special_da):: SPD
type(integra_da):: DAT
c type(nl_info):: NLD
integer n, m, sn, sz_user, sz_iuser, n_bns, nws, nlws, ifail,
& max_cs_terms, cntlin, cntnl
double precision scl
integer iuser(sz_iuser), lws(nlws), vr_reg(4*n+2*sn+2), bns(n_bns)
double precision bdscl(3*n+3*m+3), user(sz_user), ws(nws)
double precision oldbnd(2*n+2*m)
integer NRESULTS
double precision NTGRESULT(*)
integer trim77
c
chc New parameters
c
integer NDX,RESULTINDEX
integer RATIOCOUNT
integer i, j, k, l, nznu, i0, i1, i2, i3, nrst, pcsi, ix, os
integer srb, srbb, srbr, p1, fifo1, fifo2, tgb, tgbb, tgbd
integer p_scl, p_maxnu, p_da, p_minnu, p_rmi_lb, p_rmi_ub,
& p_rmi_fg, p_lda
double precision tol, com_tol, totde, avde
double precision dummy, maxv, minv, limnwt, lb, ub
double precision dp1, dp2, dp1l, dp1u, dp2l, dp2u
logical file_exists
logical edge_exists
logical fi_xst, adv_it, lp_sol, found
character ch
character(180) line
integer err
double precision infty, eps
common /NLP_eps_inf/ infty, eps
c to build sparse constraint structure array
integer lt_ix1(3,max_cs_terms), nt_ix1(3,max_cs_terms),
& nt_ix2(3,max_cs_terms)
double precision lt_co(max_cs_terms), nt_co(max_cs_terms)
integer fifo(2*MAX_RMS)
c automatic arrays to carry temporary information
c ... bounds on rm availability (set by rmlmts, used later)
double precision rmava_lb(rm+mi), rmava_ub(rm+mi)
c dChange
c boundMax is maximum amounts of nutrient,mass,cost
double precision boundMax(0:nu+2)
c end dChange
ifail = 0
c ... zero static variables for reg_cs/reg_vr
REG_FST_CL = 0
REG_NX_CS = 0
REG_NX_VR = 0
c ...pointer into user
pu_r = rm
pu_d0 = pu_r + nu*rm
pu_cs = pu_d0 + de
c ... pointer into iuser
pi_cs = 0
pi_cs_pi = pi_cs + 0
pi_cs_pu = pi_cs_pi + m + 1
pi_cs_da = pi_cs_pu + m + 1
cc ... map of sources for each bin/demand
c bn_n_srb = 0
c bn_n_srbb = bn_n_srb + mi+de
c bn_p_srb = bn_n_srbb + mi+de
c bn_l_srb = bn_p_srb + mi+de
cc ... map of targets for each rm/bin
c bn_n_tgb = bn_l_srb + n_mu
c bn_n_tgbb = bn_n_tgb + rm+mi
c bn_p_tgb = bn_n_tgbb + rm+mi
c bn_l_tgb = bn_p_tgb + rm+mi
cc ... list of first order bins
c bn_fob = bn_l_tgb + n_mu
c +mi+de
c ... pointers into workspace ws
c ...avarage value of nutrient & average price
p_scl = 0
c ... maximal value of nutrient
p_maxnu = p_scl + nu+2
c ... min of values of nutrients
p_minnu = p_maxnu + nu
c ... lb/ub for rmava/rminc constraints
p_rmi_lb = p_minnu + nu
p_rmi_ub = p_rmi_lb + mi + max(de,rm)
c ... data vector to read in values from file
p_da = p_rmi_ub + mi + max(de,rm)
c + max(2*nu, rm, 2*mi)
c pointers into workspace lws
p_rmi_fg = 0
p_lda = p_rmi_fg + mi + max(de,rm)
c n_vr = 0
c n_cs = 0
n_maxa = 0
limnwt = 2.d0 ! Stick to this 22/7-05
chm limnwt = 9.d0 ! Causes high instability and float point error
chm limnwt = 4.d0 ! Does work on the SDF 3603 but not preferred.
c limnwt sets the maximum acceptable negative weight
c used to calculate bounds on lam, mu, m, etc
lp_sol = .false.
c ================ subroutine body =====================
c --------------- read raw material prices: PRM.DAT ---------
c user[1:rm] holds raw material prices.
c ws[p_scl+nu+1] holds average price.
ws(p_scl+nu+1) = 0.
if (msg_log) then
WRITE(nout,*)' reading raw material prices: ',
& PRM_FILE(1:trim77(PRM_FILE))
end if
open(10,file=PRM_FILE)
do i=1,rm
c DAT%is_piecewise_price(i) = .false.
read(10,'(a)') line
c print *, i, line
read(line,*, iostat=err) DAT%price1(i), DAT%price2(i)
c print *, i, err, DAT%price1(i), DAT%price2(i)
c .. this here works: if there is only one record: err = -1
c if there are two records then err = 0
if (err.eq.0.and.DAT%price2(i).lt.500000) then
c if (err.eq.0) then
if (DAT%is_piecewise_price(i) .neqv. .true.) then
print *,'previously identified as needing PWL'
stop
end if
else
if (DAT%is_piecewise_price(i) .neqv. .false.) then
print *,'previously identified as not needing PWL'
stop
end if
end if
user(i) = DAT%price1(i)
ws(p_scl+nu+1) = ws(p_scl+nu+1) + user(i)
ws(p_scl+nu+1) = ws(p_scl+nu+1) + user(i)
end do
close(10)
ws(p_scl+nu+1) = ws(p_scl+nu+1)/rm
if (abs(ws(p_scl+nu+1)).le.1d-8) ws(p_scl+nu+1) = 1.d0
c - - - - - register cost variables for PWL raw mat - - - - - -
do i=1,rm
if (DAT%is_piecewise_price(i)) then
lb = 0.d0
ub = 1.d10
call reg_vr(n, m, sn, vr_reg, bdscl, TYPE_C, -i, 0, lb, ub,
& ws(p_scl+nu+1))
end if
end do
c ------------- read raw material specs: RAW_MAT.DAT ----------
c user[pu_r+1:pu_r+rm*nu] holds raw material definition
c ws[p_da+1:p_da+rm] used as temporary array
if (msg_log) then
WRITE(nout,*)
& ' reading nutrient composition of raw materials: ',
& RAWMAT_FILE(1:trim77(RAWMAT_FILE))
end if
open(10,file=RAWMAT_FILE)
do l=1,nu
read(10,*) (ws(p_da+i), i=1,rm)
c WRITE(nout,*) (ws(p_da+i), i=1,rm)
do i=1,rm
user(pu_r+(i-1)*nu+l) = ws(p_da+i)
end do
end do
close(10)
c ... calculate maxnu & minnu
do l=1,nu
ws(p_maxnu+l) = 0.
ws(p_minnu+l) = 1.d10
do i=1,rm
ws(p_maxnu+l) = max(ws(p_maxnu+l),user(pu_r+(i-1)*nu+l))
ws(p_minnu+l) = min(ws(p_minnu+l),user(pu_r+(i-1)*nu+l))
end do
end do
c -------------- read tonnages of demand: TONS.DAT ---------------
open(10,file=TONS_FILE)
if (msg_log) then
WRITE(nout,*) ' reading tonnages for demand: ',
& TONS_FILE(1:trim77(TONS_FILE))
end if
totde = 0.d0
do j=1,de
read(10,*) user(pu_d0+j)
totde = totde + user(pu_d0+j)
c WRITE(nout,*) user(pu_d0+j)
end do
close(10)
avde = totde/de
c ----------------------- calculate scales --------------
c
c scl(i), 1<=i<=nu is average level of nu i in rm's
c scl(nu+1) is average price for rm's
c scl(nu+2) is average demand for prod's
ws(p_scl+nu+2) = avde
do l=1,nu
nznu = 0
ws(p_scl+l) = 0.
do i=1,rm
if (user(pu_r+(i-1)*nu+l).gt.1.e-10) then
nznu = nznu + 1
ws(p_scl+l) = ws(p_scl+l) + user(pu_r+(i-1)*nu+l)
end if
end do
if (nznu.eq.0.or.(abs(ws(p_scl+l)).le.1d-8)) then
ws(p_scl+l) = 1.d0
else
ws(p_scl+l) = ws(p_scl+l)/nznu
end if
end do
c WRITE(nout,*) ' Print scales:'
c do i=1,nu+1
c WRITE(nout,'(I3,F15.5)') i,ws(p_scl+i)
c end do
c -------- read bounds on nutrients in bins: BRIAN.DAT ---------
c
c Every premix is described by two lines of BRIAN.DAT:
c first gives lower bounds for all nutrients
c second gives upper bounds for all nutrients
inquire(file=BRIAN_FILE,exist=file_exists)
if (file_exists) then
if (msg_log) then
WRITE(nout,*)
& ' reading bounds on nut. content of premixes: ',
& BRIAN_FILE(1:trim77(BRIAN_FILE))
end if
open(10,file=BRIAN_FILE)
do j=1,mi
read(10,*) (ws(p_da+i), i=1,nu)
chc remember the original bounds to return to GUI
do i=1,nu
NDX=RESULTINDEX(j,i,2,nu+rm+mi,NRESULTS)
NTGRESULT(NDX)=ws(p_da+i)
end do
c
read(10,*) (ws(p_da+nu+i), i=1,nu)
chc
do i=1,nu
NDX=RESULTINDEX(j,i,3,nu+rm+mi,NRESULTS)
NTGRESULT(NDX)=ws(p_da+nu+i)
end do
c
do i=1,nu
lb = ws(p_da+i)
ub = ws(p_da+nu+i)
call reg_vr(n, m, sn, vr_reg, bdscl, TYPE_M, j, i,
& lb, ub, ws(p_scl+i))
end do
lb = -1.d10
ub = 1.d10
call reg_vr(n, m, sn, vr_reg, bdscl, TYPE_C, j, 0, lb, ub,
& ws(p_scl+nu+1))
end do
close(10)
else
c ... register M, C variables
do j=1,mi
do i=1,nu
lb = -1.d10
ub = 1.d10
call reg_vr(n, m, sn, vr_reg, bdscl, TYPE_M, j, i,
& lb, ub, ws(p_scl+i))
end do
lb = -1.d10
ub = 1.d10
call reg_vr(n, m, sn, vr_reg, bdscl, TYPE_C, j, 0, lb, ub,
& ws(p_scl+nu+1))
end do
end if
c ----- read nutrient specs of demands: SPECS.DAT/LMTS.DAT -------
inquire(file=LMTS_FILE,exist=file_exists)
if (file_exists) then
com_tol = 0.0
if (msg_log) then
WRITE(nout,*)' reading bounds for demands: ',
& LMTS_FILE(1:trim77(LMTS_FILE))
WRITE(nout,*)' tolerance on bounds is:',com_tol
end if
open(10,file=LMTS_FILE)
do j=1,de
read(10,*) (ws(p_da+i), i=1,2*nu)
chc remember the original bounds to return to GUI
k=0
do i=1,nu
k=k+1
c Max constraint
NDX=RESULTINDEX(j+mi,i,3,nu+rm+mi,NRESULTS)
NTGRESULT(NDX)=ws(p_da+k)
k=k+1
c Min constraint
NDX=RESULTINDEX(j+mi,i,2,nu+rm+mi,NRESULTS)
NTGRESULT(NDX)=ws(p_da+k)
end do
do l=1,nu
tol = max(abs(ws(p_da+2*l)),abs(ws(p_da+2*l-1)))*com_tol
call reg_vr(n, m, sn, vr_reg, bdscl, TYPE_M, mi+j, l,
& ws(p_da+2*l)-tol, ws(p_da+2*l-1)+tol,
& ws(p_scl+l))
c bup(pv_dk+(j-1)*nu+l) = ws(p_da+2*l-1)*(1.+com_tol)
c blo(pv_dk+(j-1)*nu+l) = ws(p_da+2*l)*(1.-com_tol)
end do
lb = -1.d10
ub = 1.d10
call reg_vr(n, m, sn, vr_reg, bdscl, TYPE_C, mi+j, 0,
& lb, ub, ws(p_scl+nu+1))
end do
close(10)
else
if (msg_log) then
WRITE(nout,*)' reading specifications for demands: ',
& SPECS_FILE(1:trim77(SPECS_FILE))
WRITE(nout,*) ' tolerance for specs: '
end if
open(10,file=SPECS_FILE)
read(10,*) com_tol
do j=1,de
read(10,*) (ws(p_da+i), i=1,nu)
do i=1,nu
tol = abs(ws(p_da+i))*com_tol
call reg_vr(n, m, sn, vr_reg, bdscl, TYPE_M, mi+j, l,
& ws(p_da+i)-tol,ws(p_da+i)+tol, ws(p_scl+i))
c blo(pv_dk+(j-1)*nu+i) = ws(p_da+i)*(1.-com_tol)
c bup(pv_dk+(j-1)*nu+i) = ws(p_da+i)*(1.+com_tol)
end do
lb = -1.d10
ub = 1.d10
call reg_vr(n, m, sn, vr_reg, bdscl, TYPE_C, mi+j, 0,
& lb, ub, ws(p_scl+nu+1))
end do
close(10)
end if
c ------------ read bounds on bin-inclusions: INGRID.DAT ---------
c INGRID.DAT now contains bounds on all weights:
c bin/raw_mat maekup of all bins/products
c The old INGRID.DAT/STRAIGHTS.DAT/MIDELMT.DAT
c are now redundant.
c
c INGRID.DAT now also contains information on the sparsity of the
c inclusion graph: Edges with LB=UB=0 simply do not
c exsist.
c
c A variable is registered (with appropriate bounds) for every edge
c that does exist.
c
c A description of the network has already been generated in
c rd_prob_dim: it set up the following arrays:
c (bn_???? are pointers to the first elements to the array in bns)
c
c bn_n_srb [mi+de] #sources for bin/demand
c bn_n_srbb [mi+de] of which other bins
c bn_p_srb [mi+de] pointer into list of src for each bn/de
c bn_l_srb [n_mu] list of sources
c bn_n_tgb [rm+mi] #targets for rm/bin
c bn_n_tgbb [rm+mi] of which other bins
c bn_p_tgb [rm+mi] pointer into list of trg for each rm/bn
c bn_l_tgb [n_mu] list of targets
c bn_fob [mi+de] first order bins (no bin sources)
inquire(file=INGRID_FILE,exist=file_exists)
if (file_exists) then
if (msg_log) then
WRITE(nout,*) ' reading bounds on bin inclusions: ',
& INGRID_FILE(1:trim77(INGRID_FILE))
end if
open(10,file=INGRID_FILE)
do j=1,mi+de
c ... read lower bounds
read(10,*) (ws(p_da+i), i=1,rm+mi)
chc
do i=1,rm+mi
NDX=RESULTINDEX(j,i+nu,2,nu+rm+mi,NRESULTS)
NTGRESULT(NDX)=ws(p_da+i)
end do
c ... read upper bounds
read(10,*) (ws(p_da+rm+mi+i), i=1,rm+mi)
chc
do i=1,rm+mi
NDX=RESULTINDEX(j,i+nu,3,nu+rm+mi,NRESULTS)
NTGRESULT(NDX)=ws(p_da+rm+mi+i)
end do
do i=1,rm+mi
if (max(abs(ws(p_da+rm+mi+i)),abs(ws(p_da+i))).gt.1e-8)
& then
lb = min(max(ws(p_da+i),-limnwt),1.d0+limnwt)
ub = min(max(ws(p_da+rm+mi+i),-limnwt),1.d0+limnwt)
call reg_vr(n, m, sn, vr_reg, bdscl, TYPE_MU, i, j,
& lb, ub, 1.d0)
end if
end do
end do
close(10)
else
c ... no INGRID.DAT exists: take default
print *, INGRID_FILE
print *,INGRID_FILE(1:trim77(INGRID_FILE))//
& ' does not exists: No predefined action'
stop
end if
c ---------------- generate M, C, SUM constraints ------------------
c
c Now that all weight variables and the structure of the network
c is in place, we can generate the M, C, SUM constraints for
c every BIN/PRODUCT
c
c ... print network if wanted
if (msg_net) call prt_bn(bns, n_bns)
do i=1,mi+de
c ... for all bins + products
srb = bns(bn_n_srb+i)
srbb = bns(bn_n_srbb+i)
srbr = srb-srbb
if (srb+1.gt.max_cs_terms) then
ifail = 3
return
end if
p1 = bns(bn_p_srb+i)
c ... set nutrient content constraint for each nutrient
do j=1,nu
c ... set linear terms
lt_ix1(1,1) = TYPE_M
lt_ix1(2,1) = i
lt_ix1(3,1) = j
lt_co(1) = -1.d0
do k=1,srbr
i1 = bns(p1+k)
lt_ix1(1,1+k) = TYPE_MU
lt_ix1(2,1+k) = i1
lt_ix1(3,1+k) = i
lt_co(1+k) = user(pu_r+(i1-1)*nu+j)
end do
c ... set nonlinear terms
do k=1,srbb
i1 = bns(p1+srbr+k)-rm
nt_ix1(1,k) = TYPE_MU
nt_ix1(2,k) = i1+rm
nt_ix1(3,k) = i
nt_ix2(1,k) = TYPE_M
nt_ix2(2,k) = i1
nt_ix2(3,k) = j
nt_co(k) = 1.d0
end do
call reg_cs(iuser, user, sz_iuser, sz_user, CTYPE_M, i, j,
& n, m, sn, vr_reg,
& bdscl, srbr+1, srbb, lt_ix1, nt_ix1, nt_ix2, lt_co,
$ nt_co,0.d0, 0.d0, ws(p_scl+j), ifail)
if (ifail.ne.0) return
end do
c >>price constraint
c ... set linear terms
lt_ix1(1,1) = TYPE_C
lt_ix1(2,1) = i
lt_ix1(3,1) = 0
lt_co(1) = -1.d0
cntlin = 1
cntnl = 0
c ... loop over all raw-material sources of this bin
do k=1,srbr
i1 = bns(p1+k)
c ... check if raw-mat has a piecewise linear price
if (DAT%is_piecewise_price(i1)) then
c ... set of a nonlinear term
cntnl = cntnl + 1
nt_ix1(1,cntnl) = TYPE_MU
nt_ix1(2,cntnl) = i1
nt_ix1(3,cntnl) = i
nt_ix2(1,cntnl) = TYPE_C
nt_ix2(2,cntnl) = -i1
nt_ix2(3,cntnl) = 0
nt_co(cntnl) = 1.d0
else
cntlin = cntlin + 1
lt_ix1(1,cntlin) = TYPE_MU
lt_ix1(2,cntlin) = i1
lt_ix1(3,cntlin) = i
lt_co(cntlin) = user(i1)
end if
end do
c ... set nonlinear terms
do k=1,srbb
i1 = bns(p1+srbr+k)-rm
cntnl = cntnl + 1
nt_ix1(1,cntnl) = TYPE_MU
nt_ix1(2,cntnl) = rm+i1
nt_ix1(3,cntnl) = i
nt_ix2(1,cntnl) = TYPE_C
nt_ix2(2,cntnl) = i1
nt_ix2(3,cntnl) = 0
nt_co(cntnl) = 1.d0
end do
call reg_cs(iuser, user, sz_iuser, sz_user, CTYPE_C, i, 0, n,
& m, sn, vr_reg,
& bdscl, cntlin, cntnl, lt_ix1, nt_ix1, nt_ix2, lt_co,
& nt_co ,0.d0, 0.d0, ws(p_scl+nu+1), ifail)
if (ifail.ne.0) return
c >>sum = 1 constraint
do k=1,srb
i1 = bns(p1+k)
lt_ix1(1,k) = TYPE_MU
lt_ix1(2,k) = i1
lt_ix1(3,k) = i
lt_co(k) = 1.d0
end do
call reg_cs(iuser, user, sz_iuser, sz_user, CTYPE_SUM, i, 0, n,
& m, sn, vr_reg,
& bdscl, srb, 0, lt_ix1, nt_ix1, nt_ix2, lt_co, nt_co,
& 1.d0, 1.d0, 1.d0, ifail)
if (ifail.ne.0) return
end do
c ==============================================================
c
c This has been the basic model. The rest involves variables
c that are setup in a sparse manner:
c
c - nu : total inclusion of rm in a certain bin/prod
c - u : total flow through a certain rm/bin
c
c - NU can be specified for [rm][bin/prod]
c if any NU is specified (due to a rminc constrint = bound,
c or a special constraint) then all NU for this [rm] that
c might lead to the current node in the tree must also
c be specified
c - u can be specified for [rm/bin]
c if any U is specified (due to a rmlmts constraint = bound,
c or a special constraint) then all U above the current node
c in the tree must also be specified.
c
c ------------------- read rmlmts.dat -------------------------
c FORMAT:
c [no of raw mats/bins with availability constraints]
c [ix of rm/bin] [lb] [ub] (first rm are raw-mat then bins)
c [ix of rm/bin] [lb] [ub]
c ...
c vars: rm_mi_lb(rm+mi), rm_mi_ub(rm+mi), rm_mi_fg(rm+mi)
c fifo(rm+mi), fifo1, fifo2
c
c set up lists: rm_mi_fg(rm+mi): 1 if rmava c/s needed
c rm_mi_lb(rm+mi): lb if needed
c rm_mi_ub(rm+mi): ub if needed
do i=1,rm+mi
lws(p_rmi_fg+i) = 0
rmava_lb(i) = -0.05*totde
rmava_ub(i) = 1.01d0*totde
end do
fifo1 = 1
fifo2 = 1
inquire(file=RMLMTS_FILE,exist=file_exists)
if (file_exists) then
if (msg_log) then
WRITE(nout,*)
& ' reading bounds on raw material availability: ',
& RMLMTS_FILE(1:trim77(RMLMTS_FILE))
end if
open(10,file=RMLMTS_FILE)
read (10,*) nrst
do i=1,nrst
read (10, '(a)') line
read (line, *, iostat=err) i1, dp1l, dp1u, dp2l, dp2u
c read (10,*) i1, dp1, dp2
if (i1.le.rm+mi) then
rmava_lb(i1) = dp1l
rmava_ub(i1) = dp1u
c print *, i1, dp1l, dp1u
DAT%rmava1lb(i1) = dp1l
DAT%rmava1ub(i1) = dp1u
if (err.eq.0) then
if (i1.le.rm) then
if (.not.DAT%is_piecewise_price(i1)) then
print '(A,I4,A)', 'Raw material ',i1,
& ': two sets of bounds but not two prices'
c stop
end if
DAT%rmava2lb(i1) = dp2l
DAT%rmava2ub(i1) = dp2u
c print *, i1, dp2l, dp2u
rmava_lb(i1) = dp1l+dp2l
rmava_ub(i1) = dp1u+dp2u
else
if (msg_warn_edge) then
print '(A,I4,A)',
& 'WARNING: two set of lb/ub given for rm/mi ',i1,
& ' which is a bin'
end if
end if
end if
lws(p_rmi_fg+i1) = 1
fifo(fifo2) = i1
fifo2 = fifo2 + 1
chc ... remember original bounds to return to GUI
NDX=RESULTINDEX(de+mi+1,i,2,nu+rm+mi,NRESULTS)
NTGRESULT(NDX)=dp1
NDX=RESULTINDEX(de+mi+1,i,3,nu+rm+mi,NRESULTS)
NTGRESULT(NDX)=dp2
else
print *, 'No raw material ',i1
stop
end if
c
end do
close(10)
end if
if (file_exists.or.SPD%n.ne.0) then
c ... scan special file for any U variables mentioned
do k=1,SPD%n
do i=SPD%p_cs(k),SPD%p_cs(k)+SPD%n_terms(k)-1
if (SPD%vtype(i).eq.TYPE_U) then
i1 = SPD%spec1(i)
if (lws(i1).eq.0) then
lws(i1) = 1
fifo(fifo2) = i1
fifo2 = fifo2 + 1
end if
if (SPD%n_terms(k).eq.1) then
c print *,'BND>', SPD%lb(k), SPD%coeff(i), SPD%ub(k)
rmava_lb(i1) = max(rmava_lb(i1),
& SPD%lb(k)/SPD%coeff(i))
rmava_ub(i1) = min(rmava_ub(i1),
& SPD%ub(k)/SPD%coeff(i))
end if
end if
end do
end do
c ... flag all bins/rm that need implied rmava constraints
10 continue
i1 = fifo(fifo1)
i2 = bns(bn_p_tgb+i1)
fifo1 = fifo1 + 1
do i=1,bns(bn_n_tgb+i1)
i3 = bns(i2+i)+rm
if (i3.le.rm+mi) then
if (lws(p_rmi_fg+i3).eq.0) then
lws(p_rmi_fg+i3) = 1
fifo(fifo2) = i3
fifo2 = fifo2 + 1
end if
end if
end do
if (fifo1.lt.fifo2) goto 10
c .... register all vars
do i=1,rm+mi
if (lws(p_rmi_fg+i).eq.1) then
call reg_vr(n, m, sn, vr_reg, bdscl, TYPE_U, i, 0,
& rmava_lb(i), rmava_ub(i), ws(p_scl+nu+2))
end if
end do
c ... register constraints
do i=1,rm+mi
if (lws(p_rmi_fg+i).eq.1) then
tgb = bns(bn_n_tgb+i)
tgbb = bns(bn_n_tgbb+i)
tgbd = tgb-tgbb
if (tgb+1.gt.max_cs_terms) then
ifail = 3
return
end if
p1 = bns(bn_p_tgb+i)
c ...set bilinear terms
do k=1,tgbb
i1 = bns(p1+k)
nt_ix1(1,k) = TYPE_MU
nt_ix1(2,k) = i
nt_ix1(3,k) = i1
nt_ix2(1,k) = TYPE_U
nt_ix2(2,k) = i1+rm
nt_ix2(3,k) = 0
nt_co(k) = 1.d0
end do
c ... set linear terms
lt_ix1(1,1) = TYPE_U
lt_ix1(2,1) = i
lt_ix1(3,1) = 0
lt_co(1) = -1.d0
do k=1,tgbd
i1 = bns(p1+tgbb+k)
lt_ix1(1,1+k) = TYPE_MU
lt_ix1(2,1+k) = i
lt_ix1(3,1+k) = i1
c lt_co(1+k) = ws(i1-mi)
lt_co(1+k) = user(pu_d0+i1-mi)
end do
call reg_cs(iuser, user, sz_iuser, sz_user, CTYPE_U, i,
& 0, n, m, sn, vr_reg, bdscl,tgbd+1, tgbb, lt_ix1,
& nt_ix1, nt_ix2, lt_co, nt_co,0.d0, 0.d0,
& ws(p_scl+nu+2), ifail)
if (ifail.ne.0) return
end if
end do
end if
c ------------------- read rminc.dat -------------------------
c FORMAT:
c
c [no of c/s of type RMINC]
c [ix of bin/product] [ix of raw_mat] [lower bound] [upper bound]
c [ix of bin/product] [ix of raw_mat] [lower bound] [upper bound]
c [ix of bin/product] [ix of raw_mat] [lower bound] [upper bound]
c [ix of bin/product] [ix of raw_mat] [lower bound] [upper bound]
c ...
cFIXME: saved local info in ws(p_da), lws(p_lda)
c
c work out which bin/prod-rm combinations need a NU (total ingred)
c variable
c
nrst = 0
do i=1,rm
lws(p_lda+i) = 0
end do
inquire(file=RMINC_FILE,exist=file_exists)
if (file_exists) then
if (msg_log) then
WRITE(nout,*)
& ' reading bounds on raw_mat inclusion in products: ',
& RMINC_FILE(1:trim77(RMINC_FILE))
end if
c ... read file into temporary arrays:
c lws(p_lda+ i) bin/de affected by cs-i
c lws(p_lda+ nrst+i) rm affected by cs-i
c lws(p_lda+2*nrst+i) no of c/s affecting rm-i
c ws(p_da+ i) lower bound of cs-i
c ws(p_da+ nrst+i) upper bnd of cs-i
open(10,file=RMINC_FILE)
read(10,*) nrst
chc ... remember to pass to getsensitivity via common
NUM_GIG_RESULTS=nrst
c
do i=1,rm
lws(p_lda+2*nrst+i) = 0
end do
do i=1,nrst
read(10,*) lws(p_lda+i), lws(p_lda+nrst+i),
& ws(p_da+i), ws(p_da+nrst+i)
c ... count no of constraints for given raw-mat
lws(p_lda+2*nrst+lws(p_lda+nrst+i)) =
& lws(p_lda+2*nrst+lws(p_lda+nrst+i))+1
end do
close(10)
end if
if (file_exists.or.SPD%n.ne.0) then
c ... for all raw_materials that have constraints
do i=1,rm
c ... get list of all bin/de affected by particular rm
do j=1,mi+de
lws(p_rmi_fg+j) = 0 !flag if NU variable needed
c FIXME: these bounds should be tightened by working through
c from the rm level how much of the rm could end up in
c the bin in question. I'm not doing this now, there should
c be no problem from fairly lax bounds (26/10/06)
ws(p_rmi_lb+j) = -1.d0 ! and bounds for it
ws(p_rmi_ub+j) = 2.d0
end do
c for each rm make list of bins/de that have constraints
c lws(p_rmi_fg+j): =1 if bin/de needs NU var for this rm
c ws(p_rmi_lb+j): bounds on NU variable
c ws(p_rmi_ub+j):
fifo1 = 1
fifo2 = 1
c fifo(fifo1:fifo2) is FIFO stack of bin/de with rminc constraints
do j=1,nrst
if (lws(p_lda+nrst+j).eq.i) then
c ... mark all bin/de affected directly by constraints
k = lws(p_lda+j)
lws(p_rmi_fg+k) = 1
ws(p_rmi_lb+k) = ws(p_da+j)
ws(p_rmi_ub+k) = ws(p_da+nrst+j)
c ... and put the on the stack
fifo(fifo2) = k
fifo2 = fifo2 + 1
end if
end do
c ... scan special file for any NU variables for this rm
do k=1,SPD%n
do j=SPD%p_cs(k),SPD%p_cs(k)+SPD%n_terms(k)-1
if ((SPD%vtype(j).eq.TYPE_NU).and.
& SPD%spec1(j).eq.i) then
i1 = SPD%spec2(j)
if (lws(p_rmi_fg+i1).eq.0) then
lws(p_rmi_fg+i1) = 1
fifo(fifo2) = i1
fifo2 = fifo2 + 1
end if
if (SPD%n_terms(k).eq.1) then
c print *,'BND>', SPD%lb(k), SPD%coeff(j), SPD%ub(k)
ws(p_rmi_lb+i1) = max(ws(p_rmi_lb+i1),
& SPD%lb(k)/SPD%coeff(j))
ws(p_rmi_ub+i1) = min(ws(p_rmi_ub+i1),
& SPD%ub(k)/SPD%coeff(j))
end if
end if
end do
end do
c ... and also include all bin-sources
do
if (fifo1.ge.fifo2) exit
c get bin/product of the stack and find all sources of it
i1 = fifo(fifo1)
c i1 is a bin/product taken off the stack
i2 = bns(bn_p_srb+i1)
c i2 is a pointer (into bns) to a list of sources of i1
srb = bns(bn_n_srb+i1)
srbb = bns(bn_n_srbb+i1)
srbr = srb-srbb
c srb: total #sources, srbb: of which other bins
fifo1 = fifo1 + 1
c loop through all bin sources of i1
do j=srbr+1,srb
i3 = bns(i2+j)-rm
if (lws(p_rmi_fg+i3).eq.0) then
lws(p_rmi_fg+i3) = 1
fifo(fifo2) = i3
fifo2 = fifo2 + 1
end if
end do
end do
c at this point all bin/products with lws(p_rmi_fg+j), j=1,..,mi+de
c need a NU variable (total raw mat) for ram material i
c ... register all vars
do j=1,mi+de
if (lws(p_rmi_fg+j).eq.1) then
c ... if bin-j has bin sources, need NU variable
c (otherwise just use MU)
if (bns(bn_n_srbb+j).ne.0) then
call reg_vr(n, m, sn, vr_reg, bdscl, TYPE_NU,
& i, j, ws(p_rmi_lb+j),
& ws(p_rmi_ub+j), 1.d0)
else
c if there actually is a bound on NU in a first order bin,
c then this bound could be set throgh ingrid.dat
c Nevertheless, a corresponding MU exists, which is tightened
c if necessary
if (ws(p_rmi_lb+j).gt.0.d0.or.
& ws(p_rmi_ub+j).lt.1.d0) then
c ... if first-order-bin then there is a corresponding