To solve this optimization problem, we first need to define the variables and constraints based on the given information. Let's denote:

- \(x\) as the amount invested in the shoe factory,
- \(y\) as the amount invested in the hat factory.

The objective is to maximize earnings from these investments. The earnings from the shoe factory would be \(0.07x\) (7% of \(x\)), and the earnings from the hat factory would be \(0.06y\) (6% of \(y\)). Therefore, the total earnings to be maximized can be represented as \(0.07x + 0.06y\).

The constraints given are:
1. The total amount invested is $20,000: \(x + y = 20000\).
2. The investment in the shoe factory must be at least four times that of the hat factory: \(x \geq 4y\).
3. The maximum investment in the shoe factory can be $15,000: \(x \leq 15000\).

Now, let's translate these into Gurobi code to solve the linear programming problem:

```python
from gurobipy import *

# Create a new model
m = Model("Investment_Optimization")

# Define variables
x = m.addVar(vtype=GRB.CONTINUOUS, name="shoe_factory_investment")
y = m.addVar(vtype=GRB.CONTINUOUS, name="hat_factory_investment")

# Set the objective function: Maximize earnings
m.setObjective(0.07*x + 0.06*y, GRB.MAXIMIZE)

# Define constraints
m.addConstr(x + y == 20000, "total_investment")
m.addConstr(x >= 4*y, "shoe_vs_hat_investment_ratio")
m.addConstr(x <= 15000, "max_shoe_factory_investment")

# Optimize the model
m.optimize()

# Print the results
if m.status == GRB.OPTIMAL:
    print("Optimal solution found:")
    print(f"Shoe factory investment: ${x.x:.2f}")
    print(f"Hat factory investment: ${y.x:.2f}")
    print(f"Total earnings: ${m.objVal:.2f}")
else:
    print("No optimal solution found")
```