{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "provenance": [],
      "gpuType": "T4"
    },
    "kernelspec": {
      "name": "ir",
      "display_name": "R"
    },
    "language_info": {
      "name": "R"
    },
    "accelerator": "GPU"
  },
  "cells": [
    {
      "cell_type": "code",
      "execution_count": 1,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "aEWm3-obvxbV",
        "outputId": "aec96e82-1a86-4a0b-83ab-b74f483749b6"
      },
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "First 10 rows of production_data:\n",
            "   Sol Station Team Output\n",
            "1    1       1    1     52\n",
            "2    1       1    2     54\n",
            "3    1       1    3     63\n",
            "4    1       1    4     57\n",
            "5    1       2    1     58\n",
            "6    1       2    2     65\n",
            "7    1       2    3     61\n",
            "8    1       2    4     62\n",
            "9    1       3    1     61\n",
            "10   1       3    2     66\n",
            "\n",
            "Total Production per Station:\n",
            "  Station TotalOutput\n",
            "1       1        7001\n",
            "2       2        7355\n",
            "3       3        7723\n",
            "4       4        8049\n",
            "5       5        8419\n",
            "6       6        8800\n",
            "\n",
            "First 10 TRUE anomalies:\n",
            "  Sol Station Team Output Anomaly\n",
            "3   1       1    3     63    TRUE\n",
            "   Sol Station Team Output Anomaly\n",
            "28   2       1    4     66    TRUE\n",
            "   Sol Station Team Output Anomaly\n",
            "73   4       1    1     53    TRUE\n",
            "    Sol Station Team Output Anomaly\n",
            "121   6       1    1     52    TRUE\n",
            "    Sol Station Team Output Anomaly\n",
            "148   7       1    4     65    TRUE\n",
            "    Sol Station Team Output Anomaly\n",
            "165   7       6    1     65    TRUE\n",
            "    Sol Station Team Output Anomaly\n",
            "232  10       4    4     74    TRUE\n",
            "    Sol Station Team Output Anomaly\n",
            "233  10       5    1     62    TRUE\n",
            "    Sol Station Team Output Anomaly\n",
            "236  10       5    4     76    TRUE\n",
            "    Sol Station Team Output Anomaly\n",
            "241  11       1    1     51    TRUE\n",
            "\n",
            "Peak Productivity Days:\n",
            "  Station Sol TotalOutput\n",
            "1       1  16         244\n",
            "2       2   7         255\n",
            "3       3   1         268\n",
            "4       4  20         283\n",
            "5       5  27         294\n",
            "6       6   1         304\n",
            "\n",
            "First 10 rows with Efficiency Score:\n",
            "   Sol Station Team Output EfficiencyScore\n",
            "1    1       1    1     52        26.00000\n",
            "2    1       1    2     54        18.00000\n",
            "3    1       1    3     63        15.75000\n",
            "4    1       1    4     57        11.40000\n",
            "5    1       2    1     58        19.33333\n",
            "6    1       2    2     65        16.25000\n",
            "7    1       2    3     61        12.20000\n",
            "8    1       2    4     62        10.33333\n",
            "9    1       3    1     61        15.25000\n",
            "10   1       3    2     66        13.20000\n"
          ]
        }
      ],
      "source": [
        "############################################################\n",
        "# Name: [PARTH KAMLESHKUMAR SUTHAR]\n",
        "# Student ID: [2434584]\n",
        "# Module: ACCA7022 – Advanced Statistical Programming\n",
        "# Assignment: Coursework 1 – Data Manipulation\n",
        "# Date: February 2026\n",
        "# Purpose: Alien Colony Resource Management Simulation\n",
        "############################################################\n",
        "\n",
        "\n",
        "###############################\n",
        "# SET CONSTANTS\n",
        "###############################\n",
        "\n",
        "BaseOutput <- 50\n",
        "set.seed(123)   # Ensures reproducible results\n",
        "\n",
        "\n",
        "############################################################\n",
        "# PART A – GENERATE PRODUCTION DATA (PREALLOCATED)\n",
        "############################################################\n",
        "\n",
        "total_rows <- 30 * 6 * 4   # 720 rows\n",
        "\n",
        "Sol_vec <- numeric(total_rows)\n",
        "Station_vec <- numeric(total_rows)\n",
        "Team_vec <- numeric(total_rows)\n",
        "Output_vec <- numeric(total_rows)\n",
        "\n",
        "index <- 1\n",
        "\n",
        "for (sol in 1:30) {\n",
        "  for (station in 1:6) {\n",
        "    for (team in 1:4) {\n",
        "\n",
        "      RandomNoise <- sample(-5:5, 1)\n",
        "\n",
        "      Output <- BaseOutput + (station * 3) + (team * 2) + RandomNoise\n",
        "\n",
        "      Sol_vec[index] <- sol\n",
        "      Station_vec[index] <- station\n",
        "      Team_vec[index] <- team\n",
        "      Output_vec[index] <- Output\n",
        "\n",
        "      index <- index + 1\n",
        "    }\n",
        "  }\n",
        "}\n",
        "\n",
        "production_data <- data.frame(\n",
        "  Sol = Sol_vec,\n",
        "  Station = Station_vec,\n",
        "  Team = Team_vec,\n",
        "  Output = Output_vec\n",
        ")\n",
        "\n",
        "cat(\"First 10 rows of production_data:\\n\")\n",
        "print(head(production_data, 10))\n",
        "\n",
        "\n",
        "############################################################\n",
        "# PART B – TOTAL PRODUCTION PER STATION\n",
        "############################################################\n",
        "\n",
        "Station_total_vec <- numeric(6)\n",
        "Total_output_vec <- numeric(6)\n",
        "\n",
        "for (station in 1:6) {\n",
        "\n",
        "  total <- 0\n",
        "\n",
        "  for (i in 1:nrow(production_data)) {\n",
        "    if (production_data$Station[i] == station) {\n",
        "      total <- total + production_data$Output[i]\n",
        "    }\n",
        "  }\n",
        "\n",
        "  Station_total_vec[station] <- station\n",
        "  Total_output_vec[station] <- total\n",
        "}\n",
        "\n",
        "station_totals <- data.frame(\n",
        "  Station = Station_total_vec,\n",
        "  TotalOutput = Total_output_vec\n",
        ")\n",
        "\n",
        "cat(\"\\nTotal Production per Station:\\n\")\n",
        "print(station_totals)\n",
        "\n",
        "\n",
        "############################################################\n",
        "# PART C – DETECT PRODUCTION ANOMALIES\n",
        "############################################################\n",
        "\n",
        "Anomaly_Sol <- numeric(total_rows)\n",
        "Anomaly_Station <- numeric(total_rows)\n",
        "Anomaly_Team <- numeric(total_rows)\n",
        "Anomaly_Output <- numeric(total_rows)\n",
        "Anomaly_Flag <- logical(total_rows)\n",
        "\n",
        "index <- 1\n",
        "\n",
        "for (sol in 1:30) {\n",
        "  for (station in 1:6) {\n",
        "\n",
        "    daily_total <- 0\n",
        "\n",
        "    for (i in 1:nrow(production_data)) {\n",
        "      if (production_data$Sol[i] == sol &&\n",
        "          production_data$Station[i] == station) {\n",
        "        daily_total <- daily_total + production_data$Output[i]\n",
        "      }\n",
        "    }\n",
        "\n",
        "    daily_average <- daily_total / 4\n",
        "\n",
        "    for (i in 1:nrow(production_data)) {\n",
        "      if (production_data$Sol[i] == sol &&\n",
        "          production_data$Station[i] == station) {\n",
        "\n",
        "        deviation <- abs(production_data$Output[i] - daily_average)\n",
        "\n",
        "        if (deviation > (0.10 * daily_average)) {\n",
        "          flag <- TRUE\n",
        "        } else {\n",
        "          flag <- FALSE\n",
        "        }\n",
        "\n",
        "        Anomaly_Sol[index] <- sol\n",
        "        Anomaly_Station[index] <- station\n",
        "        Anomaly_Team[index] <- production_data$Team[i]\n",
        "        Anomaly_Output[index] <- production_data$Output[i]\n",
        "        Anomaly_Flag[index] <- flag\n",
        "\n",
        "        index <- index + 1\n",
        "      }\n",
        "    }\n",
        "  }\n",
        "}\n",
        "\n",
        "anomalies <- data.frame(\n",
        "  Sol = Anomaly_Sol,\n",
        "  Station = Anomaly_Station,\n",
        "  Team = Anomaly_Team,\n",
        "  Output = Anomaly_Output,\n",
        "  Anomaly = Anomaly_Flag\n",
        ")\n",
        "\n",
        "cat(\"\\nFirst 10 TRUE anomalies:\\n\")\n",
        "\n",
        "count <- 0\n",
        "for (i in 1:nrow(anomalies)) {\n",
        "  if (anomalies$Anomaly[i] == TRUE) {\n",
        "    print(anomalies[i, ])\n",
        "    count <- count + 1\n",
        "  }\n",
        "  if (count == 10) break\n",
        "}\n",
        "\n",
        "\n",
        "############################################################\n",
        "# PART D – IDENTIFY PEAK PRODUCTIVITY DAYS\n",
        "############################################################\n",
        "\n",
        "Peak_Station <- numeric(6)\n",
        "Peak_Sol <- numeric(6)\n",
        "Peak_Output <- numeric(6)\n",
        "\n",
        "for (station in 1:6) {\n",
        "\n",
        "  max_output <- -Inf\n",
        "  best_sol <- 0\n",
        "\n",
        "  for (sol in 1:30) {\n",
        "\n",
        "    sol_total <- 0\n",
        "\n",
        "    for (i in 1:nrow(production_data)) {\n",
        "      if (production_data$Station[i] == station &&\n",
        "          production_data$Sol[i] == sol) {\n",
        "        sol_total <- sol_total + production_data$Output[i]\n",
        "      }\n",
        "    }\n",
        "\n",
        "    if (sol_total > max_output) {\n",
        "      max_output <- sol_total\n",
        "      best_sol <- sol\n",
        "    }\n",
        "  }\n",
        "\n",
        "  Peak_Station[station] <- station\n",
        "  Peak_Sol[station] <- best_sol\n",
        "  Peak_Output[station] <- max_output\n",
        "}\n",
        "\n",
        "peak_days <- data.frame(\n",
        "  Station = Peak_Station,\n",
        "  Sol = Peak_Sol,\n",
        "  TotalOutput = Peak_Output\n",
        ")\n",
        "\n",
        "cat(\"\\nPeak Productivity Days:\\n\")\n",
        "print(peak_days)\n",
        "\n",
        "\n",
        "############################################################\n",
        "# PART E – CALCULATE EFFICIENCY SCORE\n",
        "############################################################\n",
        "\n",
        "production_data$EfficiencyScore <- numeric(nrow(production_data))\n",
        "\n",
        "for (i in 1:nrow(production_data)) {\n",
        "  production_data$EfficiencyScore[i] <-\n",
        "    production_data$Output[i] /\n",
        "    (production_data$Station[i] + production_data$Team[i])\n",
        "}\n",
        "\n",
        "cat(\"\\nFirst 10 rows with Efficiency Score:\\n\")\n",
        "print(head(production_data, 10))\n",
        "\n",
        "\n",
        "############################################################\n",
        "# END OF SCRIPT\n",
        "############################################################"
      ]
    }
  ]
}