Previous in Forum: Confused Regarding Secure USB Memory Creating Series of Empty Files in Linux Mint (Inodes?)   Next in Forum: Can I Customise My Android OS to Send Coded Message Through a Phone Call?
Close
Close
Close
13 comments
Rate Comments: Nested
Active Contributor

Join Date: Sep 2017
Posts: 17

AMPL

11/30/2019 4:46 AM

Can someone please tell me what is wrong with my ampl code?

set Q; #quarters

set c; #cities

param d{i in c, j in c}; # distance between city i and city j

param D{i in c, q in Q}; # demand of each city in each quarter

param U{i in c, q in Q}; # produced units limit for production facility in city i in quarter q (production capacity)

param ccp{i in c}; # contract cost for production facility in city i

param cp{q in Q}; # cost per unit produced in quarter q

param cw{q in Q}; # cost per unit warehoused at the end of quarter q

param t1 = .1; # the transportation cost as a function of the distance to wh

param t2 = .125; # the transportation cost as a function of the distance to city

var fp{i in c} binary; # 1 production centers in city i was selected, 0 o.w. (total of 4 cities)

var fw{i in c} binary; # 1 production centers in city i was selected, 0 o.w. (total of 3 cities)

var xp{x in c,q in Q}>=0; # the numb of units to be produced in each production facility and quarter

var xw{i in c, q in Q}>=0; # the number of units in each warehouse’s inventory at the end of each quarter

#var ypi{i in c, q in Q}; # the number of units transported from each production facility to each city

#var ywi{i in c, q in Q}; # the number of units transported from each warehouse to each city in each quarter

#var ypw {i in c,q in Q}; # the units are transported from each production facility to each warehouse in each quarter

minimize tot_cost:

sum{i in c, q in Q} cp[q]*xp[i,q]*fp[i] + sum{i in c} ccp[i]*fp[i] + sum{q in Q,i in c} cw[q]*xw[i,q]*fw[i] + sum{i in c,j in c,q in Q} t1*d[i,j]*xp[i,q]*fp[i] + sum{i in c, j in c,q in Q} t2 *d[i,j]*xp[i,q]*fp[i];

s.t.

#Production_Cities: sum{i in c} fp[i] = 4;

#WH_Cities: sum{i in c}fw[i] = 3;

relation{i in c, q in Q}: xp[i,q]<= 1000000 * fp[i]

Demand{i in c, q in Q}: xp[i,q]*fp[i] = D[i,q]; # quarterly demand of a city must be met exactly

Capacity{i in c, q in Q}: xp[i,q]*fp[i] <=U[i,q]*fp[i];# units output by production facility must be less than its (production capacity) for each quarter (<=U)

# One production facility must be contracted from the 6 most populated cities, another from the next 6, another from the next 6, and the last from the 7 least populated cities.

# contracts with warehouses can change from quarter to quarter, but the locations must be other than where production facilities are contracted. (fw != lofp)

#At the end of each subsequent quarter (except 1), warehouse’s inventory must equal the ending inventory from the previous quarter, plus units arriving from production during the current quarter, minus units delivered from it to satisfy demands during the current quarter.

# Warehous_Units{i in c, q in Q} : xw[i,q]*fw[i]=xw[i,q]*fw[i]+xp[i,q]*fp[i]-D[i,q];

The following is the correct data file:

# Set containing cities numbered from 1 to n=25

data;

set c:= 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;

#Time Quarters

set Q:= 1 2 3 4;

# Distance Matrix ( Distance between city i and city j)

param d : 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:=

1 0.5 347.64 663.04 529.03 428.1 1062.22 433.68 435.54 519.57 525.4 886.53 1000.53 627.82 442.85 26.1 723.77 2187.62 401.28 59.13 457.49 464.55 584.59 168.72 525.13 433.5

2 347.64 0.5 786.46 846.61 440.99 987.72 439.97 443.34 836.61 842.72 899.22 881.01 626.23 161.5 373.73 838.9 1846.91 748.7 291.04 110.71 678.15 262.39 468.75 842.73 445.16

3 663.04 786.46 0.5 535.62 354.43 578.65 358.12 354.74 530.15 533.25 312.35 616.9 265.75 717.24 660.71 61.02 2470.16 712.36 687.95 863.61 246.71 827.49 523.04 534.45 351.54

4 529.03 846.61 535.62 0.5 625.08 1108.75 633.67 632.33 10.1 3.98 847.85 1122.88 712.56 881.81 507.03 580.19 2690.08 256.73 586.25 955.67 319.25 1028.46 378.45 3.92 627.51

5 428.1 440.99 354.43 625.08 0.5 634.12 8.64 8.32 615.74 621.31 477.53 579.94 205.34 363.07 438.8 402.18 2145.9 674.52 425.75 510.7 336.28 480.46 369.07 621.99 5.44

6 1062.22 987.72 578.65 1108.75 634.12 0.5 628.61 626.71 1102.4 1106.06 275.29 157.22 441.08 841.77 1072.57 547.49 2233.02 1250.91 1057.33 1004.25 797.04 858.88 984.79 1107.2 628.73

7 433.68 439.97 358.12 633.67 8.64 628.61 0.5 3.42 624.34 629.9 475.2 572.98 202.06 359.13 444.73 405.06 2139.26 682.84 430.38 508.13 344.22 474.93 376.77 630.58 7.3

8 435.54 443.34 354.74 632.33 8.32 626.71 3.42 0.5 623.02 628.57 472.3 571.83 199.37 362.5 446.43 401.64 2142.12 682.78 432.6 511.56 342.04 478.1 377.37 629.25 4.88

9 519.57 836.61 530.15 10.1 615.74 1102.4 624.34 623.02 0.5 6.14 842.25 1115.49 704.67 871.72 497.67 575.37 2680 253.03 576.69 945.65 311.61 1018.38 368.52 6.41 618.21

10 525.4 842.72 533.25 3.98 621.31 1106.06 629.9 628.57 6.14 0.5 845.44 1119.8 709.3 877.83 503.44 578.06 2686.14 255.46 582.57 951.76 316.07 1024.48 374.59 1.32 623.76

11 886.53 899.22 312.35 847.85 477.53 275.29 475.2 472.3 842.25 845.44 0.5 355.39 275.07 780.75 891.49 274.6 2376.28 1012.36 894.88 945.19 547.48 844.58 778.83 846.63 472.33

12 1000.53 881.01 616.9 1122.88 579.94 157.22 572.98 571.83 1115.49 1119.8 355.39 0.5 416.19 727.27 1014.14 600.16 2078.97 1235.19 987.48 884.7 804.05 726.22 946.35 1120.84 574.87

13 627.82 626.23 265.75 712.56 205.34 441.08 202.06 199.37 704.67 709.3 275.07 416.19 0.5 517.12 636.09 286.1 2211.99 820.24 629.61 678.76 393.67 600.66 543.78 710.27 199.98

14 442.85 161.5 717.24 881.81 363.07 841.77 359.13 362.5 871.72 877.83 780.75 727.27 517.12 0.5 467.72 762.98 1815.8 828.75 395.54 165.49 659.87 147.94 519.72 878.08 365.77

15 26.1 373.73 660.71 507.03 438.8 1072.57 444.73 446.43 497.67 503.44 891.49 1014.14 636.09 467.72 0.5 721.62 2213.2 375.24 84.49 483.56 455.66 610.02 154.88 503.13 444.16

16 723.77 838.9 61.02 580.19 402.18 547.49 405.06 401.64 575.37 578.06 274.6 600.16 286.1 762.98 721.62 0.5 2497.85 766.82 748.06 912.84 303.58 867.66 583.97 579.3 398.83

17 2187.62 1846.91 2470.16 2690.08 2145.9 2233.02 2139.26 2142.12 2680 2686.14 2376.28 2078.97 2211.99 1815.8 2213.2 2497.85 0.5 2585.79 2128.71 1736.53 2470.63 1675.08 2314.61 2686.25 2146.56

18 401.28 748.7 712.36 256.73 674.52 1250.91 682.84 682.78 253.03 255.46 1012.36 1235.19 820.24 828.75 375.24 766.82 2585.79 0.5 459.17 858.75 466.65 975.22 320.99 254.32 678.62

19 59.13 291.04 687.95 586.25 425.75 1057.33 430.38 432.6 576.69 582.57 894.88 987.48 629.61 395.54 84.49 748.06 2128.71 459.17 0.5 400.21 502.97 534.05 218.5 582.34 431.19

20 457.49 110.71 863.61 955.67 510.7 1004.25 508.13 511.56 945.65 951.76 945.19 884.7 678.76 165.49 483.56 912.84 1736.53 858.75 400.21 0.5 775 194.23 578.45 951.8 514.17

21 464.55 678.15 246.71 319.25 336.28 797.04 344.22 342.04 311.61 316.07 547.48 804.05 393.67 659.87 455.66 303.58 2470.63 466.65 502.97 775 0.5 795.94 304.07 317.07 337.2

22 584.59 262.39 827.49 1028.46 480.46 858.88 474.93 478.1 1018.38 1024.48 844.58 726.22 600.66 147.94 610.02 867.66 1675.08 975.22 534.05 194.23 795.94 0.5 667.63 1024.76 482.08

23 168.72 468.75 523.04 378.45 369.07 984.79 376.77 377.37 368.52 374.59 778.83 946.35 543.78 519.72 154.88 583.97 2314.61 320.99 218.5 578.45 304.07 667.63 0.5 374.54 373.79

24 525.13 842.73 534.45 3.92 621.99 1107.2 630.58 629.25 6.41 1.32 846.63 1120.84 710.27 878.08 503.13 579.3 2686.25 254.32 582.34 951.8 317.07 1024.76 374.54 0.5 624.45

25 433.5 445.16 351.54 627.51 5.44 628.73 7.3 4.88 618.21 623.76 472.33 574.87 199.98 365.77 444.16 398.83 2146.56 678.62 431.19 514.17 337.2 482.08 373.79 624.45 0.5

;

# Demand of each city i in Quarter q

param D: 1 2 3 4:=

1 3452 5753 4602 9204

2 3515 5858 4687 9373

3 3535 5892 4713 9426

4 3536 5893 4714 9429

5 3662 6104 4883 9766

6 3691 6151 4921 9842

7 3805 6342 5074 10147

8 3850 6416 5133 10266

9 3856 6426 5141 10282

10 3997 6662 5330 10659

11 4163 6939 5551 11102

12 4460 7433 5947 11893

13 4768 7947 6357 12715

14 4926 8211 6569 13137

15 5021 8369 6695 13390

16 5323 8872 7097 14194

17 5728 9546 7637 15274

18 6019 10032 8025 16051

19 6553 10922 8737 17475

20 8525 14208 11366 22732

21 10115 16858 13486 26973

22 10548 17580 14064 28128

23 12213 20355 16284 32568

24 24323 40538 32431 64861

25 48839 81399 65119 130238

;

# Produced units limit for production facility in city i in quarter q

param U: 1 2 3 4:=

1 97485 97485 64990 103984

2 97454 97454 64969 103951

3 97444 97444 64962 103940

4 97443 97443 64962 103940

5 97380 97380 64920 103872

6 97366 97366 64911 103857

7 97309 97309 64872 103796

8 97286 97286 64858 103772

9 97283 97283 64856 103769

10 97213 97213 64808 103693

11 97130 97130 64753 103605

12 96981 96981 64654 103447

13 96827 96827 64551 103282

14 96748 96748 64499 103198

15 96701 96701 64467 103147

16 96550 96550 64367 102986

17 96347 96347 64232 102771

18 96202 96202 64134 102615

19 95935 95935 63956 102330

20 94949 94949 63299 101279

21 94154 94154 62769 100431

22 93937 93937 62625 100200

23 93105 93105 62070 99312

24 87050 87050 58033 92853

25 74792 74792 49861 79778

;

#Contract cost for production facility in city i

param: ccp :=

1 5362

2 5667

3 5845

4 5971

5 6072

6 6154

7 6225

8 6287

9 6341

10 6391

11 6437

12 6482

13 6525

14 6566

15 6604

16 6641

17 6677

18 6712

19 6747

20 6790

21 6835

22 6877

23 6922

24 6999

25 7121

;

# Cost per unit produced in quarter q

param cp :=

1 0.75

2 1.0

3 1.0

4 1.5

;

# Cost per unit warehoused at the end of quarter q

param cw :=

1 0.5

2 0.5

3 0.75

4 0.10

;

Register to Reply
Interested in this topic? By joining CR4 you can "subscribe" to
this discussion and receive notification when new comments are added.

"Almost" Good Answers:

Check out these comments that don't yet have enough votes to be "official" good answers and, if you agree with them, vote them!
Active Contributor

Join Date: Sep 2017
Posts: 17
#1

Re: AMPL

11/30/2019 5:47 AM

Edit on comments in code line number 11,

# fp is 1 if city i was selected as the production city (must select 4 cities)

and

# fw is 1 if city i was selected as the warehouse city (must select 3 cities)

Register to Reply
Guru

Join Date: Aug 2005
Location: Hemel Hempstead, UK
Posts: 5826
Good Answers: 322
#2

Re: AMPL

11/30/2019 8:54 AM

Does it go wrong when you try to compile (is it a compiled language?): if so what are the error messages? Or, does it go wrong when you try to run it: what are the error messages? Or does it just produce the wrong result?

Needless to say I have never heard of AMPL, but, I'm sure anyone who had would ask similar questions.

It's a large section of code: can you break it down into more manageable sections to debug?

__________________
If you spend all your time looking for people and things to complain about: trust me, you will find plenty to complain about.
Register to Reply Score 1 for Good Answer
Active Contributor

Join Date: Sep 2017
Posts: 17
#3
In reply to #2

Re: AMPL

11/30/2019 9:58 AM

Thank you for your insights,

When I run it says:

"

MINOS 5.51: ignoring integrality of 50 variables

MINOS 5.51: infeasible problem (or bad starting guess).

25 iterations

Nonlin evals: obj = 6, grad = 6, constrs = 6, Jac = 6.

"

The obj should be 32.3 million. And I should have values of xp in each of the 4 cities where the cost is minimized, here is just selecting the first 4 cities.

Actually it is not that long, it looks so cuz I have posted the data file below in case if someone wants to try to run it.

Register to Reply
Guru

Join Date: Aug 2005
Location: Hemel Hempstead, UK
Posts: 5826
Good Answers: 322
#4

Re: AMPL

11/30/2019 3:06 PM

I can't see where the values for fp(i) are allocated or how you guarentee that only 1 in 6/7 is a 1

Or where the groups of 6 and 7 are specified to the program.

__________________
If you spend all your time looking for people and things to complain about: trust me, you will find plenty to complain about.
Register to Reply
Active Contributor

Join Date: Sep 2017
Posts: 17
#5
In reply to #4

Re: AMPL

11/30/2019 3:20 PM

fp is a binary variable since it is a variable, the program should find it out, I can't set values for it in the data. The purpose of it is to select 4 among the 25 cities set in which the cost will be minimized and to use it (when it is equal to one) to find out the production quantities from those four cities xp*fp. The same thing for the three warehouse cities xw and the binary variable fw. There is something wrong though in the programming which makes fp and fw go wrong, however, that is the mathematical programming for such a problem as far as I am concerned.

Thanks again for your replay.

Register to Reply
Guru

Join Date: Aug 2005
Location: Hemel Hempstead, UK
Posts: 5826
Good Answers: 322
#7
In reply to #5

Re: AMPL

11/30/2019 8:33 PM

Forgive my questions, but, sometimes explaining things to others helps to crystalise our own thoughts.

I see that the cities are arranged in ascending order of size, so, there should be a set of constraints like:-

fp(i1) + fp(i2) + fp(i3) + fp(i4) + fp(i5) + fp(i6) + fp(i7) = 1

fp(i8) + fp(i9) + fp(i10) + fp(i11) + fp(i12) + fp(i13) = 1

and the other two

The constraint

Production_Cities: sum{i in c} fp[i] = 4;

Covers "half" the requirement but it's been commented out.

__________________
If you spend all your time looking for people and things to complain about: trust me, you will find plenty to complain about.
Register to Reply Score 1 for Good Answer
Active Contributor

Join Date: Sep 2017
Posts: 17
#9
In reply to #7

Re: AMPL

12/01/2019 4:13 AM

Thank you for your replay, it was helpful. I commented the "=4" constraint temporarily to check where the error is coming from.

Now the binary variable is not working, it is giving me all ones, not only four for some reason.

Register to Reply
Guru

Join Date: Aug 2005
Location: Hemel Hempstead, UK
Posts: 5826
Good Answers: 322
#10
In reply to #9

Re: AMPL

12/01/2019 8:38 AM

Sorry: I don't know why CR4 puts such a small limit on the size of images in posts; I can't read any of that. People like Solar Eagle know how to overcome the limit, but, I've never figured it out?

Is the all one problem with or without the constraint?

There are 12½ thousand ways of choosing 4 from 25, and, 1330 ways of choosing 3 from 21. That's nearly 17 million different combinations to try.

You can reduce the 12½ thousand to 1512 if you make it (1in7)x(1in6)3

I don't know how the AMPL optimisation algorithm works but I'm guessing that it would make a lot more difference than {12½ to 1½} if you chose that second scenario.

__________________
If you spend all your time looking for people and things to complain about: trust me, you will find plenty to complain about.
Register to Reply
Active Contributor

Join Date: Sep 2017
Posts: 17
#11
In reply to #10

Re: AMPL

12/01/2019 6:31 PM

What you mentioned makes sense. However, I believe all similar mathematical programming optimization problems are relying on the logic that the objective function along with each constraint added will make the feasible area of solutions smaller and smaller. In addition, most good optimization software are able to run as many alternatives.

Register to Reply
Guru

Join Date: Aug 2005
Location: Hemel Hempstead, UK
Posts: 5826
Good Answers: 322
#12
In reply to #11

Re: AMPL

12/01/2019 8:59 PM

So the all 1s problem is with the constraint?

__________________
If you spend all your time looking for people and things to complain about: trust me, you will find plenty to complain about.
Register to Reply
Active Contributor

Join Date: Sep 2017
Posts: 17
#13
In reply to #12

Re: AMPL

12/02/2019 5:00 AM

Yes. Thank you!

Register to Reply
Guru

Join Date: Mar 2007
Location: by the beach in Florida
Posts: 33392
Good Answers: 1817
#6

Re: AMPL

11/30/2019 5:57 PM
__________________
All living things seek to control their own destiny....this is the purpose of life
Register to Reply
Active Contributor

Join Date: Sep 2017
Posts: 17
#8
In reply to #6

Re: AMPL

12/01/2019 2:42 AM

I'm trying to optimize or minimize the total cost (transportation, contract, production, warehouse,..) of opening 4 factories to supply 25 cities and three warehouses with a set of constraints available. Maybe other programs can do that, but I'm certain that AMPL does that as well.

Thanks for your reply.

Register to Reply
Register to Reply 13 comments

"Almost" Good Answers:

Check out these comments that don't yet have enough votes to be "official" good answers and, if you agree with them, vote them!
Copy to Clipboard

Users who posted comments:

kaka.kaka (7); Randall (5); SolarEagle (1)

Previous in Forum: Confused Regarding Secure USB Memory Creating Series of Empty Files in Linux Mint (Inodes?)   Next in Forum: Can I Customise My Android OS to Send Coded Message Through a Phone Call?

Advertisement