{"nbformat":4,"nbformat_minor":0,"metadata":{"colab":{"provenance":[],"authorship_tag":"ABX9TyO2JJy7rmiO++OYYUSdoQTP"},"kernelspec":{"name":"ir","display_name":"R"},"language_info":{"name":"R"}},"cells":[{"cell_type":"code","execution_count":14,"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"UkjVv8-MYsFU","executionInfo":{"status":"ok","timestamp":1782467178899,"user_tz":-60,"elapsed":473,"user":{"displayName":"VAMSI BEJJAM","userId":"10176154799955039326"}},"outputId":"85b04aff-626f-493b-a8ca-8550f4849b72"},"outputs":[{"output_type":"stream","name":"stdout","text":["=== Task 1A: First 10 Rows of production_data ===\n","   Day Utility Team Output\n","1    1       1    1     40\n","2    1       1    2     46\n","3    1       1    3     44\n","4    1       1    4     54\n","5    1       1    5     57\n","6    1       2    1     46\n","7    1       2    2     46\n","8    1       2    3     56\n","9    1       2    4     49\n","10   1       2    5     58\n","\n","=== Task 1B: Total Production per Utility ===\n","  Utility TotalOutput\n","1       1        7394\n","2       2        7729\n","3       3        8284\n","4       4        8722\n","5       5        9148\n","6       6        9566\n","\n","=== Task 1C: First 10 Anomalies Detected ===\n","   Day Utility Team Output Anomaly\n","1    1       1    1     40    TRUE\n","2    1       1    2     46    TRUE\n","3    1       1    3     44    TRUE\n","4    1       1    4     54    TRUE\n","6    1       2    1     46    TRUE\n","7    1       2    2     46    TRUE\n","8    1       2    3     56    TRUE\n","9    1       2    4     49    TRUE\n","11   1       3    1     52    TRUE\n","12   1       3    2     51    TRUE\n","\n","=== Task 1D: Peak Productivity Days per Utility ===\n","  Utility Day TotalOutput\n","1       1   2         260\n","2       2  27         271\n","3       3  17         296\n","4       4  24         308\n","5       5   9         317\n","6       6   4         339\n","\n","=== Task 1E: First 10 Rows with EfficiencyScore ===\n","   Day Utility Team Output EfficiencyScore\n","1    1       1    1     40           11.67\n","2    1       1    2     46           12.67\n","3    1       1    3     44           12.33\n","4    1       1    4     54           14.00\n","5    1       1    5     57           14.50\n","6    1       2    1     46           12.67\n","7    1       2    2     46           12.67\n","8    1       2    3     56           14.33\n","9    1       2    4     49           13.17\n","10   1       2    5     58           14.67\n"]}],"source":["# ============================================================\n","# Name:    Vamsi Bejjam\n","# Title:   Smart City Utilities Operations Simulation\n","# Purpose: Simulate, manipulate, and summarise operational\n","#          output data for six utility services across five\n","#          teams over 30 days, using loops only (no high-level\n","#          aggregation functions).\n","# Module:  ACCA7022 Advanced Statistical Programming\n","# ============================================================\n","\n","# Set seed for reproducibility during testing\n","set.seed(42)\n","\n","# ============================================================\n","# TASK 1A: Generate Production Data\n","# ============================================================\n","\n","# Define simulation constants\n","NUM_DAYS      <- 30\n","NUM_UTILITIES <- 6\n","NUM_TEAMS     <- 5\n","BASE_OUTPUT   <- 40\n","\n","# Pre-allocate vectors to hold column data before building data frame\n","total_rows <- NUM_DAYS * NUM_UTILITIES * NUM_TEAMS\n","day_col     <- integer(total_rows)\n","utility_col <- integer(total_rows)\n","team_col    <- integer(total_rows)\n","output_col  <- numeric(total_rows)\n","\n","# Use a single index to track position in pre-allocated vectors\n","row_index <- 1\n","\n","# Nested loops: iterate over every combination of day, utility, and team\n","for (d in 1:NUM_DAYS) {\n","  for (u in 1:NUM_UTILITIES) {\n","    for (t in 1:NUM_TEAMS) {\n","\n","      # Generate random noise: integer between -5 and +5\n","      noise <- sample(-5:5, 1)\n","\n","      # Output formula: BaseOutput + (UtilityID*3 + TeamID*2) + RandomNoise\n","      output_value <- BASE_OUTPUT + ((u*3) + (t*2)) + noise\n","\n","      # Store values in pre-allocated vectors at current row index\n","      day_col[row_index]     <- d\n","      utility_col[row_index] <- u\n","      team_col[row_index]    <- t\n","      output_col[row_index]  <- output_value\n","\n","      row_index <- row_index + 1\n","    }\n","  }\n","}\n","\n","# Assemble the main data frame from the populated vectors\n","production_data <- data.frame(\n","  Day     = day_col,\n","  Utility = utility_col,\n","  Team    = team_col,\n","  Output  = output_col\n",")\n","\n","# Display the first 10 rows\n","cat(\"=== Task 1A: First 10 Rows of production_data ===\\n\")\n","print(production_data[1:10, ])\n","\n","\n","# ============================================================\n","# TASK 1B: Total Production per Utility\n","# ============================================================\n","\n","# Initialise a data frame with one row per utility, TotalOutput starts at 0\n","Utility_totals <- data.frame(\n","  Utility     = 1:NUM_UTILITIES,\n","  TotalOutput = numeric(NUM_UTILITIES)\n",")\n","\n","# Loop through every row in production_data and accumulate output by utility\n","for (i in 1:nrow(production_data)) {\n","  u <- production_data$Utility[i]               # which utility this row belongs to\n","  Utility_totals$TotalOutput[u] <- Utility_totals$TotalOutput[u] + production_data$Output[i]\n","}\n","\n","cat(\"\\n=== Task 1B: Total Production per Utility ===\\n\")\n","print(Utility_totals)\n","\n","\n","# ============================================================\n","# TASK 1C: Detect Production Anomalies\n","# ============================================================\n","\n","# Build anomalies data frame by copying production_data and adding an Anomaly column\n","anomalies <- data.frame(\n","  Day     = production_data$Day,\n","  Utility = production_data$Utility,\n","  Team    = production_data$Team,\n","  Output  = production_data$Output,\n","  Anomaly = logical(nrow(production_data))   # initialised to FALSE\n",")\n","\n","# For each day-utility combination, calculate the average output across teams,\n","# then flag any team whose output deviates by more than 10% from that average.\n","for (d in 1:NUM_DAYS) {\n","  for (u in 1:NUM_UTILITIES) {\n","\n","    # Collect the outputs for the current day and utility\n","    day_utility_outputs <- numeric(0)\n","\n","    for (i in 1:nrow(production_data)) {\n","      if (production_data$Day[i] == d && production_data$Utility[i] == u) {\n","        day_utility_outputs <- c(day_utility_outputs, production_data$Output[i])\n","      }\n","    }\n","\n","    # Calculate daily average: sum divided by number of teams (per spec: divide by 4)\n","    daily_avg <- 0\n","    for (val in day_utility_outputs) {\n","      daily_avg <- daily_avg + val\n","    }\n","    daily_avg <- daily_avg / 4   # as specified in the brief\n","\n","    # Calculate the 10% threshold\n","    threshold <- daily_avg * 0.10\n","\n","    # Mark anomaly for each matching row\n","    for (i in 1:nrow(anomalies)) {\n","      if (anomalies$Day[i] == d && anomalies$Utility[i] == u) {\n","        deviation <- anomalies$Output[i] - daily_avg\n","        if (deviation < 0) deviation <- -deviation    # absolute value without abs()\n","        anomalies$Anomaly[i] <- deviation > threshold\n","      }\n","    }\n","  }\n","}\n","\n","# Display first 10 detected anomalies (rows where Anomaly == TRUE)\n","anomaly_rows <- which(anomalies$Anomaly == TRUE)\n","cat(\"\\n=== Task 1C: First 10 Anomalies Detected ===\\n\")\n","print(anomalies[anomaly_rows[1:10], ])\n","\n","\n","# ============================================================\n","# TASK 1D: Identify Peak Productivity Days\n","# ============================================================\n","\n","# Initialise peak_days: store best day and its total output for each utility\n","peak_days <- data.frame(\n","  Utility     = 1:NUM_UTILITIES,\n","  Day         = integer(NUM_UTILITIES),\n","  TotalOutput = numeric(NUM_UTILITIES)\n",")\n","\n","# For each utility, find the day whose sum of team outputs is highest\n","for (u in 1:NUM_UTILITIES) {\n","\n","  best_day    <- 0\n","  best_output <- -Inf\n","\n","  for (d in 1:NUM_DAYS) {\n","\n","    # Sum outputs for this utility on this day\n","    day_total <- 0\n","    for (i in 1:nrow(production_data)) {\n","      if (production_data$Utility[i] == u && production_data$Day[i] == d) {\n","        day_total <- day_total + production_data$Output[i]\n","      }\n","    }\n","\n","    # Update best day if this total exceeds current best\n","    if (day_total > best_output) {\n","      best_output <- day_total\n","      best_day    <- d\n","    }\n","  }\n","\n","  peak_days$Day[u]         <- best_day\n","  peak_days$TotalOutput[u] <- best_output\n","}\n","\n","cat(\"\\n=== Task 1D: Peak Productivity Days per Utility ===\\n\")\n","print(peak_days)\n","\n","\n","# ============================================================\n","# TASK 1E: Calculate Efficiency Score\n","# ============================================================\n","\n","# Maximum possible output for scaling: BaseOutput + (max_utility*3 + max_team*2) + max_noise\n","MAX_OUTPUT <- BASE_OUTPUT + ((NUM_UTILITIES*3) + (NUM_TEAMS*2)) + 5\n","\n","# Add EfficiencyScore column to production_data (Output /utility + team)\n","production_data$EfficiencyScore <- numeric(nrow(production_data))\n","\n","for (i in 1:nrow(production_data)) {\n","  production_data$EfficiencyScore[i] <- (production_data$Output[i] / u+t)\n","}\n","\n","# Round to 2 decimal places for readability\n","for (i in 1:nrow(production_data)) {\n","  production_data$EfficiencyScore[i] <- round(production_data$EfficiencyScore[i], 2)\n","}\n","\n","cat(\"\\n=== Task 1E: First 10 Rows with EfficiencyScore ===\\n\")\n","print(production_data[1:10, ])\n"]}]}