Problems
The Problems layer defines the optimization problems solvable by KOPPU.
Base Class
pykoppu.problems.base.PUBOProblem
Bases: ABC
Polynomial Unconstrained Binary Optimization (PUBO) Problem.
Source code in pykoppu/problems/base.py
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 | |
evaluate(solution)
Evaluate the quality of a solution.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
solution
|
Any
|
The solution vector. |
required |
Returns:
| Type | Description |
|---|---|
Dict[str, Any]
|
Dict[str, Any]: Metrics dictionary. |
Source code in pykoppu/problems/base.py
29 30 31 32 33 34 35 36 37 38 39 40 | |
plot(result, threshold=0.5)
abstractmethod
Visualize the solution.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
result
|
Any
|
The simulation result object. |
required |
threshold
|
float
|
Threshold for binarizing the solution. Defaults to 0.5. |
0.5
|
Source code in pykoppu/problems/base.py
42 43 44 45 46 47 48 49 50 51 | |
to_hamiltonian()
abstractmethod
Convert the problem to Hamiltonian form (J, h). Must be implemented by subclasses.
Source code in pykoppu/problems/base.py
21 22 23 24 25 26 27 | |
Math Problems
pykoppu.problems.math.Factorization
Bases: PUBOProblem
Integer Factorization Problem.
Factors a number N into two integers p and q such that N = p * q. Uses a multiplication circuit reduction to QUBO.
Source code in pykoppu/problems/math/factorization.py
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 | |
__init__(target, p_bits=None, q_bits=None, penalty=10.0)
Initialize Factorization problem.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
target
|
int
|
The number to factor (N). |
required |
p_bits
|
int
|
Number of bits for the first factor p. |
None
|
q_bits
|
int
|
Number of bits for the second factor q. |
None
|
penalty
|
float
|
Penalty strength for consistency constraints. |
10.0
|
Source code in pykoppu/problems/math/factorization.py
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 | |
evaluate(solution)
Evaluate Factorization solution.
Source code in pykoppu/problems/math/factorization.py
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 | |
plot(result, threshold=0.5)
Visualize Factorization result.
Source code in pykoppu/problems/math/factorization.py
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 | |
to_hamiltonian()
Convert Factorization to Hamiltonian.
Variables: - x_i: bits of p (0..n-1) - y_j: bits of q (0..m-1) - z_{ij}: auxiliary variables for x_i * y_j
Hamiltonian H = H_fact + H_cons
H_fact = (N - sum_{i,j} 2^{i+j} z_{ij})^2 = (N - P)^2 where P is the product constructed from z
H_cons = sum_{i,j} P_penalty * (3 z_{ij} + x_i y_j - 2 x_i z_{ij} - 2 y_j z_{ij}) This penalty enforces z_{ij} = x_i AND y_j.
Source code in pykoppu/problems/math/factorization.py
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 | |
pykoppu.problems.math.SAT3
Bases: PUBOProblem
3-SAT Problem.
Determines if there exists an interpretation that satisfies a given Boolean formula.
Source code in pykoppu/problems/math/sat3.py
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 | |
__init__(clauses, n_vars, penalty=2.0)
Initialize 3-SAT problem.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
clauses
|
List[Tuple[int, int, int]]
|
List of clauses. Each clause is a tuple of 3 literals. Positive int k means variable x_k. Negative int -k means NOT x_k. Variables are 1-indexed (1 to n_vars). |
required |
n_vars
|
int
|
Number of variables. |
required |
penalty
|
float
|
Penalty strength for constraints. |
2.0
|
Source code in pykoppu/problems/math/sat3.py
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | |
decode_solution(result, threshold=0.5)
Decode solution from MIS to Truth Assignment.
Source code in pykoppu/problems/math/sat3.py
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 | |
plot(result, threshold=0.5)
Visualize SAT graph and solution.
Source code in pykoppu/problems/math/sat3.py
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 | |
to_hamiltonian()
Convert 3-SAT to Hamiltonian via Maximum Independent Set (MIS).
Reduction: 1. Construct a graph G where each node represents a literal in a clause. Total nodes = 3 * num_clauses. 2. Add edges between literals in the same clause (triangle/clique). This ensures at most one literal per clause is selected in MIS. 3. Add edges between conflicting literals (x and NOT x) across clauses. This ensures consistency.
We want to find an Independent Set of size equal to num_clauses. If such a set exists, we pick one true literal from each clause, and no conflicts exist.
Hamiltonian for MIS: Maximize size of independent set: Minimize H = - sum y_i + P * sum_{(i,j) in E} y_i y_j Where y_i is binary variable for node i.
Source code in pykoppu/problems/math/sat3.py
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 | |
Graph Problems
pykoppu.problems.graph.MaxCut
Bases: PUBOProblem
MaxCut Problem.
Finds a cut that maximizes the sum of weights of edges crossing the cut.
Source code in pykoppu/problems/graph/maxcut.py
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 | |
__init__(graph)
Initialize MaxCut problem.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
graph
|
Graph
|
The input graph. |
required |
Source code in pykoppu/problems/graph/maxcut.py
19 20 21 22 23 24 25 26 27 28 | |
evaluate(solution)
Calculate MaxCut quality (percentage of edges cut).
Source code in pykoppu/problems/graph/maxcut.py
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 | |
plot(result, threshold=0.5)
Visualize MaxCut solution.
Source code in pykoppu/problems/graph/maxcut.py
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 | |
to_hamiltonian()
Convert MaxCut to Hamiltonian.
For MaxCut, we want to maximize the number of cut edges. In Ising formulation (s_i \in {-1, 1}): H = sum_{i,j} J_{ij} s_i s_j
To maximize cut, we want neighbors to have different spins. If J_{ij} > 0 (antiferromagnetic), minimizing H favors s_i != s_j.
So we set J_{uv} = 1.0 for all edges (u, v).
Source code in pykoppu/problems/graph/maxcut.py
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 | |
Logistics Problems
pykoppu.problems.logistics.TSP
Bases: PUBOProblem
Traveling Salesperson Problem (TSP).
Finds the shortest route visiting each city exactly once and returning to the origin.
Source code in pykoppu/problems/logistics/tsp.py
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 | |
__init__(distance_matrix, penalty=10.0)
Initialize TSP problem.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
distance_matrix
|
ndarray
|
NxN matrix of distances between cities. |
required |
penalty
|
float
|
Penalty strength for constraints. |
10.0
|
Source code in pykoppu/problems/logistics/tsp.py
19 20 21 22 23 24 25 26 27 28 29 30 31 | |
plot(result, threshold=0.5)
Visualize TSP solution.
Source code in pykoppu/problems/logistics/tsp.py
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 | |
to_hamiltonian()
Convert TSP to Hamiltonian.
Variables: x_{i,t} = 1 if city i is visited at step t. Total variables: N^2.
Constraints: 1. Each city visited exactly once: sum_t x_{i,t} = 1 for all i. 2. Each step has exactly one city: sum_i x_{i,t} = 1 for all t.
Objective: Minimize distance: sum_{i,j} sum_t d_{ij} x_{i,t} x_{j,t+1}
Hamiltonian: H = A * sum_i (sum_t x_{i,t} - 1)^2 (Row constraints) + A * sum_t (sum_i x_{i,t} - 1)^2 (Column constraints) + sum_{i,j} sum_t d_{ij} x_{i,t} x_{j,t+1} (Distance)
Source code in pykoppu/problems/logistics/tsp.py
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 | |
pykoppu.problems.logistics.Knapsack
Bases: PUBOProblem
Knapsack Problem.
Maximize sum v_i x_i subject to sum w_i x_i <= C. Implemented as QUBO with penalty for equality constraint (slack variables omitted for simplicity or assuming exact capacity match if that's the prompt's implication, but usually Knapsack is inequality. The prompt formula uses (sum w_i x_i - C)^2 which enforces equality sum w_i x_i = C).
Source code in pykoppu/problems/logistics/knapsack.py
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 | |
__init__(items, capacity, penalty)
Initialize Knapsack problem.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
items
|
List[Dict]
|
List of items with 'value' and 'weight'. |
required |
capacity
|
float
|
Target capacity. |
required |
penalty
|
float
|
Penalty coefficient (P). |
required |
Source code in pykoppu/problems/logistics/knapsack.py
22 23 24 25 26 27 28 29 30 31 32 33 34 35 | |
evaluate(solution)
Evaluate Knapsack solution.
Source code in pykoppu/problems/logistics/knapsack.py
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 | |
plot(result, threshold=0.5)
Visualize Knapsack solution.
Source code in pykoppu/problems/logistics/knapsack.py
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 | |
to_hamiltonian()
Convert to Hamiltonian.
H = - sum v_i x_i + P (sum w_i x_i - C)^2
Expand squared term: (sum w_i x_i - C)^2 = (sum w_i x_i)^2 - 2C sum w_i x_i + C^2 (sum w_i x_i)^2 = sum w_i^2 x_i^2 + sum_{i!=j} w_i w_j x_i x_j Since x_i is binary, x_i^2 = x_i. = sum w_i^2 x_i + sum_{i!=j} w_i w_j x_i x_j
Combine linear terms (x_i): H_linear = - v_i + P(w_i^2 - 2C w_i)
Combine quadratic terms (x_i x_j): H_quad = P w_i w_j
We want to minimize H. E = -0.5 x^T J x - h^T x
So we need to map H coefficients to J and h. H = sum_{i<j} 2 * (P w_i w_j) x_i x_j + sum (coeff_i) x_i (The 2 is because sum_{i!=j} includes both ij and ji).
Map to E: -0.5 * J_ij = P w_i w_j => J_ij = -2 P w_i w_j -h_i = -v_i + P(w_i^2 - 2C w_i) => h_i = v_i - P(w_i^2 - 2C w_i)
Source code in pykoppu/problems/logistics/knapsack.py
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 | |
Finance Problems
pykoppu.problems.finance.PortfolioOptimization
Bases: PUBOProblem
Portfolio Optimization Problem.
Minimize risk and maximize return. H = q * sum sigma_ij x_i x_j - sum mu_i x_i
Source code in pykoppu/problems/finance/portfolio.py
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 | |
__init__(expected_returns, covariance_matrix, risk_aversion)
Initialize Portfolio Optimization.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
expected_returns
|
list
|
List of expected returns (mu). |
required |
covariance_matrix
|
ndarray
|
Covariance matrix (sigma). |
required |
risk_aversion
|
float
|
Risk aversion coefficient (q). |
required |
Source code in pykoppu/problems/finance/portfolio.py
19 20 21 22 23 24 25 26 27 28 29 30 31 32 | |
plot(result, threshold=0.5)
Visualize Portfolio Optimization solution.
Source code in pykoppu/problems/finance/portfolio.py
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 | |
to_hamiltonian()
Convert to Hamiltonian.
H = q * x^T Sigma x - mu^T x
Map to E = -0.5 x^T J x - h^T x
-0.5 J = q * Sigma => J = -2 * q * Sigma -h = -mu => h = mu
Source code in pykoppu/problems/finance/portfolio.py
34 35 36 37 38 39 40 41 42 43 44 45 46 | |
Energy Problems
pykoppu.problems.energy.WellPlacement
Bases: PUBOProblem
Well Placement Optimization Problem.
Selects optimal well locations from a set of candidates to maximize production value while respecting budget and minimum distance constraints.
Source code in pykoppu/problems/energy/well_placement.py
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 | |
__init__(locations, budget, min_dist, penalty_budget=10.0, penalty_dist=10.0)
Initialize Well Placement problem.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
locations
|
List[Dict]
|
List of candidate locations. Each dict must have: - 'id': int/str identifier - 'x': float x-coordinate - 'y': float y-coordinate - 'value': float estimated production value - 'cost': float drilling cost |
required |
budget
|
float
|
Total available budget. |
required |
min_dist
|
float
|
Minimum required distance between any two wells. |
required |
penalty_budget
|
float
|
Penalty strength for budget constraint. |
10.0
|
penalty_dist
|
float
|
Penalty strength for distance constraint. |
10.0
|
Source code in pykoppu/problems/energy/well_placement.py
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 | |
evaluate(solution)
Evaluate solution.
Source code in pykoppu/problems/energy/well_placement.py
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 | |
plot(result, threshold=0.5)
Visualize Well Placement.
Source code in pykoppu/problems/energy/well_placement.py
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 | |
to_hamiltonian()
Convert to Hamiltonian.
Variables: x_i = 1 if well i is selected, 0 otherwise.
H = H_value + H_budget + H_dist
-
H_value (Maximize value => Minimize negative value): H_value = sum (-v_i * x_i)
-
H_budget (Constraint: sum c_i x_i <= B): Modeled as equality penalty (sum c_i x_i - B)^2 for simplicity, assuming we want to utilize the budget. = (sum c_i x_i)^2 - 2B sum c_i x_i + B^2 = sum c_i^2 x_i + sum_{i!=j} c_i c_j x_i x_j - 2B sum c_i x_i + B^2
-
H_dist (Constraint: dist(i, j) >= min_dist): Penalty if both x_i and x_j are selected and dist(i, j) < min_dist. H_dist = sum_{i<j, incompatible} P_dist * x_i * x_j
Source code in pykoppu/problems/energy/well_placement.py
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 | |
pykoppu.problems.energy.SeismicFeatureSelection
Bases: PUBOProblem
Seismic Feature Selection Problem.
Selects optimal subset of seismic attributes to maximize relevance to a target while minimizing redundancy between selected attributes (mRMR). Subject to a cardinality constraint (select exactly k attributes).
Source code in pykoppu/problems/energy/seismic.py
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 | |
__init__(relevance, redundancy, k, alpha=1.0, beta=1.0, penalty_k=10.0)
Initialize Seismic Feature Selection problem.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
relevance
|
Array - like
|
Vector of relevance scores for each attribute (R). |
required |
redundancy
|
Array - like
|
Matrix of redundancy/correlation between attributes (C). |
required |
k
|
int
|
Number of attributes to select. |
required |
alpha
|
float
|
Weight for relevance term. |
1.0
|
beta
|
float
|
Weight for redundancy term. |
1.0
|
penalty_k
|
float
|
Penalty strength for cardinality constraint. |
10.0
|
Source code in pykoppu/problems/energy/seismic.py
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 | |
evaluate(solution)
Evaluate solution.
Source code in pykoppu/problems/energy/seismic.py
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 | |
plot(result, threshold=0.5)
Visualize Feature Selection.
Source code in pykoppu/problems/energy/seismic.py
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 | |
to_hamiltonian()
Convert to Hamiltonian.
Variables: x_i = 1 if attribute i is selected, 0 otherwise.
H = H_rel + H_red + H_card
-
H_rel (Maximize relevance => Minimize negative): H_rel = sum (-alpha * R_i * x_i)
-
H_red (Minimize redundancy): H_red = sum_{i,j} beta * C_{ij} * x_i * x_j
-
H_card (Select exactly k): H_card = P * (sum x_i - k)^2 = P * (sum x_i^2 + sum_{i!=j} x_i x_j - 2k sum x_i + k^2) = P * (sum x_i + sum_{i!=j} x_i x_j - 2k sum x_i + k^2) (since x^2=x) = P * (sum (1-2k) x_i + sum_{i!=j} x_i x_j + k^2)
Source code in pykoppu/problems/energy/seismic.py
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 | |