library(ram)
#> Warning: replacing previous import 'DT::dataTableOutput' by
#> 'shiny::dataTableOutput' when loading 'ram'
#> Warning: replacing previous import 'DT::renderDataTable' by
#> 'shiny::renderDataTable' when loading 'ram'

Problem Statement

A Mediterranean farm plans to allocate land to 4 winter crops: - Gramineae: Oat, Barley - Legumes: Fava bean, Lupin

Scenario:

  • Total land available: 15 ha
  • Total labor hours: 350 hours
  • Total nitrogen: 500 kg
  • Oat: 2 ≤ area ≤ 8 ha
  • Barley: 1 ≤ area ≤ 6 ha
  • Fava bean: 3 ≤ area ≤ 7 ha
  • Lupin: 2 ≤ area ≤ 5 ha

Objective: Maximize total gross margin (profit)

Crop N fertilizer need (kg N/haN/ha) Work labor (h/hah/ha) Gross margin (/ha€/ha)
Oat 80 20 400
Barley 100 25 450
Lupin 0 15 350
Fava bean 30 30 500
  • Objective: Maximize total gross margin.

Step 1: Define Resource Constraints (flexible minfor each crop)

# Step 1: Define Resource Constraints (flexible min/max for each crop)
resources <- data.frame(
  resource     = c("land", "labor","nitrogen"),
  availability = c(15, 350, 500),
  direction    = c("<=", "<=","<=")
)
print(resources)
#>   resource availability direction
#> 1     land           15        <=
#> 2    labor          350        <=
#> 3 nitrogen          500        <=

Step 2: Define Activities (Crop Choices)

# Step 2: Define Activities 
activities <- data.frame(
  activity  = c("oat", "barley",  "lupin", "fava"),
  land      = c(1, 1, 1, 1),
  labor     = c(20, 25, 15, 30),
  nitrogen  = c(80, 100, 0, 30),
  objective = c(400, 450, 350, 500)
)
print(activities)
#>   activity land labor nitrogen objective
#> 1      oat    1    20       80       400
#> 2   barley    1    25      100       450
#> 3    lupin    1    15        0       350
#> 4     fava    1    30       30       500

Step 3: Build and Solve the Model

# Step 3: Build and Solve the Model
model <- create_ram_model(resources, activities)
solution <- solve_ram(model, direction = "max")

Step 4: Results

summary_ram(solution)
cat("Maximum gross margin (EUR):", solution$objective_value, "\n")
#> Maximum gross margin (EUR): 6500
plot_ram(solution)