I'm trying to program the interval scheduling problem with dynamic programming. All jobs have different (positive) weights and don't overlap. These weights represent different run times. An idle time of three "gaps" may exist between jobs. Furthermore, each time unit for each job (in seconds) takes one resource. The resources can all have different values. I want to find the smallest sum of resources for all jobs with a dynamic programming algorithm (recursively).
It may be more clear with an example:
Let's say you have three jobs with time units { 2, 2, 3 }
and you have a list of resources of eight long { 2, 5, 1, 8, 4, 1, 1, 5 }
. Job one takes two time units and thus two resources, and because it's the first job it will take the first 2 resources of the resources-list. Job two doesn't have to start immediately after job one, it can also start from one of the next three "gaps". This second job takes also two resources and because it can start from one of the three gaps and it's not the first job, the possibilities of resources that it can take are (1,8) (8,4) (4,1) (1,1) = 9 12 5 2
(the different sums of the available resources). The sum of all jobs is of course less than the number of resources.
This is continued until all jobs are finished. I want to find the smallest sum of resources for all jobs with a dynamic programming algorithm (recursively).
I tried different solving methods, but I find this one very hard to solve recursively without any other function.
I did write the following code, which is not doing as I expected:
public static double getCost(int t, int q, int r, int[] jobs, double[] resources){
double table[][] = new double[t+1][r+1];
for(int i = 0;i < t;i++){
for(int j = 0;j < r;j++){
double cost = 0;
for(int k = j-jobs[i] + 1;k <= j;k++){
if(k < 0){
break;
}
cost = cost + resources[k];
}
double min = Double.POSITIVE_INFINITY;
for(int l = 1;l <= q;l++){
if(i == 0 && l == 1){
min = cost+j-jobs[0]-l;
}
else{
double neww = cost+j-jobs[i]-l;
if(neww < min){
min = neww;
}
}
}
table[i+1][j+1] = min;
}
}
return table[t][r];
}
Can someone please give me some advice?