An electric utility models its service area as a network of power plants and demand regions. The utility keeps three parallel data structures:
generation — a double[] where generation[p] is the rated output of plant p, in megawatts (MW).
demand — a double[] where demand[r] is the instantaneous load of region r, in megawatts (MW).
distance — a double[][] where distance[p][r] is the length of the transmission line from plant p to region r, in kilometers. A value of -1.0 indicates that no line exists.
Power delivered to a region is reduced by transmission loss. The utility uses the following efficiency factor for a line of length d kilometers:
efficiency(d) = 1.0 - 0.002 * d / / d >= 0
If the efficiency is negative for some d, the method is still required to return the literal result of the formula (the utility will reject such a route elsewhere).
You will implement two methods of the EnergyGrid class. A partial class declaration is shown below.
public class EnergyGrid
{
private double[] generation;
private double[] demand;
private double[][] distance;
/ / constructor and other methods not shown
public double powerDelivered(int plant, int region)
{ / * to implement in part (a) * / }
public int closestAdequatePlant(int region)
{ / * to implement in part (b) * / }
}
Part (a) — powerDelivered
Write the method powerDelivered. The method returns the power (in MW) that plant plant delivers to region region, computed as generation[plant] * efficiency(distance[plant][region]). If no line exists between the plant and the region (distance[plant][region] equals -1.0), the method must return 0.0 without computing the formula.
Method Signature
public double powerDelivered(int plant, int region)
For example, suppose generation = {400.0, 260.0}, demand = {150.0, 300.0, 90.0}, and
distance = {
{ 50.0, 120.0, - 1.0 },
{220.0, 80.0, 30.0 }
}
Then powerDelivered(0, 0) returns 400.0 * (1.0 - 0.002 * 50.0) = 360.0. powerDelivered(0, 2) returns 0.0 because no line exists. powerDelivered(1, 1) returns 260.0 * (1.0 - 0.002 * 80.0) = 218.4.
Part (b) — closestAdequatePlant
Write the method closestAdequatePlant. The method returns the index of the plant that is closest to region region (smallest distance[plant][region]) among all plants that satisfy both of the following conditions:
- a transmission line exists between the plant and the region; that is,
distance[plant][region] is not -1.0;
- the power delivered to the region by that plant, as computed in part (a), is at least
demand[region].
If no plant satisfies both conditions, the method returns -1. If several plants tie for the smallest distance, the method returns the smallest index among them.
Assume part (a) is implemented correctly. You must call powerDelivered; do not repeat its formula.
Method Signature
public int closestAdequatePlant(int region)
Continuing the example above: for closestAdequatePlant(1), plant 0 delivers 400 * (1 - 0.002 * 120) = 304.0 MW, which meets the 300 MW demand, and plant 1 delivers 260 * (1 - 0.002 * 80) = 218.4 MW, which does not. Only plant 0 qualifies, so the method returns 0. For closestAdequatePlant(2) only plant 1 has a line (distance 30, delivery 260 * 0.94 = 244.4 MW > 90), so the method returns 1.