GreenWeave specializes in transforming raw materials such as organic cotton, jute, and linen into eco-friendly textiles: organic cotton fabric, jute fabric, and linen fabric. Organic cotton fabric can be manufactured by processing either organic cotton or jute. Linen fabric can be produced from any of the raw materials—organic cotton, jute, or linen. Jute fabric, on the other hand, can be created by processing jute or linen. The production process for organic cotton fabric reduces the input fiber by 30%, leaving 70% for the final product. For linen fabric, the process removes 20% of the input fiber. For jute fabric, the process removes 25% of the input fiber. The costs of purchasing and processing each raw material, along with their availability, are detailed as follows:

| Raw Material      | Purchasing Cost ($/ton) | Processing Cost ($/ton)  | Availability (tons) |
|-------------------|-------------------------|--------------------------|---------------------|
| Organic Cotton    | 350                     | 100                      | 1000                |
| Jute              | 400                     | 80                       | 800                 |
| Linen             | 300                     | 150                      | 600                 |

To fulfill market requirements, GreenWeave must produce a minimum of 300 tons of organic cotton fabric fiber, 400 tons of jute fabric fiber, and 200 tons of linen fabric fiber. What is the minimum total cost to meet GreenWeave's production needs?

Problem type: LP (Small: 7 variables, 6 constraints, 14 nonzeros)

Mathematical model:
Set:
1. RawMaterials
The set of raw materials, \( R = \{"cotton", "jute", "linen"\} \)
2. Products
The set of products, \( P = \{"cotton_fabric", "jute_fabric", "linen_fabric"\} \)
3. ProducedFrom
The set of products and their corresponding raw materials, \( PF = \{"cotton_fabric": ("cotton", "jute"), "linen_fabric": ("linen", "jute", "cotton"), "jute_fabric": ("jute", "linen")\} \)

Parameter:
1. ReducedFiberRatio
The ratio of fiber remaining after processing, \( RFR = \{ "cotton_fabric": 0.7, "jute_fabric": 0.75, "linen_fabric": 0.8 \} \)
2. PurchasingCost 
The cost of purchasing each raw material, \( PC = \{ "cotton": 350, "jute": 400, "linen": 300 \} \) # ($/ton)
3. ProcessingCost
The cost of processing each raw material, \( PrC = \{ "cotton": 100, "jute": 80, "linen": 150 \} \) # ($/ton)
4. Availability
The availability of each raw material, \( A = \{ "cotton": 1000, "jute": 800, "linen": 600 \} \) # (tons)
5. Demand
The demand for each product, \( D = \{ "cotton_fabric": 300, "jute_fabric": 400, "linen_fabric": 200 \} \) # (tons)

Decision variable:
1. X
Continuous variable, \( X_{r,p} \forall r \in PF[p], p \in P \), where \( X_{r,p} \) is the amount of raw material \( r \) used to produce product \( p \).

Objective:
1. Minimize the total cost of purchasing and processing raw materials.
min: \sum_{p \in P} \sum_{r \in PF[p]} (PC[r] + PrC[r]) * X_{r,p}

Constraint:
1. Demand constraint.
\sum_{r \in PF[p]} RFR[p] * X_{r,p} \geq D[p] \forall p \in P
2. Availability constraint.
\sum_{p \in P, r \in PF[p]} X_{r,p} \leq A[r] \forall r \in R

Type:
Continuous, Linear, Linear
LP

Gurobi implementation:
import gurobipy as gp
from gurobipy import GRB


def solve_textile_manufacturing(
    ProducedFrom={
        "cotton_fabric": ["cotton", "jute"],
        "linen_fabric": ["linen", "jute", "cotton"],
        "jute_fabric": ["jute", "linen"]
    },
    ReducedFiberRatio={
        "cotton_fabric": 0.7,
        "jute_fabric": 0.75,
        "linen_fabric": 0.8
    },
    PurchasingCost={
        "cotton": 350,
        "jute": 400,
        "linen": 300
    },
    ProcessingCost={
        "cotton": 100,
        "jute": 80,
        "linen": 150
    },
    Availability={
        "cotton": 1000,
        "jute": 800,
        "linen": 600
    },
    Demand={
        "cotton_fabric": 300,
        "jute_fabric": 400,
        "linen_fabric": 200
    }):
    """
    Solves the textile manufacturing optimization problem.
    """
    # Create a new model
    model = gp.Model("Textile Manufacturing Optimization")

    # Sets
    RawMaterials = list(PurchasingCost.keys())
    Products = list(Demand.keys())

    # Decision variables
    X = model.addVars(RawMaterials, Products, vtype=GRB.CONTINUOUS, name="X")

    # Objective: Minimize total cost
    obj = gp.quicksum(
        (PurchasingCost[r] + ProcessingCost[r]) * X[r, p] for p in Products
        for r in RawMaterials if r in ProducedFrom.get(p, []))
    model.setObjective(obj, GRB.MINIMIZE)

    # Constraint 1: Demand constraint
    for p in Products:
        model.addConstr(
            gp.quicksum(ReducedFiberRatio[p] * X[r, p]
                        for r in ProducedFrom[p]) >= Demand[p], f"Demand_{p}")

    # Constraint 2: Availability constraint
    for r in RawMaterials:
        model.addConstr(
            gp.quicksum(X[r, p] for p in Products
                        if r in ProducedFrom.get(p, [])) <= Availability[r],
            f"Availability_{r}")

    # Optimize the model
    model.optimize()

    # Return results
    if model.status == GRB.OPTIMAL:
        return {"status": "optimal", "obj": model.ObjVal}
    else:
        return {"status": f"{model.status}"}


if __name__ == "__main__":
    result = solve_textile_manufacturing()
    print(result)

Solution status: OPTIMAL
Optimal objective value: 545357.142857143