-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdynamic_programming1.m
More file actions
140 lines (97 loc) · 3.83 KB
/
dynamic_programming1.m
File metadata and controls
140 lines (97 loc) · 3.83 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
clc, clear, close all
%A regional electric power company is planning a large investment in power
% plants over the next few years. A total of eight power plants must be
% built over the next six years because of both increasing demand in the
% region and the energy crisis, which has forced the closing of some of
% their antiquated fossil- fuel plants. Suppose that, for a first
% approximation, we assume that demand for electric power in the region is
% known with certainty and that we must satisfy the minimum levels of
% cumulative demand indicated in Table below. The power company has decided
% at least to meet this minimum-demand schedule. The building of power
% plants takes approximately one year. In addition to a cost directly
% associated with the construction of a plant, there is a common cost of
% $1.5 million incurred when any plants are constructed in any year,
% independent of the number of plants constructed. This common cost results
% from contract preparation and certification of the impact statement for
% the Environmental Protection Agency. In any given year, at most three
% plants can be constructed. The cost of construction per plant is given in
% Table below for each year in the planning horizon. These costs are
% currently increasing due to the elimination of an investment tax credit
% designed to speed investment in power. However, new technology should be
% available soon, which will tend to bring the costs down, even given the
% elimination of the investment tax credit.
% Year Cumulative demand(in number of plants) Cost per plant ($ × 1000)
% 1991 1 5400
% 1992 2 5600
% 1993 4 5800
% 1994 6 5700
% 1995 7 5500
% 1996 8 5200
% SEE THE GRAPH NETWORK
years = 1:6; % 1991–1996
T = length(years);
D = [1 2 4 6 7 8]; % cumulative demand per year
C = [5400 5600 5800 5700 5500 5200]; % cost per plant (in $1000)
F = 1500; % fixed cost per year (if k>0)
Kmax = 3; % max plants per year
TotalPlants = 8;
% optimize the cost value --> 48.8M $
INF = 1e12;
% DP table: f(t,x)
% t = 1..6, x = 0..8
f = INF * ones(T+2, TotalPlants+1);
% Decision table to recover policy
policy = zeros(T, TotalPlants+1);
for x = 0:TotalPlants
if x == TotalPlants
f(T+1, x+1) = 0; % MATLAB index shift
else
f(T+1, x+1) = INF;
end
end
%Backward computation
for t = T:-1:1 % all years backward
for x = 0:TotalPlants % plants built before year t
bestCost = INF;
bestK = 0;
for k = 0:Kmax % each decision
% cumulative demand constraint
if x + k < D(t)
continue;
end
% cannot exceed total requirement
if x + k > TotalPlants
continue;
end
% cost this year
if k == 0
costThisYear = 0;
else
costThisYear = k * C(t) + F;
end
% cost-to-go
costFuture = f(t+1, x+k+1); % +1 for MATLAB indexing
totalCost = costThisYear + costFuture;
% update best
if totalCost < bestCost
bestCost = totalCost;
bestK = k;
end
end
f(t, x+1) = bestCost;
policy(t, x+1) = bestK;
end
end
x = 0; % start with zero plants built
opt_build = zeros(1, T);
for t = 1:T
k = policy(t, x+1);
opt_build(t) = k;
x = x + k;
end
opt_cost = f(1, 1);
fprintf('\nOptimal yearly construction schedule:\n');
for t = 1:T
fprintf('Year %d: build %d plants\n', 1990 + t, opt_build(t));
end
fprintf('\nTotal minimum cost = %.2f million dollars\n', opt_cost/1000);