1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599
| import numpy as np import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D from scipy.optimize import differential_evolution import warnings warnings.filterwarnings('ignore')
np.random.seed(42)
class MarsExplorationMission: """ Mars Exploration Mission Design Problem Decision variables: - x[0]: Number of cameras (0-5) - x[1]: Number of spectrometers (0-3) - x[2]: Number of drilling instruments (0-2) - x[3]: Mission duration in days (180-720) - x[4]: Solar panel area in m^2 (10-50) - x[5]: Communication bandwidth in Mbps (1-20) """ def __init__(self): self.camera_cost = 15 self.spectro_cost = 25 self.drill_cost = 40 self.camera_mass = 5 self.spectro_mass = 12 self.drill_mass = 20 self.camera_power = 50 self.spectro_power = 80 self.drill_power = 150 self.base_spacecraft_cost = 200 self.daily_operation_cost = 0.5 def calculate_cost(self, x): """Calculate total mission cost in Million USD""" n_cameras = int(x[0]) n_spectros = int(x[1]) n_drills = int(x[2]) duration = x[3] solar_area = x[4] bandwidth = x[5] instrument_cost = (n_cameras * self.camera_cost + n_spectros * self.spectro_cost + n_drills * self.drill_cost) total_mass = (n_cameras * self.camera_mass + n_spectros * self.spectro_mass + n_drills * self.drill_mass) spacecraft_cost = self.base_spacecraft_cost + total_mass * 2 power_cost = solar_area * 3 comm_cost = bandwidth * 5 ops_cost = duration * self.daily_operation_cost total_cost = (instrument_cost + spacecraft_cost + power_cost + comm_cost + ops_cost) return total_cost def calculate_risk(self, x): """Calculate mission risk score (0-100, higher is worse)""" n_cameras = int(x[0]) n_spectros = int(x[1]) n_drills = int(x[2]) duration = x[3] solar_area = x[4] bandwidth = x[5] n_instruments = n_cameras + n_spectros + n_drills complexity_risk = n_instruments * 5 duration_risk = (duration / 720) * 30 required_power = (n_cameras * self.camera_power + n_spectros * self.spectro_power + n_drills * self.drill_power) available_power = solar_area * 100 power_margin = available_power - required_power if power_margin < 0: power_risk = 50 elif power_margin < required_power * 0.2: power_risk = 30 elif power_margin < required_power * 0.5: power_risk = 15 else: power_risk = 5 data_rate = n_instruments * 10 comm_capability = bandwidth * 60 if comm_capability < data_rate: comm_risk = 40 elif comm_capability < data_rate * 1.5: comm_risk = 20 else: comm_risk = 5 total_risk = complexity_risk + duration_risk + power_risk + comm_risk return min(total_risk, 100) def calculate_science_return(self, x): """Calculate scientific return score (0-100, higher is better)""" n_cameras = int(x[0]) n_spectros = int(x[1]) n_drills = int(x[2]) duration = x[3] solar_area = x[4] bandwidth = x[5] camera_science = n_cameras * 10 spectro_science = n_spectros * 20 drill_science = n_drills * 30 if n_cameras > 0 and n_spectros > 0: camera_science *= 1.3 spectro_science *= 1.2 if n_spectros > 0 and n_drills > 0: spectro_science *= 1.3 drill_science *= 1.2 duration_factor = np.log(duration / 180 + 1) / np.log(5) data_generated = (n_cameras + n_spectros + n_drills) * duration * 10 data_transmitted = bandwidth * duration * 60 transmission_efficiency = min(data_transmitted / (data_generated + 1), 1.0) total_science = (camera_science + spectro_science + drill_science) * \ duration_factor * transmission_efficiency return min(total_science, 100) def evaluate(self, x): """Evaluate all three objectives""" cost = self.calculate_cost(x) risk = self.calculate_risk(x) science = self.calculate_science_return(x) return cost, risk, -science
class NSGA2Optimizer: """NSGA-II Multi-objective Genetic Algorithm""" def __init__(self, problem, pop_size=100, n_gen=100): self.problem = problem self.pop_size = pop_size self.n_gen = n_gen self.bounds = [ (0, 5), (0, 3), (0, 2), (180, 720), (10, 50), (1, 20) ] def initialize_population(self): """Create initial random population""" pop = np.random.rand(self.pop_size, 6) for i in range(6): pop[:, i] = pop[:, i] * (self.bounds[i][1] - self.bounds[i][0]) + self.bounds[i][0] return pop def fast_non_dominated_sort(self, objectives): """Fast non-dominated sorting""" n = len(objectives) domination_count = np.zeros(n) dominated_solutions = [[] for _ in range(n)] fronts = [[]] for i in range(n): for j in range(n): if i == j: continue if self.dominates(objectives[i], objectives[j]): dominated_solutions[i].append(j) elif self.dominates(objectives[j], objectives[i]): domination_count[i] += 1 if domination_count[i] == 0: fronts[0].append(i) k = 0 while len(fronts[k]) > 0: next_front = [] for i in fronts[k]: for j in dominated_solutions[i]: domination_count[j] -= 1 if domination_count[j] == 0: next_front.append(j) k += 1 fronts.append(next_front) return fronts[:-1] def dominates(self, obj1, obj2): """Check if obj1 dominates obj2 (minimization)""" return all(obj1 <= obj2) and any(obj1 < obj2) def crowding_distance(self, objectives, front): """Calculate crowding distance for solutions in a front""" n = len(front) if n <= 2: return np.full(n, np.inf) distances = np.zeros(n) for m in range(3): sorted_indices = np.argsort([objectives[i][m] for i in front]) distances[sorted_indices[0]] = np.inf distances[sorted_indices[-1]] = np.inf obj_range = objectives[front[sorted_indices[-1]]][m] - \ objectives[front[sorted_indices[0]]][m] if obj_range == 0: continue for i in range(1, n - 1): distances[sorted_indices[i]] += \ (objectives[front[sorted_indices[i + 1]]][m] - \ objectives[front[sorted_indices[i - 1]]][m]) / obj_range return distances def tournament_selection(self, population, objectives, fronts): """Binary tournament selection""" idx1, idx2 = np.random.choice(len(population), 2, replace=False) front1, front2 = None, None for i, front in enumerate(fronts): if idx1 in front: front1 = i if idx2 in front: front2 = i if front1 < front2: return population[idx1].copy() elif front2 < front1: return population[idx2].copy() else: front = fronts[front1] distances = self.crowding_distance(objectives, front) pos1 = front.index(idx1) pos2 = front.index(idx2) if distances[pos1] > distances[pos2]: return population[idx1].copy() else: return population[idx2].copy() def crossover(self, parent1, parent2): """Simulated Binary Crossover (SBX)""" eta = 20 child1 = parent1.copy() child2 = parent2.copy() for i in range(len(parent1)): if np.random.rand() < 0.5: if abs(parent1[i] - parent2[i]) > 1e-6: u = np.random.rand() if u <= 0.5: beta = (2 * u) ** (1 / (eta + 1)) else: beta = (1 / (2 * (1 - u))) ** (1 / (eta + 1)) child1[i] = 0.5 * ((1 + beta) * parent1[i] + (1 - beta) * parent2[i]) child2[i] = 0.5 * ((1 - beta) * parent1[i] + (1 + beta) * parent2[i]) return child1, child2 def mutate(self, individual): """Polynomial mutation""" eta = 20 mutated = individual.copy() for i in range(len(individual)): if np.random.rand() < 1.0 / len(individual): u = np.random.rand() lb, ub = self.bounds[i] delta1 = (individual[i] - lb) / (ub - lb) delta2 = (ub - individual[i]) / (ub - lb) if u < 0.5: deltaq = (2 * u + (1 - 2 * u) * (1 - delta1) ** (eta + 1)) ** (1 / (eta + 1)) - 1 else: deltaq = 1 - (2 * (1 - u) + 2 * (u - 0.5) * (1 - delta2) ** (eta + 1)) ** (1 / (eta + 1)) mutated[i] = individual[i] + deltaq * (ub - lb) mutated[i] = np.clip(mutated[i], lb, ub) return mutated def optimize(self): """Run NSGA-II optimization""" population = self.initialize_population() for gen in range(self.n_gen): objectives = np.array([self.problem.evaluate(ind) for ind in population]) fronts = self.fast_non_dominated_sort(objectives) offspring = [] while len(offspring) < self.pop_size: parent1 = self.tournament_selection(population, objectives, fronts) parent2 = self.tournament_selection(population, objectives, fronts) child1, child2 = self.crossover(parent1, parent2) child1 = self.mutate(child1) child2 = self.mutate(child2) offspring.append(child1) if len(offspring) < self.pop_size: offspring.append(child2) offspring = np.array(offspring) combined = np.vstack([population, offspring]) combined_obj = np.array([self.problem.evaluate(ind) for ind in combined]) fronts = self.fast_non_dominated_sort(combined_obj) new_population = [] for front in fronts: if len(new_population) + len(front) <= self.pop_size: new_population.extend([combined[i] for i in front]) else: distances = self.crowding_distance(combined_obj, front) sorted_indices = np.argsort(distances)[::-1] remaining = self.pop_size - len(new_population) new_population.extend([combined[front[i]] for i in sorted_indices[:remaining]]) break population = np.array(new_population) if (gen + 1) % 20 == 0: print(f"Generation {gen + 1}/{self.n_gen} completed") objectives = np.array([self.problem.evaluate(ind) for ind in population]) fronts = self.fast_non_dominated_sort(objectives) pareto_front_indices = fronts[0] pareto_solutions = population[pareto_front_indices] pareto_objectives = objectives[pareto_front_indices] return pareto_solutions, pareto_objectives
print("=" * 60) print("Mars Exploration Mission Multi-Objective Optimization") print("=" * 60) print("\nObjectives:") print(" 1. Minimize Cost (Million USD)") print(" 2. Minimize Risk (0-100)") print(" 3. Maximize Scientific Return (0-100)") print("\nRunning NSGA-II optimization...") print("=" * 60)
mission = MarsExplorationMission() optimizer = NSGA2Optimizer(mission, pop_size=100, n_gen=100)
pareto_solutions, pareto_objectives = optimizer.optimize()
print("\n" + "=" * 60) print(f"Optimization complete! Found {len(pareto_solutions)} Pareto-optimal solutions") print("=" * 60)
pareto_objectives_display = pareto_objectives.copy() pareto_objectives_display[:, 2] = -pareto_objectives_display[:, 2]
fig = plt.figure(figsize=(20, 12))
ax1 = fig.add_subplot(2, 3, 1, projection='3d') scatter = ax1.scatter(pareto_objectives_display[:, 0], pareto_objectives_display[:, 1], pareto_objectives_display[:, 2], c=pareto_objectives_display[:, 2], cmap='viridis', s=100, alpha=0.6, edgecolors='black') ax1.set_xlabel('Cost (Million USD)', fontsize=10, fontweight='bold') ax1.set_ylabel('Risk Score', fontsize=10, fontweight='bold') ax1.set_zlabel('Scientific Return', fontsize=10, fontweight='bold') ax1.set_title('3D Pareto Front', fontsize=12, fontweight='bold') plt.colorbar(scatter, ax=ax1, label='Scientific Return', shrink=0.5)
ax2 = fig.add_subplot(2, 3, 2) scatter2 = ax2.scatter(pareto_objectives_display[:, 0], pareto_objectives_display[:, 1], c=pareto_objectives_display[:, 2], cmap='viridis', s=100, alpha=0.6, edgecolors='black') ax2.set_xlabel('Cost (Million USD)', fontsize=10, fontweight='bold') ax2.set_ylabel('Risk Score', fontsize=10, fontweight='bold') ax2.set_title('Cost vs Risk Trade-off', fontsize=12, fontweight='bold') ax2.grid(True, alpha=0.3) plt.colorbar(scatter2, ax=ax2, label='Scientific Return')
ax3 = fig.add_subplot(2, 3, 3) scatter3 = ax3.scatter(pareto_objectives_display[:, 0], pareto_objectives_display[:, 2], c=pareto_objectives_display[:, 1], cmap='coolwarm_r', s=100, alpha=0.6, edgecolors='black') ax3.set_xlabel('Cost (Million USD)', fontsize=10, fontweight='bold') ax3.set_ylabel('Scientific Return', fontsize=10, fontweight='bold') ax3.set_title('Cost vs Scientific Return', fontsize=12, fontweight='bold') ax3.grid(True, alpha=0.3) plt.colorbar(scatter3, ax=ax3, label='Risk Score')
ax4 = fig.add_subplot(2, 3, 4) scatter4 = ax4.scatter(pareto_objectives_display[:, 1], pareto_objectives_display[:, 2], c=pareto_objectives_display[:, 0], cmap='plasma', s=100, alpha=0.6, edgecolors='black') ax4.set_xlabel('Risk Score', fontsize=10, fontweight='bold') ax4.set_ylabel('Scientific Return', fontsize=10, fontweight='bold') ax4.set_title('Risk vs Scientific Return', fontsize=12, fontweight='bold') ax4.grid(True, alpha=0.3) plt.colorbar(scatter4, ax=ax4, label='Cost (M USD)')
ax5 = fig.add_subplot(2, 3, 5) variables = ['Cameras', 'Spectros', 'Drills', 'Duration', 'Solar', 'Bandwidth'] for i in range(3): ax5.hist(pareto_solutions[:, i], bins=10, alpha=0.5, label=variables[i]) ax5.set_xlabel('Count', fontsize=10, fontweight='bold') ax5.set_ylabel('Frequency', fontsize=10, fontweight='bold') ax5.set_title('Instrument Distribution in Pareto Solutions', fontsize=12, fontweight='bold') ax5.legend() ax5.grid(True, alpha=0.3)
ax6 = fig.add_subplot(2, 3, 6) total_instruments = (pareto_solutions[:, 0] + pareto_solutions[:, 1] + pareto_solutions[:, 2]) scatter6 = ax6.scatter(pareto_solutions[:, 3], total_instruments, c=pareto_objectives_display[:, 2], cmap='viridis', s=100, alpha=0.6, edgecolors='black') ax6.set_xlabel('Mission Duration (days)', fontsize=10, fontweight='bold') ax6.set_ylabel('Total Instruments', fontsize=10, fontweight='bold') ax6.set_title('Mission Duration vs Instrument Count', fontsize=12, fontweight='bold') ax6.grid(True, alpha=0.3) plt.colorbar(scatter6, ax=ax6, label='Scientific Return')
plt.tight_layout() plt.savefig('pareto_front_analysis.png', dpi=300, bbox_inches='tight') plt.show()
print("\n" + "=" * 60) print("Statistical Analysis of Pareto-Optimal Solutions") print("=" * 60) print(f"\nCost (Million USD):") print(f" Min: ${pareto_objectives_display[:, 0].min():.2f}M") print(f" Max: ${pareto_objectives_display[:, 0].max():.2f}M") print(f" Mean: ${pareto_objectives_display[:, 0].mean():.2f}M") print(f" Std: ${pareto_objectives_display[:, 0].std():.2f}M")
print(f"\nRisk Score:") print(f" Min: {pareto_objectives_display[:, 1].min():.2f}") print(f" Max: {pareto_objectives_display[:, 1].max():.2f}") print(f" Mean: {pareto_objectives_display[:, 1].mean():.2f}") print(f" Std: {pareto_objectives_display[:, 1].std():.2f}")
print(f"\nScientific Return:") print(f" Min: {pareto_objectives_display[:, 2].min():.2f}") print(f" Max: {pareto_objectives_display[:, 2].max():.2f}") print(f" Mean: {pareto_objectives_display[:, 2].mean():.2f}") print(f" Std: {pareto_objectives_display[:, 2].std():.2f}")
print("\n" + "=" * 60) print("Representative Mission Configurations") print("=" * 60)
idx_low_cost = np.argmin(pareto_objectives_display[:, 0]) print("\n1. LOWEST COST MISSION:") print(f" Cost: ${pareto_objectives_display[idx_low_cost, 0]:.2f}M") print(f" Risk: {pareto_objectives_display[idx_low_cost, 1]:.2f}") print(f" Science: {pareto_objectives_display[idx_low_cost, 2]:.2f}") print(f" Configuration:") print(f" - Cameras: {int(pareto_solutions[idx_low_cost, 0])}") print(f" - Spectrometers: {int(pareto_solutions[idx_low_cost, 1])}") print(f" - Drills: {int(pareto_solutions[idx_low_cost, 2])}") print(f" - Duration: {int(pareto_solutions[idx_low_cost, 3])} days") print(f" - Solar panels: {pareto_solutions[idx_low_cost, 4]:.1f} m²") print(f" - Bandwidth: {pareto_solutions[idx_low_cost, 5]:.1f} Mbps")
idx_high_science = np.argmax(pareto_objectives_display[:, 2]) print("\n2. HIGHEST SCIENCE MISSION:") print(f" Cost: ${pareto_objectives_display[idx_high_science, 0]:.2f}M") print(f" Risk: {pareto_objectives_display[idx_high_science, 1]:.2f}") print(f" Science: {pareto_objectives_display[idx_high_science, 2]:.2f}") print(f" Configuration:") print(f" - Cameras: {int(pareto_solutions[idx_high_science, 0])}") print(f" - Spectrometers: {int(pareto_solutions[idx_high_science, 1])}") print(f" - Drills: {int(pareto_solutions[idx_high_science, 2])}") print(f" - Duration: {int(pareto_solutions[idx_high_science, 3])} days") print(f" - Solar panels: {pareto_solutions[idx_high_science, 4]:.1f} m²") print(f" - Bandwidth: {pareto_solutions[idx_high_science, 5]:.1f} Mbps")
idx_low_risk = np.argmin(pareto_objectives_display[:, 1]) print("\n3. LOWEST RISK MISSION:") print(f" Cost: ${pareto_objectives_display[idx_low_risk, 0]:.2f}M") print(f" Risk: {pareto_objectives_display[idx_low_risk, 1]:.2f}") print(f" Science: {pareto_objectives_display[idx_low_risk, 2]:.2f}") print(f" Configuration:") print(f" - Cameras: {int(pareto_solutions[idx_low_risk, 0])}") print(f" - Spectrometers: {int(pareto_solutions[idx_low_risk, 1])}") print(f" - Drills: {int(pareto_solutions[idx_low_risk, 2])}") print(f" - Duration: {int(pareto_solutions[idx_low_risk, 3])} days") print(f" - Solar panels: {pareto_solutions[idx_low_risk, 4]:.1f} m²") print(f" - Bandwidth: {pareto_solutions[idx_low_risk, 5]:.1f} Mbps")
normalized_obj = (pareto_objectives_display - pareto_objectives_display.min(axis=0)) / \ (pareto_objectives_display.max(axis=0) - pareto_objectives_display.min(axis=0)) normalized_obj[:, 2] = 1 - normalized_obj[:, 2] distances = np.sqrt(np.sum(normalized_obj**2, axis=1)) idx_balanced = np.argmin(distances)
print("\n4. BALANCED MISSION (Best compromise):") print(f" Cost: ${pareto_objectives_display[idx_balanced, 0]:.2f}M") print(f" Risk: {pareto_objectives_display[idx_balanced, 1]:.2f}") print(f" Science: {pareto_objectives_display[idx_balanced, 2]:.2f}") print(f" Configuration:") print(f" - Cameras: {int(pareto_solutions[idx_balanced, 0])}") print(f" - Spectrometers: {int(pareto_solutions[idx_balanced, 1])}") print(f" - Drills: {int(pareto_solutions[idx_balanced, 2])}") print(f" - Duration: {int(pareto_solutions[idx_balanced, 3])} days") print(f" - Solar panels: {pareto_solutions[idx_balanced, 4]:.1f} m²") print(f" - Bandwidth: {pareto_solutions[idx_balanced, 5]:.1f} Mbps")
print("\n" + "=" * 60)
|