I'm trying to make algorithm for optimization daily task schedule. For example:
1. There are 16 tasks to finish in one day, expirience worker can finish 8 tasks and newer worker could finish 4 tasks. As output we should get 2 expirienced worker and none new worker.
For now I have this code, but it's not working properly:
private void calculate(int numberOfTasks, int oldWorkerValue, int youngWorkerValue) {
int numberOfOldWorkers = numberOfTasks / oldWorkerValue;
int numberOfYoungWorkers = 0;
if(numberOfOldWorkers == 0) {
numberOfOldWorkers = 1;
} else {
numberOfYoungWorkers = (numberOfTasks % numberOfOldWorkers) / youngWorkerValue;
if(numberOfTasks % oldWorkerValue != youngWorkerValue*numberOfYoungWorkers) {
numberOfYoungWorkers = numberOfYoungWorkers + 1;
}
}
System.out.println("Expirienced worker: " + numberOfOldWorkers + " and new workers: " + numberOfYoungWorkers);
}
it returns me number of worker but it doesn't work like it should be. I don't get result like I wrote in this few examples. Could you give me some advice for this?
1 Answer 1
I'm not sure whether you can find exact formula for this, so I'd do simple brute force:
public static Pair<Integer, Integer> calculate(int numberOfTasks, int oldWorkerValue, int youngWorkerValue) {
int m = Integer.MAX_VALUE; // minimal value
int o = 0; // old workers
int n = 0; // new workers
for (int i = 1; i <= (int)Math.ceil((double) numberOfTasks / oldWorkerValue); i++) {
int nw = (int) Math.ceil((double)(numberOfTasks - i * oldWorkerValue) / youngWorkerValue);
if (nw < 0) nw = 0;
int tm = i * oldWorkerValue + nw * youngWorkerValue;
if (tm < m) {
m = tm; o = i; n = nw;
}
}
return new Pair<>(o, n); // pair <old workers, new workers>
}
answered Jul 11, 2019 at 18:59
4 Comments
ankerr
everything works great, but can you pls explain me a little bit more this code, because i've never working with brute force, and also this first line confuse me, you write comment minimal value but you take max integer value
Iłya Bursov
brute force just checks all possible variants and finds minimal work usage, this minimal usage is stored in variable
m
, but initial values - maximal possible, to simplify condition in loopankerr
thank you, and just one question. I've make test with 100 tasks and as result it return 1 experienced and 15 new workers, and I expect it would return 10 experienced workers. Is this ok?
Iłya Bursov
@ankerr you see, both answers are technically correct, this algorithm tries to minimize number of experience workers (in case of multiple minimal solutions overall), but if you change
if (tm < m) {
to if (tm <= m) {
, it will try to maximize it insteadlang-java
If there are 17 tasks, as output we should get 2 expirienced worker.
this gives us 2*10 = 20, why can't we use 3 new workers, this will be only 18?