Optimization¶
This page documents 3 public symbols. Each entry includes its purpose, import path, full API docstring, and the maintained example that exercises it.
OptimizationResult¶
Result returned by a one-dimensional optimizer.
| Property | Value |
|---|---|
| Type | class |
| Import | from pygwrx.core import OptimizationResult |
| Signature | OptimizationResult(value: 'Union[float, int]', score: 'float', iterations: 'int', converged: 'bool', evaluations: 'int' = 0, message: 'str' = '') -> None |
| Maintained example | examples/core/06_optimization.py |
OptimizationResult
dataclass
¶
OptimizationResult(
value: Union[float, int],
score: float,
iterations: int,
converged: bool,
evaluations: int = 0,
message: str = "",
)
Result returned by a one-dimensional optimizer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
Union[float, int]
|
Best parameter value found. |
required |
score
|
float
|
Objective-function value at |
required |
iterations
|
int
|
Number of optimization updates performed. |
required |
converged
|
bool
|
Whether the stopping criterion was satisfied and a finite solution was found. |
required |
evaluations
|
int
|
Number of unique objective-function evaluations. |
0
|
message
|
str
|
Human-readable termination message. |
''
|
Notes
The first four fields are retained for backward compatibility with the
original project implementation. evaluations and message are
additive metadata fields.
GoldenSectionSearch¶
Golden-section search for one-dimensional minimization.
| Property | Value |
|---|---|
| Type | class |
| Import | from pygwrx.core import GoldenSectionSearch |
| Signature | GoldenSectionSearch(tol: 'float' = 1e-05, max_iter: 'int' = 100, verbose: 'bool' = True) |
| Maintained example | examples/core/06_optimization.py |
GoldenSectionSearch ¶
Golden-section search for one-dimensional minimization.
Continuous searches use the standard golden-section interval reduction.
Adaptive bandwidth searches use a discrete integer variant and finish by
evaluating every integer in the final short bracket. Unlike the original
implementation, convergence is controlled by tol rather than by a
hard-coded constant or by equality of objective values.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tol
|
float
|
Positive convergence tolerance for the search interval. |
1e-05
|
max_iter
|
int
|
Maximum number of interval-reduction updates. |
100
|
verbose
|
bool
|
Whether to print progress information. |
True
|
Source code in src/pygwrx/core/optimization.py
minimize ¶
minimize(
func: Callable[[float], float],
lower: float,
upper: float,
adaptive: bool = False,
) -> OptimizationResult
Minimize a scalar objective on a closed interval.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
func
|
Callable[[float], float]
|
Scalar objective function. Lower scores are better. |
required |
lower
|
float
|
Finite lower search bound. |
required |
upper
|
float
|
Finite upper search bound with |
required |
adaptive
|
bool
|
If |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
OptimizationResult |
OptimizationResult
|
Best candidate, objective score, convergence state and metadata. |
Source code in src/pygwrx/core/optimization.py
auto_bounds
staticmethod
¶
Derive safe default bandwidth-search bounds from coordinates.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
coords
|
ndarray
|
Finite numeric coordinates. |
required |
adaptive
|
bool
|
Whether the bandwidth represents an integer neighbour count. |
required |
bandwidth_type
|
str
|
Retained for API compatibility. Both values currently use the same robust bounds; unsupported values are rejected rather than ignored. |
'gwr'
|
Returns:
| Type | Description |
|---|---|
Tuple[float, float]
|
(lower, upper) : tuple of float: Valid ordered search bounds. |
Notes
Adaptive bounds use [20, n] for datasets with at least 40 samples
and a 5%-based lower bound (at least 2) for smaller datasets. Fixed bounds
are based on positive
observed pairwise distances, avoiding zero-width bounds for repeated or
degenerate coordinates.
Source code in src/pygwrx/core/optimization.py
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 | |
BrentSearch¶
Brent's bounded method for continuous one-dimensional minimization.
| Property | Value |
|---|---|
| Type | class |
| Import | from pygwrx.core import BrentSearch |
| Signature | BrentSearch(tol: 'float' = 1e-05, max_iter: 'int' = 100, verbose: 'bool' = True) |
| Maintained example | examples/core/06_optimization.py |
BrentSearch ¶
Brent's bounded method for continuous one-dimensional minimization.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tol
|
float
|
Positive relative/absolute convergence tolerance. |
1e-05
|
max_iter
|
int
|
Maximum number of optimization updates. |
100
|
verbose
|
bool
|
Whether to print progress information. |
True
|
Notes
Brent's method is a continuous optimizer. Adaptive integer bandwidths
should normally use GoldenSectionSearch(..., adaptive=True) or an
explicit integer post-processing step in the bandwidth selector.
Source code in src/pygwrx/core/optimization.py
minimize ¶
Minimize a scalar objective on the closed interval [lower, upper].
Source code in src/pygwrx/core/optimization.py
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 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 | |
Runnable examples used on this page¶
examples/core/06_optimization.py
# SPDX-FileCopyrightText: 2026 Jinghao Hu
# SPDX-License-Identifier: MIT
"""Use both public scalar optimizers and the OptimizationResult container."""
# Allow this script to run directly from any working directory.
import sys
from pathlib import Path
_PROJECT_ROOT = Path(__file__).resolve().parents[2]
_EXAMPLES_ROOT = _PROJECT_ROOT / "examples"
_SRC_ROOT = _PROJECT_ROOT / "src"
for _path in (_SRC_ROOT, _EXAMPLES_ROOT):
if str(_path) not in sys.path:
sys.path.insert(0, str(_path))
from pygwrx.core import BrentSearch, GoldenSectionSearch, OptimizationResult
def objective(x):
"""Simple convex objective with a known minimum."""
return (x - 2.5) ** 2 + 1.0
golden = GoldenSectionSearch(tol=1e-7, max_iter=100, verbose=False)
brent = BrentSearch(tol=1e-7, max_iter=100, verbose=False)
print("golden=", golden.minimize(objective, 0.0, 5.0))
print("brent=", brent.minimize(objective, 0.0, 5.0))
print("manual_result=", OptimizationResult(2.5, 1.0, 10, True, evaluations=12))