|
| 1 | +""" |
| 2 | +MCDA Custom Analysis Request Schemas |
| 3 | +
|
| 4 | +Defines input schemas for fully customized MCDA analysis: |
| 5 | +- User-defined goals with weights and directions |
| 6 | +- User-defined alternatives with scores per goal |
| 7 | +""" |
| 8 | +from pydantic import BaseModel, Field, model_validator |
| 9 | +from typing import Dict, List, Literal, Optional, Set |
| 10 | + |
| 11 | + |
| 12 | +class CustomGoalInput(BaseModel): |
| 13 | + """ |
| 14 | + Represents a user-defined goal in a custom MCDA analysis. |
| 15 | + """ |
| 16 | + name: str = Field(..., description="Goal name") |
| 17 | + weight: float = Field(..., gt=0, description="Goal weight (must be > 0)") |
| 18 | + direction: Literal["max", "min"] = Field( |
| 19 | + "max", description="Optimization direction for the goal" |
| 20 | + ) |
| 21 | + |
| 22 | + |
| 23 | +class CustomAlternativeInput(BaseModel): |
| 24 | + """ |
| 25 | + Represents a user-defined alternative with scores per goal. |
| 26 | + """ |
| 27 | + name: str = Field(..., description="Alternative name") |
| 28 | + values: Dict[str, float] = Field( |
| 29 | + ..., description="Scores per goal (goal name -> score)" |
| 30 | + ) |
| 31 | + |
| 32 | + |
| 33 | +class McdaCustomAnalysisParams(BaseModel): |
| 34 | + """ |
| 35 | + Request schema for a fully customized MCDA analysis. |
| 36 | + """ |
| 37 | + name: Optional[str] = Field(None, description="Optional analysis name") |
| 38 | + goals: List[CustomGoalInput] = Field( |
| 39 | + ..., description="List of goals with weights and directions" |
| 40 | + ) |
| 41 | + alternatives: List[CustomAlternativeInput] = Field( |
| 42 | + ..., description="List of alternatives with scores per goal" |
| 43 | + ) |
| 44 | + |
| 45 | + @model_validator(mode="after") |
| 46 | + def validate_structure(self): |
| 47 | + goals = self.goals or [] |
| 48 | + alternatives = self.alternatives or [] |
| 49 | + |
| 50 | + if len(goals) < 2: |
| 51 | + raise ValueError("At least two goals are required for MCDA analysis") |
| 52 | + if len(alternatives) < 2: |
| 53 | + raise ValueError("At least two alternatives are required for MCDA analysis") |
| 54 | + |
| 55 | + goal_names = [goal.name for goal in goals] |
| 56 | + unique_goal_names: Set[str] = set(goal_names) |
| 57 | + if len(unique_goal_names) != len(goal_names): |
| 58 | + raise ValueError("Goal names must be unique") |
| 59 | + |
| 60 | + alt_names = [alt.name for alt in alternatives] |
| 61 | + if len(set(alt_names)) != len(alt_names): |
| 62 | + raise ValueError("Alternative names must be unique") |
| 63 | + |
| 64 | + for alt in alternatives: |
| 65 | + if not alt.values: |
| 66 | + raise ValueError( |
| 67 | + f"Alternative '{alt.name}' must define scores for all goals" |
| 68 | + ) |
| 69 | + missing = unique_goal_names.difference(alt.values.keys()) |
| 70 | + extra = set(alt.values.keys()).difference(unique_goal_names) |
| 71 | + if missing: |
| 72 | + missing_list = ", ".join(sorted(missing)) |
| 73 | + raise ValueError( |
| 74 | + f"Alternative '{alt.name}' is missing scores for goals: {missing_list}" |
| 75 | + ) |
| 76 | + if extra: |
| 77 | + extra_list = ", ".join(sorted(extra)) |
| 78 | + raise ValueError( |
| 79 | + f"Alternative '{alt.name}' has scores for unknown goals: {extra_list}" |
| 80 | + ) |
| 81 | + |
| 82 | + return self |
| 83 | + |
| 84 | + class Config: |
| 85 | + json_schema_extra = { |
| 86 | + "example": { |
| 87 | + "name": "Custom MCDA Run", |
| 88 | + "goals": [ |
| 89 | + {"name": "Environmental Impact", "weight": 0.35, "direction": "max"}, |
| 90 | + {"name": "Economic Cost", "weight": 0.25, "direction": "min"}, |
| 91 | + {"name": "Social Acceptance", "weight": 0.40, "direction": "max"} |
| 92 | + ], |
| 93 | + "alternatives": [ |
| 94 | + { |
| 95 | + "name": "Project A", |
| 96 | + "values": { |
| 97 | + "Environmental Impact": 0.82, |
| 98 | + "Economic Cost": 1200.0, |
| 99 | + "Social Acceptance": 0.67 |
| 100 | + } |
| 101 | + }, |
| 102 | + { |
| 103 | + "name": "Project B", |
| 104 | + "values": { |
| 105 | + "Environmental Impact": 0.75, |
| 106 | + "Economic Cost": 980.0, |
| 107 | + "Social Acceptance": 0.74 |
| 108 | + } |
| 109 | + } |
| 110 | + ] |
| 111 | + } |
| 112 | + } |
0 commit comments