|
| 1 | +#title: Random Sampling Algorithm (Template) |
| 2 | +#author: Funz Contributors |
| 3 | +#type: sampling |
| 4 | +#options: nvalues=10;seed=42 |
| 5 | + |
| 6 | +""" |
| 7 | +Template algorithm for fz plugin system. |
| 8 | +
|
| 9 | +This is a minimal algorithm that performs random sampling. |
| 10 | +Customize it to implement your own algorithm. |
| 11 | +
|
| 12 | +Algorithm Interface: |
| 13 | + __init__(**options): Constructor with algorithm-specific options |
| 14 | + get_initial_design(input_vars, output_vars): Return initial design of experiments |
| 15 | + get_next_design(X, Y): Return next design based on previous results, or [] when finished |
| 16 | + get_analysis(X, Y): Return analysis results dict |
| 17 | + get_analysis_tmp(X, Y): [OPTIONAL] Return intermediate analysis at each iteration |
| 18 | +""" |
| 19 | + |
| 20 | +import random |
| 21 | + |
| 22 | + |
| 23 | +class Algorithm: |
| 24 | + """ |
| 25 | + Template algorithm: Random Sampling |
| 26 | +
|
| 27 | + This is a one-shot sampling algorithm that generates random points |
| 28 | + uniformly distributed within the input variable bounds. |
| 29 | +
|
| 30 | + Replace this with your own algorithm implementation. |
| 31 | +
|
| 32 | + Options: |
| 33 | + nvalues (int): Number of random samples to generate (default: 10) |
| 34 | + seed (int): Random seed for reproducibility (default: 42) |
| 35 | + """ |
| 36 | + |
| 37 | + def __init__(self, **options): |
| 38 | + """ |
| 39 | + Initialize algorithm with options. |
| 40 | +
|
| 41 | + Options are parsed from the #options header comment and/or |
| 42 | + passed explicitly via fzd(..., algorithm_options={...}). |
| 43 | + Explicit options take precedence over header defaults. |
| 44 | + """ |
| 45 | + self.nvalues = int(options.get("nvalues", 10)) |
| 46 | + seed = options.get("seed", None) |
| 47 | + if seed is not None: |
| 48 | + random.seed(int(seed)) |
| 49 | + |
| 50 | + def get_initial_design(self, input_vars, output_vars): |
| 51 | + """ |
| 52 | + Generate initial design of experiments. |
| 53 | +
|
| 54 | + Args: |
| 55 | + input_vars: Dict[str, Tuple[float, float]] - {var_name: (min, max)} |
| 56 | + e.g., {"x": (0.0, 1.0), "y": (-5.0, 5.0)} |
| 57 | + output_vars: List[str] - Output variable names |
| 58 | + e.g., ["pressure", "temperature"] |
| 59 | +
|
| 60 | + Returns: |
| 61 | + List[Dict[str, float]] - List of input variable combinations to evaluate |
| 62 | + e.g., [{"x": 0.5, "y": 0.0}, {"x": 0.7, "y": 2.3}] |
| 63 | + """ |
| 64 | + samples = [] |
| 65 | + for _ in range(self.nvalues): |
| 66 | + sample = {} |
| 67 | + for var_name, (min_val, max_val) in input_vars.items(): |
| 68 | + sample[var_name] = random.uniform(min_val, max_val) |
| 69 | + samples.append(sample) |
| 70 | + return samples |
| 71 | + |
| 72 | + def get_next_design(self, X, Y): |
| 73 | + """ |
| 74 | + Generate next design based on previous results. |
| 75 | +
|
| 76 | + For this one-shot algorithm, always returns [] (finished). |
| 77 | + For iterative algorithms, return new points to evaluate, |
| 78 | + or [] when converged. |
| 79 | +
|
| 80 | + Args: |
| 81 | + X: List[Dict[str, float]] - Previous input combinations |
| 82 | + Y: List[float] - Corresponding output values (may contain None for failed cases) |
| 83 | +
|
| 84 | + Returns: |
| 85 | + List[Dict[str, float]] - Next points to evaluate, or [] if finished |
| 86 | + """ |
| 87 | + return [] # One-shot algorithm: no further iterations |
| 88 | + |
| 89 | + def get_analysis(self, X, Y): |
| 90 | + """ |
| 91 | + Return final analysis results. |
| 92 | +
|
| 93 | + Args: |
| 94 | + X: List[Dict[str, float]] - All evaluated input combinations |
| 95 | + Y: List[float] - All corresponding output values (may contain None) |
| 96 | +
|
| 97 | + Returns: |
| 98 | + Dict with analysis information. Common keys: |
| 99 | + 'text': Human-readable summary string |
| 100 | + 'data': Dict with numerical results |
| 101 | + 'html': Optional HTML output (e.g., plots encoded as base64 images) |
| 102 | + """ |
| 103 | + # Filter out None values (failed evaluations) |
| 104 | + valid_results = [(inp, out) for inp, out in zip(X, Y) if out is not None] |
| 105 | + |
| 106 | + if not valid_results: |
| 107 | + return { |
| 108 | + "text": "No valid results", |
| 109 | + "data": {"samples": len(X), "valid_samples": 0}, |
| 110 | + } |
| 111 | + |
| 112 | + valid_outputs = [out for _, out in valid_results] |
| 113 | + best_input, best_output = min(valid_results, key=lambda r: r[1]) |
| 114 | + worst_input, worst_output = max(valid_results, key=lambda r: r[1]) |
| 115 | + mean_output = sum(valid_outputs) / len(valid_outputs) |
| 116 | + |
| 117 | + result_text = f"""Random Sampling Results: |
| 118 | + Total samples: {len(X)} |
| 119 | + Valid samples: {len(valid_results)} |
| 120 | + Best output: {best_output:.6g} |
| 121 | + Best input: {best_input} |
| 122 | + Worst output: {worst_output:.6g} |
| 123 | + Mean output: {mean_output:.6g} |
| 124 | +""" |
| 125 | + |
| 126 | + return { |
| 127 | + "text": result_text, |
| 128 | + "data": { |
| 129 | + "samples": len(X), |
| 130 | + "valid_samples": len(valid_results), |
| 131 | + "best_output": best_output, |
| 132 | + "best_input": best_input, |
| 133 | + "worst_output": worst_output, |
| 134 | + "mean_output": mean_output, |
| 135 | + }, |
| 136 | + } |
| 137 | + |
| 138 | + def get_analysis_tmp(self, X, Y): |
| 139 | + """ |
| 140 | + [OPTIONAL] Return intermediate analysis at each iteration. |
| 141 | +
|
| 142 | + Called after each batch of evaluations to show progress. |
| 143 | + If this method is not present, no intermediate results are displayed. |
| 144 | +
|
| 145 | + Args: |
| 146 | + X: List[Dict[str, float]] - All evaluated inputs so far |
| 147 | + Y: List[float] - All outputs so far (may contain None) |
| 148 | +
|
| 149 | + Returns: |
| 150 | + Dict with intermediate analysis information (typically 'text' and 'data') |
| 151 | + """ |
| 152 | + valid_count = sum(1 for y in Y if y is not None) |
| 153 | + return { |
| 154 | + "text": f" Progress: {valid_count}/{len(Y)} valid samples collected", |
| 155 | + "data": {"valid_samples": valid_count, "total_samples": len(Y)}, |
| 156 | + } |
0 commit comments