# ============================================================================
# ALIEN COLONY RESOURCE MANAGEMENT SIMULATION
# ============================================================================
# Author: Jure Jurcevic
# Date: February 10, 2026
# Description: Simulating 30 sols of production data for 6 stations with 4 teams
# ============================================================================
# Clear workspace
rm(list = ls())
# Set seed for reproducibility
set.seed(123)
# ============================================================================
# PART A: GENERATE PRODUCTION DATA
# ============================================================================
# Define constants
num_sols <- 30        # Number of days (sols)
num_stations <- 6     # Number of stations
num_teams <- 4        # Number of teams per station
base_output <- 50     # Base production output
# Calculate total number of rows needed
total_rows <- num_sols * num_stations * num_teams  # 30 * 6 * 4 = 720 rows
# Create empty dataframe with correct structure
production_data <- data.frame(
Sol = integer(total_rows),
Station = integer(total_rows),
Team = integer(total_rows),
Output = numeric(total_rows)
)
# Initialize row counter
row_counter <- 1
# Nested loops to populate the dataframe
# Outer loop: iterate through each sol (day)
for (sol in 1:num_sols) {
# Middle loop: iterate through each station
for (station in 1:num_stations) {
# Inner loop: iterate through each team
for (team in 1:num_teams) {
# Generate random noise between -5 and +5
random_noise <- sample(-5:5, 1)
# Calculate output using the formula:
# Output = BaseOutput + (StationID × TeamID) + RandomNoise
output <- base_output + (station * team) + random_noise
# Store values in the dataframe
production_data$Sol[row_counter] <- sol
production_data$Station[row_counter] <- station
production_data$Team[row_counter] <- team
production_data$Output[row_counter] <- output
# Increment row counter
row_counter <- row_counter + 1
}
}
}
# Display the first 10 rows
cat("\n========== PART A: First 10 Rows of Production Data ==========\n")
print(head(production_data, 10))
# Display total number of rows
cat("\nTotal rows in production_data:", nrow(production_data), "\n")
View(production_data)
View(production_data)
View(production_data)
# ============================================================================
# PART B: TOTAL PRODUCTION PER STATION
# ============================================================================
# Create empty dataframe for station totals
station_totals <- data.frame(
Station = integer(num_stations),
TotalOutput = numeric(num_stations)
)
# Loop through each station
for (station in 1:num_stations) {
# Initialize total output for this station
total_output <- 0
# Loop through all rows in production_data
for (row in 1:nrow(production_data)) {
# If this row belongs to the current station, add its output
if (production_data$Station[row] == station) {
total_output <- total_output + production_data$Output[row]
}
}
# Store the station and its total output
station_totals$Station[station] <- station
station_totals$TotalOutput[station] <- total_output
}
# Display station totals
cat("\n========== PART B: Total Production per Station ==========\n")
print(station_totals)
View(station_totals)
# ============================================================================
# PART C: DETECT PRODUCTION ANOMALIES
# ============================================================================
# Create empty dataframe for anomalies (same size as production_data)
anomalies <- data.frame(
Sol = integer(total_rows),
Station = integer(total_rows),
Team = integer(total_rows),
Output = numeric(total_rows),
Anomaly = logical(total_rows)
)
# Initialize row counter for anomalies
anomaly_row <- 1
# Loop through each sol
for (sol in 1:num_sols) {
# Loop through each station
for (station in 1:num_stations) {
# Calculate daily average for this station on this sol
# First, sum all outputs for this station on this sol
daily_sum <- 0
team_count <- 0
for (row in 1:nrow(production_data)) {
if (production_data$Sol[row] == sol && production_data$Station[row] == station) {
daily_sum <- daily_sum + production_data$Output[row]
team_count <- team_count + 1
}
}
# Calculate average (should be sum / 4 teams)
daily_avg <- daily_sum / team_count
# Now check each team's output against this average
for (team in 1:num_teams) {
# Find the output for this specific sol, station, and team
team_output <- 0
for (row in 1:nrow(production_data)) {
if (production_data$Sol[row] == sol &&
production_data$Station[row] == station &&
production_data$Team[row] == team) {
team_output <- production_data$Output[row]
break  # Found it, exit loop
}
}
# Calculate deviation from average (as percentage)
deviation_percent <- abs((team_output - daily_avg) / daily_avg) * 100
# Mark as anomaly if deviation > 10%
is_anomaly <- deviation_percent > 10
# Store in anomalies dataframe
anomalies$Sol[anomaly_row] <- sol
anomalies$Station[anomaly_row] <- station
anomalies$Team[anomaly_row] <- team
anomalies$Output[anomaly_row] <- team_output
anomalies$Anomaly[anomaly_row] <- is_anomaly
anomaly_row <- anomaly_row + 1
}
}
}
# Display first 10 anomalies detected (where Anomaly = TRUE)
cat("\n========== PART C: First 10 Detected Anomalies ==========\n")
# Find rows where Anomaly is TRUE
anomaly_counter <- 0
for (row in 1:nrow(anomalies)) {
if (anomalies$Anomaly[row] == TRUE) {
print(anomalies[row, ])
anomaly_counter <- anomaly_counter + 1
if (anomaly_counter >= 10) {
break
}
}
}
# Count total anomalies
total_anomalies <- 0
for (row in 1:nrow(anomalies)) {
if (anomalies$Anomaly[row] == TRUE) {
total_anomalies <- total_anomalies + 1
}
}
cat("\nTotal anomalies detected:", total_anomalies, "\n")
View(anomalies)
# ============================================================================
# PART D: IDENTIFY PEAK PRODUCTIVITY DAYS
# ============================================================================
# Create empty dataframe for peak days
peak_days <- data.frame(
Station = integer(num_stations),
Sol = integer(num_stations),
TotalOutput = numeric(num_stations)
)
# Loop through each station
for (station in 1:num_stations) {
# Initialize variables to track maximum
max_sol <- 0
max_output <- 0
# Loop through each sol
for (sol in 1:num_sols) {
# Calculate total output for this station on this sol
sol_total <- 0
for (row in 1:nrow(production_data)) {
if (production_data$Sol[row] == sol && production_data$Station[row] == station) {
sol_total <- sol_total + production_data$Output[row]
}
}
# Check if this is the maximum so far
if (sol_total > max_output) {
max_output <- sol_total
max_sol <- sol
}
}
# Store the peak day information
peak_days$Station[station] <- station
peak_days$Sol[station] <- max_sol
peak_days$TotalOutput[station] <- max_output
}
# Display peak days
cat("\n========== PART D: Peak Productivity Days ==========\n")
print(peak_days)
View(production_data)
View(peak_days)
# ============================================================================
# PART E: CALCULATE EFFICIENCY SCORE
# ============================================================================
# Add new column for Efficiency Score
production_data$EfficiencyScore <- numeric(nrow(production_data))
# Loop through each row and calculate efficiency score
for (row in 1:nrow(production_data)) {
# Formula: EfficiencyScore = (Output / BaseOutput) × 100
production_data$EfficiencyScore[row] <- (production_data$Output[row] / base_output) * 100
}
# Display first 10 rows with the new column
cat("\n========== PART E: First 10 Rows with Efficiency Score ==========\n")
print(head(production_data, 10))
# Display summary statistics
cat("\n========== Efficiency Score Summary ==========\n")
cat("Minimum Efficiency:", min(production_data$EfficiencyScore), "%\n")
cat("Maximum Efficiency:", max(production_data$EfficiencyScore), "%\n")
# Calculate average efficiency manually using loop
total_efficiency <- 0
for (row in 1:nrow(production_data)) {
total_efficiency <- total_efficiency + production_data$EfficiencyScore[row]
}
avg_efficiency <- total_efficiency / nrow(production_data)
cat("Average Efficiency:", avg_efficiency, "%\n")
View(production_data)
View(anomalies)
View(peak_days)
View(production_data)
View(station_totals)
View(production_data)
# ============================================================================
# FINAL SUMMARY AND VERIFICATION
# ============================================================================
cat("\n========== SIMULATION COMPLETE ==========\n")
cat("Total records generated:", nrow(production_data), "\n")
cat("Stations analyzed:", num_stations, "\n")
cat("Sols simulated:", num_sols, "\n")
cat("Teams per station:", num_teams, "\n")
cat("\nAll tasks completed successfully!\n")
cat("=========================================\n")
