This is a basic constrained optimization problem. This will require linear programming because you have inequality constraints.
Alternatively since this is entirely linear and each factory is independent of each other, work out which option A/B offers the highest return per factory. Call this the chosen product. Work out how many units of the chosen product can be produced given the constraint. Look at the factory downtime, if this is bigger than required to build more unit of the non-chosen item then this must also be produced.
Then compare hour blocks to produce the object A,B (i.e. F1 is 3 or 7) to calculate how many multiples of the not-chosen product are foregone in producing the last unit of the chosen product, you must also add on any factory downtime at this stage. i.e. let the value for the chosen product be 7 and 3 for the non-chosen, then we have 2 units foregone. But if we have 2 hours downtime, then it should be (7+2) / 3 to yield 3 units foregone.
Compare if the units foregone for the last output of the chosen product at a given factory exceed in value the last unit produced (this is the remainder problem).
If true, then the factory produces this mixed output. If false, proceed with the prior output strategy for said factory.
Select highest and then move on to the next factory. Rest should be obvious.
This is my C++ code to solve the problem using my alternative method. I hope this clears up all questions.
// Cybernex's example program in c++
include iostream
using namespace std;
// prototypes and n11 variables setting
void announcer(int);
void calculatefactory (float, float, float, float, float);
void closingannounce();
float price1 = 10.5;
float price2 = 13;
float hourf1a = 3;
float hourf1b = 7;
float hourf2a = 6;
float hourf2b = 5;
float hourf3a = 16;
float hourf3b = 11;
float totalf1 = 5;
float totalf2 = 11;
float totalf3 = 21.6;
// total output of good a
float r1 = 0;
// total output of good b
float r2 = 0;
// total value of output
float r3 = 0;
int main(){
// factory 1
announcer(1);
calculatefactory (price1, price2, hourf1a, hourf1b, totalf1);
// factory 2
announcer(2);
calculatefactory (price1, price2, hourf2a, hourf2b, totalf2);
// factory 3
announcer(3);
calculatefactory(price1, price2, hourf3a, hourf3b, totalf3);
// closing outputs
closingannounce();
return 0;
};
// for clarity in outputs
void announcer (int a){
cout << endl << "We are calculating for factory: " << a << endl << endl;
};
// gives final results
void closingannounce (){
cout << endl << endl << endl << endl;
cout << "Total quantity of A produced by all factories " << r1 << endl;
cout << "Total quantity of B produced by all factories " << r2 << endl;
r3 = (r1 * price1);
r3 = r3 + (r2 * price2);
cout << "Total value of output is " << r3 << endl;
}
// main alg
void calculatefactory (float pricea, float priceb, float hoursa, float hoursb, float totaltime){
// CHECK AT LEAST 1 UNIT CAN BE PRODUCED FOR EACH GOOD
bool canamake = false;
bool canbmake = false;
if (hoursa <= totaltime){
canamake = true;
};
if (hoursb <= totaltime){
canbmake = true;
};
// No output possible
if (canamake == false && canbmake == false){
cout << endl << "No output possible at this factory" << endl;
};
// A is possible, B is not
if (canamake == true && canbmake == false){
// first step work out how many of A can be made
int internaloutput = totaltime / hoursa;
// We cannot use the remaining hours so this is total output for this factory
r1 = r1 + internaloutput;
cout << endl << "The factory produces " << internaloutput << " of good A" << endl;
cout << "The factory produces 0 of good B" << endl;
};
// B is possible, A is not
if (canamake == false && canbmake == true){
// first step work out how many of B can be made
float internaloutput = totaltime / hoursb;
// We cannot use the remaining hours so this is total output for this factory
r2 = r2 + internaloutput;
cout << endl << "The factory produces " << internaloutput << " of good B" << endl;
cout << "The factory produces 0 of good A" << endl;
};
// A and B are feasible
if (canamake == true && canbmake == true){
// SELECT CHOSEN OUTPUT
float internal1 = pricea / hoursa;
float internal2 = priceb / hoursb;
// Chosen output is A OR if they are even we will make A anyway
if (internal1 >= internal2){
// Work out how much of A we can make
int internaloutputa = totaltime/hoursa;
int internaloutputb;
// can we make any of B with the remainer?
float internal3 = totaltime;
internal3 = internal3 - (internaloutputa * hoursa);
if (internal3 >= hoursb){
internaloutputb = internal3 /hoursb;
};
// We now have our first set of solutions, which we must test for the unit foregone problem
// first remove the marginal output from good a
int altinternaloutputa = internaloutputa - 1;
int altinternaloutputb;
// now we look at how many more of b we can make
float internal4 = totaltime;
internal4 = internal4 - (altinternaloutputa * hoursa);
if (internal4 >= hoursb){
altinternaloutputb = internal4 / hoursb;
};
// Now we compare the values to work out which solution we prefer
float value1 = internaloutputa*price1 + internaloutputb * price2;
float value2 = altinternaloutputa * price1 + altinternaloutputb * price2;
// former output is more profitable
if (value1 >= value2){
r1 = r1 + internaloutputa;
r2 = r2+ internaloutputb;
cout << endl << "The factory produces " << internaloutputa << " of good A" << endl;
cout << "The factory produces " << internaloutputb << " of good B" << endl;
}
if (value1 < value2){
r1 = r1 + altinternaloutputa;
r2 = r2 + altinternaloutputb;
cout << endl << "The factory produces " << altinternaloutputa << " of good A" << endl;
cout << "The factory produces " << altinternaloutputb << " of good B" << endl;
}
};
// Chosen output is B
if (internal1 < internal2){
// Work out how much of B we can make
int internaloutputb = totaltime/hoursb;
int internaloutputa;
// can we make any of B with the remainer?
float internal3 = totaltime;
internal3 = internal3 - (internaloutputb * hoursb);
if (internal3 >= hoursa){
internaloutputa = internal3 /hoursa;
};
// We now have our first set of solutions, which we must test for the unit foregone problem
// first remove the marginal output from good a
int altinternaloutputb = internaloutputa - 1;
int altinternaloutputa;
// now we look at how many more of b we can make
float internal4 = totaltime;
internal4 = internal4 - (altinternaloutputb * hoursb);
if (internal4 >= hoursa){
altinternaloutputa = internal4 /hoursa;
};
// Now we compare the values to work out which solution we prefer
float value1 = internaloutputa*price1 + internaloutputb * price2;
float value2 = altinternaloutputa * price1 + altinternaloutputb * price2;
// former output is more profitable
if (value1 >= value2){
r1 = r1 + internaloutputa;
r2 = r2 + internaloutputb;
cout << endl << "The factory produces " << internaloutputa << " of good A" << endl;
cout << "The factory produces " << internaloutputb << " of good B" << endl;
}
if (value1 < value2){
r1 = r1 + altinternaloutputa;
r2 = r2 + altinternaloutputb;
cout << endl << "The factory produces " << altinternaloutputa << " of good A" << endl;
cout << "The factory produces " << altinternaloutputb << " of good B" << endl;
}
};
};
};