Originally posted by: PhilippeLaborie
The constraints you posted (usage <= ResourceCapacity + costVar) says that at any time point t, you have usage[t]<=ResourceCapacity + costVar. Thus if you minimize costVar, you minimize the peak usage of the resource, not the area between usage and the constant function ResourceCapacity + costVar. This explains the discrepancy with what you expect.
If you want to measure the sum of the extra resource consumption at each time-point, you would have to discretize time and will loose most of the advantages of CP Optimizer (but it may still be ok depending on your problem).
You can also discretize time with a larger granularity than the time unit and consider a period P>=1. You can have an expression that measures the peak extra-consumption for every period P (so on [0,P), [P,2P), [2P,3P),...). Then in the model, you create one additional interval variable free[i] for each period with fixed start time (i*P) and end time ((i+1)*P). Suppose that Max is an integer denoting the resource capacity you can by no way exceed (you need to have Max>=ResourceCapacity; Max can be an arbitrary large number but depending on your application, you may also consider a really hard max value for the resource). Then, free[i] will reserve (with a pulse) a certain quality of the extra resource (between 0 and Max-ResourceCapacity) that will not be consumed over the ith period. So free[i] will contribute to the cumul function with a variable range in 0..Max-ResourceCapacity and you will try to maximize the height of these pulses.
So you have a cumul function:
f = usage + sum(i) pulse(free[i],0..Max-ResourceCapacity);
You have:
f <= Max
The peak of over-consumption of the resource on period i is the integer expression : Max - heightAtStart(free[i],f)
So you want to minimize sum(i) ( Max - heightAtStart(free[i],f) )
What you ideally would like is to do P=1, it will work but will probably result in a large number of periods. And the model you sketched in your question is the same as the special case where you only have one interval (P equal to the whole horizon of the schedule).
#CPOptimizer#DecisionOptimization