Coverage for moptipy / examples / bitstrings / ising1d.py: 68%

22 statements  

« prev     ^ index     » next       coverage.py v7.12.0, created at 2025-11-24 08:49 +0000

1""" 

2The one-dimensional Ising problem. 

3 

4The one-dimensional Ising problem describes a ring. For each bit that differs 

5from its (right-side) neighboring bit, a penalty of 1 is incurred. The optimum 

6is a bit string of either all ones or all zeros. The optimal objective value 

7is 0, the worst-possible one is `n`. 

8 

91. Simon Fischer and Ingo Wegener. The one-dimensional Ising model: Mutation 

10 versus recombination. *Theoretical Computer Science.* 344(2-3):208-225. 

11 November 2005. doi: https://doi.org/10.1016/j.tcs.2005.04.002 

122. Carola Doerr and Furong Ye and Naama Horesh and Hao Wang and Ofer M. Shir 

13 and Thomas Bäck. Benchmarking Discrete Optimization Heuristics with 

14 IOHprofiler. Applied Soft Computing 88:106027, March 2020, 

15 doi: https://doi.org/10.1016/j.asoc.2019.106027. 

163. Clarissa Van Hoyweghen, David Edward Goldberg, and Bart Naudts. From Twomax 

17 To The Ising Model: Easy And Hard Symmetrical Problems. *In Proceedings of 

18 the Genetic and Evolutionary Computation Conference (GECCO'02),* July 9-13, 

19 2002, New York, NY, USA, pages 626-633. Morgan Kaufmann. 

20 http://gpbib.cs.ucl.ac.uk/gecco2002/GA013.pdf 

214. Thomas Weise, Zhize Wu, Xinlu Li, Yan Chen, and Jörg Lässig. Frequency 

22 Fitness Assignment: Optimization without Bias for Good Solutions can be 

23 Efficient. *IEEE Transactions on Evolutionary Computation (TEVC)*. 

24 27(4):980-992. August 2023. 

25 doi: https://doi.org/10.1109/TEVC.2022.3191698 

26""" 

27 

28from typing import Callable, Iterator, cast 

29 

30import numba # type: ignore 

31import numpy as np 

32 

33from moptipy.examples.bitstrings.bitstring_problem import BitStringProblem 

34 

35 

36@numba.njit(nogil=True, cache=True) 

37def ising1d(x: np.ndarray) -> int: 

38 """ 

39 Compute the objective value of the 1-dimensional Ising problem. 

40 

41 :param x: the np array 

42 :return: the ising1d function value 

43 

44 >>> ising1d(np.array([True, True, True, True, True])) 

45 0 

46 >>> ising1d(np.array([False, False, False, False, False])) 

47 0 

48 >>> ising1d(np.array([False, False, False, True, False])) 

49 2 

50 >>> ising1d(np.array([True, False, False, False, False])) 

51 2 

52 >>> ising1d(np.array([False, False, False, False, True])) 

53 2 

54 >>> ising1d(np.array([True, False, False, False, True])) 

55 2 

56 >>> ising1d(np.array([True, False, True, False, False])) 

57 4 

58 >>> ising1d(np.array([True, False, True, False, True, False])) 

59 6 

60 

61 # n = 1 and 0 true bits 

62 >>> ising1d(np.array([0])) 

63 0 

64 

65 # n = 1 and 1 true bit 

66 >>> ising1d(np.array([1])) 

67 0 

68 

69 # n = 2 and 0 true bits 

70 >>> ising1d(np.array([0, 0])) 

71 0 

72 

73 # n = 2 and 1 true bit 

74 >>> ising1d(np.array([1, 0])) 

75 2 

76 

77 # n = 2 and 1 true bit 

78 >>> ising1d(np.array([0, 1])) 

79 2 

80 

81 # n = 2 and 1 true bit 

82 >>> ising1d(np.array([0, 1])) 

83 2 

84 

85 # n = 2 and 2 true bits 

86 >>> ising1d(np.array([1, 1])) 

87 0 

88 

89 # n = 3 and 0 true bits 

90 >>> ising1d(np.array([0, 0, 0])) 

91 0 

92 

93 # n = 3 and 1 true bit 

94 >>> ising1d(np.array([1, 0, 0])) 

95 2 

96 

97 # n = 3 and 1 true bit 

98 >>> ising1d(np.array([0, 0, 1])) 

99 2 

100 

101 # n = 3 and 1 true bit 

102 >>> ising1d(np.array([1, 0, 0])) 

103 2 

104 

105 # n = 3 and 2 true bits 

106 >>> ising1d(np.array([1, 0, 1])) 

107 2 

108 

109 # n = 3 and 2 true bits 

110 >>> ising1d(np.array([1, 0, 1])) 

111 2 

112 

113 # n = 3 and 2 true bits 

114 >>> ising1d(np.array([1, 1, 0])) 

115 2 

116 

117 # n = 3 and 3 true bits 

118 >>> ising1d(np.array([1, 1, 1])) 

119 0 

120 

121 # n = 4 and 0 true bits 

122 >>> ising1d(np.array([0, 0, 0, 0])) 

123 0 

124 

125 # n = 4 and 1 true bit 

126 >>> ising1d(np.array([1, 0, 0, 0])) 

127 2 

128 

129 # n = 4 and 1 true bit 

130 >>> ising1d(np.array([0, 0, 1, 0])) 

131 2 

132 

133 # n = 4 and 1 true bit 

134 >>> ising1d(np.array([1, 0, 0, 0])) 

135 2 

136 

137 # n = 4 and 2 true bits 

138 >>> ising1d(np.array([1, 0, 0, 1])) 

139 2 

140 

141 # n = 4 and 2 true bits 

142 >>> ising1d(np.array([1, 1, 0, 0])) 

143 2 

144 

145 # n = 4 and 2 true bits 

146 >>> ising1d(np.array([0, 1, 1, 0])) 

147 2 

148 

149 # n = 4 and 3 true bits 

150 >>> ising1d(np.array([0, 1, 1, 1])) 

151 2 

152 

153 # n = 4 and 3 true bits 

154 >>> ising1d(np.array([1, 1, 0, 1])) 

155 2 

156 

157 # n = 4 and 3 true bits 

158 >>> ising1d(np.array([0, 1, 1, 1])) 

159 2 

160 

161 # n = 4 and 4 true bits 

162 >>> ising1d(np.array([1, 1, 1, 1])) 

163 0 

164 

165 # n = 5 and 0 true bits 

166 >>> ising1d(np.array([0, 0, 0, 0, 0])) 

167 0 

168 

169 # n = 5 and 1 true bit 

170 >>> ising1d(np.array([0, 0, 0, 0, 1])) 

171 2 

172 

173 # n = 5 and 1 true bit 

174 >>> ising1d(np.array([0, 0, 0, 0, 1])) 

175 2 

176 

177 # n = 5 and 1 true bit 

178 >>> ising1d(np.array([0, 1, 0, 0, 0])) 

179 2 

180 

181 # n = 5 and 2 true bits 

182 >>> ising1d(np.array([0, 1, 0, 0, 1])) 

183 4 

184 

185 # n = 5 and 2 true bits 

186 >>> ising1d(np.array([0, 1, 0, 0, 1])) 

187 4 

188 

189 # n = 5 and 2 true bits 

190 >>> ising1d(np.array([0, 1, 1, 0, 0])) 

191 2 

192 

193 # n = 5 and 3 true bits 

194 >>> ising1d(np.array([1, 1, 0, 1, 0])) 

195 4 

196 

197 # n = 5 and 3 true bits 

198 >>> ising1d(np.array([1, 1, 1, 0, 0])) 

199 2 

200 

201 # n = 5 and 3 true bits 

202 >>> ising1d(np.array([1, 0, 1, 0, 1])) 

203 4 

204 

205 # n = 5 and 4 true bits 

206 >>> ising1d(np.array([1, 1, 1, 1, 0])) 

207 2 

208 

209 # n = 5 and 4 true bits 

210 >>> ising1d(np.array([1, 1, 0, 1, 1])) 

211 2 

212 

213 # n = 5 and 4 true bits 

214 >>> ising1d(np.array([1, 1, 0, 1, 1])) 

215 2 

216 

217 # n = 5 and 5 true bits 

218 >>> ising1d(np.array([1, 1, 1, 1, 1])) 

219 0 

220 

221 # n = 6 and 0 true bits 

222 >>> ising1d(np.array([0, 0, 0, 0, 0, 0])) 

223 0 

224 

225 # n = 6 and 1 true bit 

226 >>> ising1d(np.array([0, 0, 0, 0, 0, 1])) 

227 2 

228 

229 # n = 6 and 1 true bit 

230 >>> ising1d(np.array([0, 0, 0, 0, 1, 0])) 

231 2 

232 

233 # n = 6 and 1 true bit 

234 >>> ising1d(np.array([0, 1, 0, 0, 0, 0])) 

235 2 

236 

237 # n = 6 and 2 true bits 

238 >>> ising1d(np.array([1, 1, 0, 0, 0, 0])) 

239 2 

240 

241 # n = 6 and 2 true bits 

242 >>> ising1d(np.array([1, 1, 0, 0, 0, 0])) 

243 2 

244 

245 # n = 6 and 2 true bits 

246 >>> ising1d(np.array([0, 0, 0, 1, 1, 0])) 

247 2 

248 

249 # n = 6 and 3 true bits 

250 >>> ising1d(np.array([1, 0, 0, 1, 0, 1])) 

251 4 

252 

253 # n = 6 and 3 true bits 

254 >>> ising1d(np.array([1, 0, 0, 0, 1, 1])) 

255 2 

256 

257 # n = 6 and 3 true bits 

258 >>> ising1d(np.array([1, 1, 0, 0, 1, 0])) 

259 4 

260 

261 # n = 6 and 4 true bits 

262 >>> ising1d(np.array([1, 0, 1, 1, 1, 0])) 

263 4 

264 

265 # n = 6 and 4 true bits 

266 >>> ising1d(np.array([1, 1, 1, 1, 0, 0])) 

267 2 

268 

269 # n = 6 and 4 true bits 

270 >>> ising1d(np.array([1, 1, 0, 1, 0, 1])) 

271 4 

272 

273 # n = 6 and 5 true bits 

274 >>> ising1d(np.array([1, 1, 1, 1, 0, 1])) 

275 2 

276 

277 # n = 6 and 5 true bits 

278 >>> ising1d(np.array([1, 0, 1, 1, 1, 1])) 

279 2 

280 

281 # n = 6 and 5 true bits 

282 >>> ising1d(np.array([0, 1, 1, 1, 1, 1])) 

283 2 

284 

285 # n = 6 and 6 true bits 

286 >>> ising1d(np.array([1, 1, 1, 1, 1, 1])) 

287 0 

288 

289 # n = 7 and 0 true bits 

290 >>> ising1d(np.array([0, 0, 0, 0, 0, 0, 0])) 

291 0 

292 

293 # n = 7 and 1 true bit 

294 >>> ising1d(np.array([0, 0, 0, 1, 0, 0, 0])) 

295 2 

296 

297 # n = 7 and 1 true bit 

298 >>> ising1d(np.array([0, 0, 0, 0, 0, 1, 0])) 

299 2 

300 

301 # n = 7 and 1 true bit 

302 >>> ising1d(np.array([0, 0, 0, 0, 0, 1, 0])) 

303 2 

304 

305 # n = 7 and 2 true bits 

306 >>> ising1d(np.array([0, 1, 1, 0, 0, 0, 0])) 

307 2 

308 

309 # n = 7 and 2 true bits 

310 >>> ising1d(np.array([1, 0, 0, 0, 0, 1, 0])) 

311 4 

312 

313 # n = 7 and 2 true bits 

314 >>> ising1d(np.array([1, 0, 0, 0, 0, 0, 1])) 

315 2 

316 

317 # n = 7 and 3 true bits 

318 >>> ising1d(np.array([1, 0, 0, 0, 0, 1, 1])) 

319 2 

320 

321 # n = 7 and 3 true bits 

322 >>> ising1d(np.array([1, 0, 1, 0, 0, 1, 0])) 

323 6 

324 

325 # n = 7 and 3 true bits 

326 >>> ising1d(np.array([1, 0, 0, 1, 0, 1, 0])) 

327 6 

328 

329 # n = 7 and 4 true bits 

330 >>> ising1d(np.array([0, 1, 1, 0, 1, 1, 0])) 

331 4 

332 

333 # n = 7 and 4 true bits 

334 >>> ising1d(np.array([0, 1, 0, 1, 1, 0, 1])) 

335 6 

336 

337 # n = 7 and 4 true bits 

338 >>> ising1d(np.array([1, 1, 1, 0, 1, 0, 0])) 

339 4 

340 

341 # n = 7 and 5 true bits 

342 >>> ising1d(np.array([1, 0, 1, 1, 1, 0, 1])) 

343 4 

344 

345 # n = 7 and 5 true bits 

346 >>> ising1d(np.array([0, 1, 1, 1, 0, 1, 1])) 

347 4 

348 

349 # n = 7 and 5 true bits 

350 >>> ising1d(np.array([1, 1, 1, 0, 1, 1, 0])) 

351 4 

352 

353 # n = 7 and 6 true bits 

354 >>> ising1d(np.array([1, 1, 1, 0, 1, 1, 1])) 

355 2 

356 

357 # n = 7 and 6 true bits 

358 >>> ising1d(np.array([1, 1, 1, 1, 1, 1, 0])) 

359 2 

360 

361 # n = 7 and 6 true bits 

362 >>> ising1d(np.array([0, 1, 1, 1, 1, 1, 1])) 

363 2 

364 

365 # n = 7 and 7 true bits 

366 >>> ising1d(np.array([1, 1, 1, 1, 1, 1, 1])) 

367 0 

368 

369 # n = 8 and 0 true bits 

370 >>> ising1d(np.array([0, 0, 0, 0, 0, 0, 0, 0])) 

371 0 

372 

373 # n = 8 and 1 true bit 

374 >>> ising1d(np.array([0, 0, 1, 0, 0, 0, 0, 0])) 

375 2 

376 

377 # n = 8 and 1 true bit 

378 >>> ising1d(np.array([0, 0, 1, 0, 0, 0, 0, 0])) 

379 2 

380 

381 # n = 8 and 1 true bit 

382 >>> ising1d(np.array([0, 0, 0, 0, 0, 1, 0, 0])) 

383 2 

384 

385 # n = 8 and 2 true bits 

386 >>> ising1d(np.array([1, 0, 1, 0, 0, 0, 0, 0])) 

387 4 

388 

389 # n = 8 and 2 true bits 

390 >>> ising1d(np.array([0, 0, 0, 0, 1, 1, 0, 0])) 

391 2 

392 

393 # n = 8 and 2 true bits 

394 >>> ising1d(np.array([0, 0, 0, 1, 0, 0, 0, 1])) 

395 4 

396 

397 # n = 8 and 3 true bits 

398 >>> ising1d(np.array([0, 0, 1, 1, 1, 0, 0, 0])) 

399 2 

400 

401 # n = 8 and 3 true bits 

402 >>> ising1d(np.array([1, 0, 1, 0, 0, 0, 1, 0])) 

403 6 

404 

405 # n = 8 and 3 true bits 

406 >>> ising1d(np.array([0, 1, 0, 1, 0, 0, 1, 0])) 

407 6 

408 

409 # n = 8 and 4 true bits 

410 >>> ising1d(np.array([0, 1, 0, 1, 1, 0, 0, 1])) 

411 6 

412 

413 # n = 8 and 4 true bits 

414 >>> ising1d(np.array([1, 0, 1, 0, 1, 0, 1, 0])) 

415 8 

416 

417 # n = 8 and 4 true bits 

418 >>> ising1d(np.array([1, 0, 1, 0, 1, 0, 0, 1])) 

419 6 

420 

421 # n = 8 and 5 true bits 

422 >>> ising1d(np.array([1, 0, 1, 0, 0, 1, 1, 1])) 

423 4 

424 

425 # n = 8 and 5 true bits 

426 >>> ising1d(np.array([1, 1, 0, 1, 0, 0, 1, 1])) 

427 4 

428 

429 # n = 8 and 5 true bits 

430 >>> ising1d(np.array([1, 0, 0, 1, 0, 1, 1, 1])) 

431 4 

432 

433 # n = 8 and 6 true bits 

434 >>> ising1d(np.array([0, 1, 1, 1, 0, 1, 1, 1])) 

435 4 

436 

437 # n = 8 and 6 true bits 

438 >>> ising1d(np.array([0, 0, 1, 1, 1, 1, 1, 1])) 

439 2 

440 

441 # n = 8 and 6 true bits 

442 >>> ising1d(np.array([1, 1, 1, 1, 1, 1, 0, 0])) 

443 2 

444 

445 # n = 8 and 7 true bits 

446 >>> ising1d(np.array([1, 1, 1, 0, 1, 1, 1, 1])) 

447 2 

448 

449 # n = 8 and 7 true bits 

450 >>> ising1d(np.array([1, 1, 1, 1, 1, 1, 0, 1])) 

451 2 

452 

453 # n = 8 and 7 true bits 

454 >>> ising1d(np.array([1, 1, 1, 1, 1, 1, 0, 1])) 

455 2 

456 

457 # n = 8 and 8 true bits 

458 >>> ising1d(np.array([1, 1, 1, 1, 1, 1, 1, 1])) 

459 0 

460 

461 # n = 9 and 0 true bits 

462 >>> ising1d(np.array([0, 0, 0, 0, 0, 0, 0, 0, 0])) 

463 0 

464 

465 # n = 9 and 1 true bit 

466 >>> ising1d(np.array([0, 0, 0, 0, 0, 0, 0, 1, 0])) 

467 2 

468 

469 # n = 9 and 1 true bit 

470 >>> ising1d(np.array([0, 0, 0, 0, 0, 1, 0, 0, 0])) 

471 2 

472 

473 # n = 9 and 1 true bit 

474 >>> ising1d(np.array([0, 1, 0, 0, 0, 0, 0, 0, 0])) 

475 2 

476 

477 # n = 9 and 2 true bits 

478 >>> ising1d(np.array([0, 1, 0, 1, 0, 0, 0, 0, 0])) 

479 4 

480 

481 # n = 9 and 2 true bits 

482 >>> ising1d(np.array([0, 1, 0, 0, 1, 0, 0, 0, 0])) 

483 4 

484 

485 # n = 9 and 2 true bits 

486 >>> ising1d(np.array([0, 1, 0, 1, 0, 0, 0, 0, 0])) 

487 4 

488 

489 # n = 9 and 3 true bits 

490 >>> ising1d(np.array([0, 0, 1, 0, 0, 1, 0, 1, 0])) 

491 6 

492 

493 # n = 9 and 3 true bits 

494 >>> ising1d(np.array([0, 0, 0, 1, 1, 0, 0, 1, 0])) 

495 4 

496 

497 # n = 9 and 3 true bits 

498 >>> ising1d(np.array([0, 0, 0, 1, 0, 0, 0, 1, 1])) 

499 4 

500 

501 # n = 9 and 4 true bits 

502 >>> ising1d(np.array([1, 0, 1, 1, 1, 0, 0, 0, 0])) 

503 4 

504 

505 # n = 9 and 4 true bits 

506 >>> ising1d(np.array([1, 1, 1, 0, 1, 0, 0, 0, 0])) 

507 4 

508 

509 # n = 9 and 4 true bits 

510 >>> ising1d(np.array([0, 1, 0, 0, 0, 1, 1, 0, 1])) 

511 6 

512 

513 # n = 9 and 5 true bits 

514 >>> ising1d(np.array([0, 0, 1, 1, 0, 0, 1, 1, 1])) 

515 4 

516 

517 # n = 9 and 5 true bits 

518 >>> ising1d(np.array([0, 1, 0, 1, 1, 0, 1, 0, 1])) 

519 8 

520 

521 # n = 9 and 5 true bits 

522 >>> ising1d(np.array([1, 0, 1, 1, 1, 0, 0, 1, 0])) 

523 6 

524 

525 # n = 9 and 6 true bits 

526 >>> ising1d(np.array([1, 0, 1, 1, 1, 1, 1, 0, 0])) 

527 4 

528 

529 # n = 9 and 6 true bits 

530 >>> ising1d(np.array([0, 0, 1, 1, 1, 1, 1, 1, 0])) 

531 2 

532 

533 # n = 9 and 6 true bits 

534 >>> ising1d(np.array([1, 1, 1, 1, 0, 1, 0, 0, 1])) 

535 4 

536 

537 # n = 9 and 7 true bits 

538 >>> ising1d(np.array([1, 1, 0, 1, 1, 0, 1, 1, 1])) 

539 4 

540 

541 # n = 9 and 7 true bits 

542 >>> ising1d(np.array([1, 1, 0, 1, 1, 1, 1, 1, 0])) 

543 4 

544 

545 # n = 9 and 7 true bits 

546 >>> ising1d(np.array([1, 0, 1, 0, 1, 1, 1, 1, 1])) 

547 4 

548 

549 # n = 9 and 8 true bits 

550 >>> ising1d(np.array([1, 0, 1, 1, 1, 1, 1, 1, 1])) 

551 2 

552 

553 # n = 9 and 8 true bits 

554 >>> ising1d(np.array([1, 1, 1, 1, 1, 1, 0, 1, 1])) 

555 2 

556 

557 # n = 9 and 8 true bits 

558 >>> ising1d(np.array([1, 1, 1, 1, 1, 0, 1, 1, 1])) 

559 2 

560 

561 # n = 9 and 9 true bits 

562 >>> ising1d(np.array([1, 1, 1, 1, 1, 1, 1, 1, 1])) 

563 0 

564 

565 # n = 10 and 0 true bits 

566 >>> ising1d(np.array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0])) 

567 0 

568 

569 # n = 10 and 1 true bit 

570 >>> ising1d(np.array([0, 0, 0, 0, 0, 0, 0, 0, 0, 1])) 

571 2 

572 

573 # n = 10 and 1 true bit 

574 >>> ising1d(np.array([0, 0, 0, 1, 0, 0, 0, 0, 0, 0])) 

575 2 

576 

577 # n = 10 and 1 true bit 

578 >>> ising1d(np.array([1, 0, 0, 0, 0, 0, 0, 0, 0, 0])) 

579 2 

580 

581 # n = 10 and 2 true bits 

582 >>> ising1d(np.array([0, 1, 0, 0, 0, 0, 0, 0, 1, 0])) 

583 4 

584 

585 # n = 10 and 2 true bits 

586 >>> ising1d(np.array([1, 0, 0, 0, 0, 0, 0, 0, 0, 1])) 

587 2 

588 

589 # n = 10 and 2 true bits 

590 >>> ising1d(np.array([0, 0, 0, 0, 1, 0, 0, 0, 0, 1])) 

591 4 

592 

593 # n = 10 and 3 true bits 

594 >>> ising1d(np.array([1, 1, 1, 0, 0, 0, 0, 0, 0, 0])) 

595 2 

596 

597 # n = 10 and 3 true bits 

598 >>> ising1d(np.array([0, 0, 1, 0, 0, 0, 1, 0, 1, 0])) 

599 6 

600 

601 # n = 10 and 3 true bits 

602 >>> ising1d(np.array([0, 1, 0, 0, 0, 0, 1, 0, 0, 1])) 

603 6 

604 

605 # n = 10 and 4 true bits 

606 >>> ising1d(np.array([1, 0, 0, 1, 1, 0, 0, 0, 1, 0])) 

607 6 

608 

609 # n = 10 and 4 true bits 

610 >>> ising1d(np.array([0, 0, 0, 1, 1, 0, 1, 0, 0, 1])) 

611 6 

612 

613 # n = 10 and 4 true bits 

614 >>> ising1d(np.array([1, 1, 0, 0, 0, 0, 0, 1, 0, 1])) 

615 4 

616 

617 # n = 10 and 5 true bits 

618 >>> ising1d(np.array([1, 0, 0, 0, 1, 1, 1, 0, 1, 0])) 

619 6 

620 

621 # n = 10 and 5 true bits 

622 >>> ising1d(np.array([1, 1, 0, 0, 1, 0, 1, 0, 1, 0])) 

623 8 

624 

625 # n = 10 and 5 true bits 

626 >>> ising1d(np.array([0, 0, 1, 0, 0, 1, 0, 1, 1, 1])) 

627 6 

628 

629 # n = 10 and 6 true bits 

630 >>> ising1d(np.array([1, 1, 1, 0, 0, 1, 1, 0, 0, 1])) 

631 4 

632 

633 # n = 10 and 6 true bits 

634 >>> ising1d(np.array([1, 1, 0, 1, 0, 0, 1, 1, 1, 0])) 

635 6 

636 

637 # n = 10 and 6 true bits 

638 >>> ising1d(np.array([1, 0, 0, 0, 1, 1, 1, 0, 1, 1])) 

639 4 

640 

641 # n = 10 and 7 true bits 

642 >>> ising1d(np.array([1, 0, 0, 0, 1, 1, 1, 1, 1, 1])) 

643 2 

644 

645 # n = 10 and 7 true bits 

646 >>> ising1d(np.array([1, 0, 0, 1, 1, 1, 1, 1, 1, 0])) 

647 4 

648 

649 # n = 10 and 7 true bits 

650 >>> ising1d(np.array([0, 1, 1, 1, 0, 1, 0, 1, 1, 1])) 

651 6 

652 

653 # n = 10 and 8 true bits 

654 >>> ising1d(np.array([1, 0, 1, 1, 0, 1, 1, 1, 1, 1])) 

655 4 

656 

657 # n = 10 and 8 true bits 

658 >>> ising1d(np.array([1, 1, 1, 1, 1, 1, 1, 0, 1, 0])) 

659 4 

660 

661 # n = 10 and 8 true bits 

662 >>> ising1d(np.array([0, 1, 1, 1, 1, 1, 1, 0, 1, 1])) 

663 4 

664 

665 # n = 10 and 9 true bits 

666 >>> ising1d(np.array([1, 1, 1, 1, 1, 1, 1, 1, 0, 1])) 

667 2 

668 

669 # n = 10 and 9 true bits 

670 >>> ising1d(np.array([1, 1, 1, 1, 1, 0, 1, 1, 1, 1])) 

671 2 

672 

673 # n = 10 and 9 true bits 

674 >>> ising1d(np.array([1, 0, 1, 1, 1, 1, 1, 1, 1, 1])) 

675 2 

676 

677 # n = 10 and 10 true bits 

678 >>> ising1d(np.array([1, 1, 1, 1, 1, 1, 1, 1, 1, 1])) 

679 0 

680 """ 

681 n: int = 0 

682 prev: bool = x[-1] 

683 for cur in x: 

684 if cur != prev: 

685 n += 1 

686 prev = cur 

687 return n 

688 

689 

690class Ising1d(BitStringProblem): 

691 """The one-dimensional Ising problem.""" 

692 

693 def __init__(self, n: int) -> None: 

694 """ 

695 Initialize the one-dimensional Ising problem. 

696 

697 :param n: the dimension of the problem 

698 

699 >>> Ising1d(7).n 

700 7 

701 >>> Ising1d(3).evaluate(np.array([True, False, True])) 

702 2 

703 """ 

704 super().__init__(n) 

705 self.evaluate = ising1d # type: ignore 

706 

707 def __str__(self) -> str: 

708 """ 

709 Get the name of the one-dimensional Ising problem. 

710 

711 :return: `ising1d_` + length of string 

712 

713 >>> Ising1d(5) 

714 ising1d_5 

715 """ 

716 return f"ising1d_{self.n}" 

717 

718 @classmethod 

719 def default_instances( 

720 cls: type, scale_min: int = 2, scale_max: int = 100) \ 

721 -> Iterator[Callable[[], "Ising1d"]]: 

722 """ 

723 Get the 56 default instances of the :class:`Ising1d` problem. 

724 

725 :param scale_min: the minimum permitted scale, by default `2` 

726 :param scale_max: the maximum permitted scale, by default `100` 

727 :returns: a sequence of default :class:`Ising1d` instances 

728 

729 >>> len(list(Ising1d.default_instances())) 

730 56 

731 

732 >>> [x() for x in Ising1d.default_instances()] 

733 [ising1d_2, ising1d_3, ising1d_4, ising1d_5, ising1d_6, ising1d_7, \ 

734ising1d_8, ising1d_9, ising1d_10, ising1d_11, ising1d_12, ising1d_13, \ 

735ising1d_14, ising1d_15, ising1d_16, ising1d_17, ising1d_18, ising1d_19, \ 

736ising1d_20, ising1d_21, ising1d_22, ising1d_23, ising1d_24, ising1d_25, \ 

737ising1d_26, ising1d_27, ising1d_28, ising1d_29, ising1d_30, ising1d_31, \ 

738ising1d_32, ising1d_33, ising1d_36, ising1d_40, ising1d_41, ising1d_42, \ 

739ising1d_44, ising1d_48, ising1d_49, ising1d_50, ising1d_55, ising1d_59, \ 

740ising1d_60, ising1d_64, ising1d_66, ising1d_70, ising1d_77, ising1d_79, \ 

741ising1d_80, ising1d_81, ising1d_85, ising1d_88, ising1d_90, ising1d_96, \ 

742ising1d_99, ising1d_100] 

743 """ 

744 return cast("Iterator[Callable[[], Ising1d]]", 

745 super().default_instances( # type: ignore 

746 scale_min, scale_max))