# MCEG / ELEG 4402 — Engineering concept scoring starter
# Example only. Replace all sample values with project-specific data.
# Base R only (no packages required).

input_file <- if (file.exists("concept_scoring.csv")) {
  "concept_scoring.csv"
} else {
  "materials/software/concept_scoring.csv"
}

if (!file.exists(input_file)) {
  stop("Place concept_scoring.csv in the working directory or run from the project root.")
}

criteria <- read.csv(input_file, check.names = FALSE)
required <- c("Criterion", "Weight", "Concept_A", "Concept_B", "Concept_C")
stopifnot(all(required %in% names(criteria)))
stopifnot(all(is.finite(criteria$Weight)), all(criteria$Weight >= 0))
stopifnot(sum(criteria$Weight) > 0)

weights <- criteria$Weight / sum(criteria$Weight)
concept_columns <- c("Concept_A", "Concept_B", "Concept_C")
score_matrix <- as.matrix(criteria[, concept_columns])
stopifnot(all(is.finite(score_matrix)))

weighted_scores <- as.numeric(crossprod(weights, score_matrix))
results <- data.frame(
  Concept = concept_columns,
  Weighted_Score = round(weighted_scores, 3)
)
results <- results[order(-results$Weighted_Score), , drop = FALSE]
rownames(results) <- NULL

cat("MCEG / ELEG 4402 — EXAMPLE WEIGHTED CONCEPT EVALUATION\n")
cat("Weights normalized to a sum of 1.0\n")
print(results, row.names = FALSE)
cat("\nCAUTION: Ranking depends on chosen criteria and subjective scores.\n")
