Search code examples
javaoptimizationlinear-programmingmathematical-expressionssimplex-algorithm

Optimization: Farming wheat and rice


This is the problem statement

An Indian farmer has a piece of farmland, say L square kilometers long, and wants to either sow wheat or rice or a combination of both. The farmer has a limited amount of F kg of fertilizer and P kg of insecticides.

Every square kilometre of wheat requires F1 Kg of fertilizer and P1 Kg of insecticide. Every square kilometre of rice farming requires F2 Kg of fertilizer and P2 Kg of insecticide. Let S1 be the price obtained by selling wheat harvested from a square kilometre and S2 be the price obtained by selling rice harvested from a square kilometre.

You have to find the maximum total profit that the farmer can earn by selecting the area in which wheat and/or rice should be farmed.

For example:

L = 10 Km2 , F = 10 Kg, P = 5 Kg, F1 = 2 Kg, P1 = 2 Kg, F2 = 3 Kg, P2 = 1 Kg, S1 = 14 , S2 = 25.

In this case, the farmer will earn a maximum profit if he sows only Rice on 3.33 Km2 area and maximum profit value will be 83.33.

Input Format

The only of input will consist of 9 integers space-separated, L, F, P, F1, P1, F2, P2, S1, S2

Constraints

1 <= L <= 10^4
1 <= F <= 10^4
1 <= P <= 10^4
F1 + F2< = F
P1 + P2 <= P
1 <= S1 <= 10^4
1 <= S2 <= 10^4

Output Format

Output will be

  • Maximum profit correct up-to 2 digits
  • value in km 2 where wheat should be planted correct up-to 2 digits
  • value in km 2 where rice should be planted correct up-to 2 digits.

For the example considered in the question output will be 83.33 0.00 3.33 .

Sample Test Case

Input

10 10 5 2 2 3 1 14 25

Output

83.33 0.00 3.33

Explanation

let us say that L = 10 Km2 , F = 10 Kg, P = 5 Kg, F1 = 2 Kg, P1 = 2 Kg, F2 = 3 Kg, P2 = 1 Kg, S1 = 14 , S2 = 25 . Total profit will be maximum if farmer does not plant any wheat but plants rice on 3.33 km2 area and maximum profit value is 83.33 .

enter image description here

I require a solution to this problem. However, unable to grasp the statement itself. Please help me.


Solution

  • import java.text.DecimalFormat;
    import java.util.Scanner;
    
    public class Test {
    
        public static void main(String args[]) {
            // String input = "10,10,5,2,2,3,1,14,25";
            System.out.println(get_total_profit());
        }
    
        public static String get_total_profit() {
            // String[] inputs = input1.split(",");
            // Piece of farm land in square kilometer
            Scanner in = new Scanner(System.in);
            float L = in.nextInt(); // Float.valueOf(inputs[0]);
            // Fertilizer in kg
            float F = in.nextInt();// Float.valueOf(inputs[1]);
            // Insecticide in kg
            float P = in.nextInt();// Float.valueOf(inputs[2]);
            // Fertilizer required in kg for square kilometer of Wheat
            float F1 = in.nextInt();// Float.valueOf(inputs[3]);
            // Insecticide required in kg for square kilometer of Wheat
            float P1 = in.nextInt();// Float.valueOf(inputs[4]);
            // Fertilizer required in kg for square kilometer of Rice
            float F2 = in.nextInt();// Float.valueOf(inputs[5]);
            // Insecticide required in kg for square kilometer of Rice
            float P2 = in.nextInt();// Float.valueOf(inputs[6]);
            // Selling price of wheat per square kilometer
            float S1 = in.nextInt();// Float.valueOf(inputs[7]);
            // Selling price of rice per square kilometer
            float S2 = in.nextInt();// Float.valueOf(inputs[8]);
    
            // Result Variables
            float totalRiceInsecUsed = 0f;
            float totalRiceFertUsed = 0f;
            float totalWheatInsecUsed = 0f;
            float totalWheatFertUsed = 0f;
            float areaOfWheat = 0.00f;
            float areaOfRice = 0.00f;
            float amount = 0.00f;
    
            while (true) {
                if ((L == areaOfRice + areaOfWheat) || P == totalRiceInsecUsed + totalWheatInsecUsed
                        || F == totalRiceFertUsed + totalWheatFertUsed || F2 == 0 || F1 == 0 || P2 == 0 || P1 == 0) {
                    break;
                }
    
                float calRiceProfit = Math.min(F / F2, P / P2) * S2;
                float calWheatProfit = Math.min(F / F1, P / P1) * S1;
    
                if (calRiceProfit > calWheatProfit) {
                    float areaInsecUsed = P / P2;
                    float areaFertUsed = F / F2;
                    if (areaInsecUsed > areaFertUsed) {
                        L = L - areaFertUsed;
                        F2 = 0;
                        totalRiceFertUsed = totalRiceFertUsed + F2;
                        areaOfRice = areaOfRice + areaFertUsed;
                        amount = amount + areaFertUsed * S2;
                    } else if (areaInsecUsed < areaFertUsed) {
                        L = L - areaInsecUsed;
                        P2 = 0;
                        totalRiceInsecUsed = totalRiceInsecUsed + areaInsecUsed;
                        areaOfRice = areaOfRice + areaInsecUsed;
                        amount = amount + areaInsecUsed * S2;
                    }
                } else {
                    float areaInsecUsed = P / P1;
                    float areaFertUsed = F / F1;
                    if (areaInsecUsed > areaFertUsed) {
                        L = L - areaFertUsed;
                        F1 = 0;
                        totalWheatFertUsed = totalWheatFertUsed + F1;
                        areaOfWheat = areaOfWheat + areaFertUsed;
                        amount = amount + areaFertUsed * S1;
                    } else if (areaInsecUsed < areaFertUsed) {
                        L = L - areaInsecUsed;
                        P1 = 0;
                        totalWheatInsecUsed = totalWheatInsecUsed + areaInsecUsed;
                        areaOfWheat = areaOfWheat + areaInsecUsed;
                        amount = amount + areaInsecUsed * S1;
                    }
                }
    
            }
            DecimalFormat df = new DecimalFormat();
            df.setMaximumFractionDigits(2);
            df.setMinimumFractionDigits(2);
            return df.format(amount) + "," + df.format(areaOfWheat) + "," + df.format(areaOfRice);
        }
    
    }