gggp 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- gggp-0.1.0/.github/workflows/ci.yml +36 -0
- gggp-0.1.0/.github/workflows/publish.yml +48 -0
- gggp-0.1.0/.gitignore +13 -0
- gggp-0.1.0/Cargo.lock +180 -0
- gggp-0.1.0/Cargo.toml +16 -0
- gggp-0.1.0/LICENSE +22 -0
- gggp-0.1.0/PKG-INFO +53 -0
- gggp-0.1.0/README.md +38 -0
- gggp-0.1.0/pyproject.toml +24 -0
- gggp-0.1.0/python/gggp/__init__.py +4 -0
- gggp-0.1.0/src/lib.rs +58 -0
- gggp-0.1.0/tests/test_core.py +21 -0
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
name: CI
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
push:
|
|
5
|
+
branches: ["main"]
|
|
6
|
+
pull_request:
|
|
7
|
+
|
|
8
|
+
jobs:
|
|
9
|
+
build-test:
|
|
10
|
+
runs-on: ubuntu-latest
|
|
11
|
+
steps:
|
|
12
|
+
- name: Checkout
|
|
13
|
+
uses: actions/checkout@v4
|
|
14
|
+
|
|
15
|
+
- name: Set up Python
|
|
16
|
+
uses: actions/setup-python@v5
|
|
17
|
+
with:
|
|
18
|
+
python-version: "3.11"
|
|
19
|
+
|
|
20
|
+
- name: Set up Rust
|
|
21
|
+
uses: dtolnay/rust-toolchain@stable
|
|
22
|
+
|
|
23
|
+
- name: Install build tools
|
|
24
|
+
run: |
|
|
25
|
+
python -m pip install --upgrade pip
|
|
26
|
+
python -m pip install maturin pytest
|
|
27
|
+
|
|
28
|
+
- name: Build wheel
|
|
29
|
+
run: maturin build --release --interpreter python3.11 -o dist
|
|
30
|
+
|
|
31
|
+
- name: Install built wheel
|
|
32
|
+
run: python -m pip install dist/*.whl
|
|
33
|
+
|
|
34
|
+
- name: Run tests
|
|
35
|
+
run: pytest -q
|
|
36
|
+
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
name: Publish
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
push:
|
|
5
|
+
tags:
|
|
6
|
+
- "v*"
|
|
7
|
+
|
|
8
|
+
jobs:
|
|
9
|
+
build:
|
|
10
|
+
runs-on: ubuntu-latest
|
|
11
|
+
steps:
|
|
12
|
+
- name: Checkout
|
|
13
|
+
uses: actions/checkout@v4
|
|
14
|
+
|
|
15
|
+
- name: Build wheels and sdist
|
|
16
|
+
uses: PyO3/maturin-action@v1
|
|
17
|
+
with:
|
|
18
|
+
command: build
|
|
19
|
+
args: --release --out dist
|
|
20
|
+
|
|
21
|
+
- name: Build source distribution
|
|
22
|
+
run: |
|
|
23
|
+
python -m pip install --upgrade pip maturin
|
|
24
|
+
maturin sdist --out dist
|
|
25
|
+
|
|
26
|
+
- name: Upload dist artifacts
|
|
27
|
+
uses: actions/upload-artifact@v4
|
|
28
|
+
with:
|
|
29
|
+
name: dist
|
|
30
|
+
path: dist/*
|
|
31
|
+
|
|
32
|
+
publish:
|
|
33
|
+
needs: build
|
|
34
|
+
runs-on: ubuntu-latest
|
|
35
|
+
permissions:
|
|
36
|
+
id-token: write
|
|
37
|
+
steps:
|
|
38
|
+
- name: Download dist artifacts
|
|
39
|
+
uses: actions/download-artifact@v4
|
|
40
|
+
with:
|
|
41
|
+
name: dist
|
|
42
|
+
path: dist
|
|
43
|
+
|
|
44
|
+
- name: Publish to PyPI
|
|
45
|
+
uses: pypa/gh-action-pypi-publish@release/v1
|
|
46
|
+
with:
|
|
47
|
+
packages-dir: dist
|
|
48
|
+
|
gggp-0.1.0/.gitignore
ADDED
gggp-0.1.0/Cargo.lock
ADDED
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
# This file is automatically @generated by Cargo.
|
|
2
|
+
# It is not intended for manual editing.
|
|
3
|
+
version = 4
|
|
4
|
+
|
|
5
|
+
[[package]]
|
|
6
|
+
name = "autocfg"
|
|
7
|
+
version = "1.5.0"
|
|
8
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
9
|
+
checksum = "c08606f8c3cbf4ce6ec8e28fb0014a2c086708fe954eaa885384a6165172e7e8"
|
|
10
|
+
|
|
11
|
+
[[package]]
|
|
12
|
+
name = "cfg-if"
|
|
13
|
+
version = "1.0.4"
|
|
14
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
15
|
+
checksum = "9330f8b2ff13f34540b44e946ef35111825727b38d33286ef986142615121801"
|
|
16
|
+
|
|
17
|
+
[[package]]
|
|
18
|
+
name = "gggp-core-py"
|
|
19
|
+
version = "0.1.0"
|
|
20
|
+
dependencies = [
|
|
21
|
+
"pyo3",
|
|
22
|
+
]
|
|
23
|
+
|
|
24
|
+
[[package]]
|
|
25
|
+
name = "heck"
|
|
26
|
+
version = "0.5.0"
|
|
27
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
28
|
+
checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea"
|
|
29
|
+
|
|
30
|
+
[[package]]
|
|
31
|
+
name = "indoc"
|
|
32
|
+
version = "2.0.7"
|
|
33
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
34
|
+
checksum = "79cf5c93f93228cf8efb3ba362535fb11199ac548a09ce117c9b1adc3030d706"
|
|
35
|
+
dependencies = [
|
|
36
|
+
"rustversion",
|
|
37
|
+
]
|
|
38
|
+
|
|
39
|
+
[[package]]
|
|
40
|
+
name = "libc"
|
|
41
|
+
version = "0.2.182"
|
|
42
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
43
|
+
checksum = "6800badb6cb2082ffd7b6a67e6125bb39f18782f793520caee8cb8846be06112"
|
|
44
|
+
|
|
45
|
+
[[package]]
|
|
46
|
+
name = "memoffset"
|
|
47
|
+
version = "0.9.1"
|
|
48
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
49
|
+
checksum = "488016bfae457b036d996092f6cb448677611ce4449e970ceaf42695203f218a"
|
|
50
|
+
dependencies = [
|
|
51
|
+
"autocfg",
|
|
52
|
+
]
|
|
53
|
+
|
|
54
|
+
[[package]]
|
|
55
|
+
name = "once_cell"
|
|
56
|
+
version = "1.21.3"
|
|
57
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
58
|
+
checksum = "42f5e15c9953c5e4ccceeb2e7382a716482c34515315f7b03532b8b4e8393d2d"
|
|
59
|
+
|
|
60
|
+
[[package]]
|
|
61
|
+
name = "portable-atomic"
|
|
62
|
+
version = "1.13.1"
|
|
63
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
64
|
+
checksum = "c33a9471896f1c69cecef8d20cbe2f7accd12527ce60845ff44c153bb2a21b49"
|
|
65
|
+
|
|
66
|
+
[[package]]
|
|
67
|
+
name = "proc-macro2"
|
|
68
|
+
version = "1.0.106"
|
|
69
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
70
|
+
checksum = "8fd00f0bb2e90d81d1044c2b32617f68fcb9fa3bb7640c23e9c748e53fb30934"
|
|
71
|
+
dependencies = [
|
|
72
|
+
"unicode-ident",
|
|
73
|
+
]
|
|
74
|
+
|
|
75
|
+
[[package]]
|
|
76
|
+
name = "pyo3"
|
|
77
|
+
version = "0.22.6"
|
|
78
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
79
|
+
checksum = "f402062616ab18202ae8319da13fa4279883a2b8a9d9f83f20dbade813ce1884"
|
|
80
|
+
dependencies = [
|
|
81
|
+
"cfg-if",
|
|
82
|
+
"indoc",
|
|
83
|
+
"libc",
|
|
84
|
+
"memoffset",
|
|
85
|
+
"once_cell",
|
|
86
|
+
"portable-atomic",
|
|
87
|
+
"pyo3-build-config",
|
|
88
|
+
"pyo3-ffi",
|
|
89
|
+
"pyo3-macros",
|
|
90
|
+
"unindent",
|
|
91
|
+
]
|
|
92
|
+
|
|
93
|
+
[[package]]
|
|
94
|
+
name = "pyo3-build-config"
|
|
95
|
+
version = "0.22.6"
|
|
96
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
97
|
+
checksum = "b14b5775b5ff446dd1056212d778012cbe8a0fbffd368029fd9e25b514479c38"
|
|
98
|
+
dependencies = [
|
|
99
|
+
"once_cell",
|
|
100
|
+
"target-lexicon",
|
|
101
|
+
]
|
|
102
|
+
|
|
103
|
+
[[package]]
|
|
104
|
+
name = "pyo3-ffi"
|
|
105
|
+
version = "0.22.6"
|
|
106
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
107
|
+
checksum = "9ab5bcf04a2cdcbb50c7d6105de943f543f9ed92af55818fd17b660390fc8636"
|
|
108
|
+
dependencies = [
|
|
109
|
+
"libc",
|
|
110
|
+
"pyo3-build-config",
|
|
111
|
+
]
|
|
112
|
+
|
|
113
|
+
[[package]]
|
|
114
|
+
name = "pyo3-macros"
|
|
115
|
+
version = "0.22.6"
|
|
116
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
117
|
+
checksum = "0fd24d897903a9e6d80b968368a34e1525aeb719d568dba8b3d4bfa5dc67d453"
|
|
118
|
+
dependencies = [
|
|
119
|
+
"proc-macro2",
|
|
120
|
+
"pyo3-macros-backend",
|
|
121
|
+
"quote",
|
|
122
|
+
"syn",
|
|
123
|
+
]
|
|
124
|
+
|
|
125
|
+
[[package]]
|
|
126
|
+
name = "pyo3-macros-backend"
|
|
127
|
+
version = "0.22.6"
|
|
128
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
129
|
+
checksum = "36c011a03ba1e50152b4b394b479826cad97e7a21eb52df179cd91ac411cbfbe"
|
|
130
|
+
dependencies = [
|
|
131
|
+
"heck",
|
|
132
|
+
"proc-macro2",
|
|
133
|
+
"pyo3-build-config",
|
|
134
|
+
"quote",
|
|
135
|
+
"syn",
|
|
136
|
+
]
|
|
137
|
+
|
|
138
|
+
[[package]]
|
|
139
|
+
name = "quote"
|
|
140
|
+
version = "1.0.44"
|
|
141
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
142
|
+
checksum = "21b2ebcf727b7760c461f091f9f0f539b77b8e87f2fd88131e7f1b433b3cece4"
|
|
143
|
+
dependencies = [
|
|
144
|
+
"proc-macro2",
|
|
145
|
+
]
|
|
146
|
+
|
|
147
|
+
[[package]]
|
|
148
|
+
name = "rustversion"
|
|
149
|
+
version = "1.0.22"
|
|
150
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
151
|
+
checksum = "b39cdef0fa800fc44525c84ccb54a029961a8215f9619753635a9c0d2538d46d"
|
|
152
|
+
|
|
153
|
+
[[package]]
|
|
154
|
+
name = "syn"
|
|
155
|
+
version = "2.0.115"
|
|
156
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
157
|
+
checksum = "6e614ed320ac28113fa64972c4262d5dbc89deacdfd00c34a3e4cea073243c12"
|
|
158
|
+
dependencies = [
|
|
159
|
+
"proc-macro2",
|
|
160
|
+
"quote",
|
|
161
|
+
"unicode-ident",
|
|
162
|
+
]
|
|
163
|
+
|
|
164
|
+
[[package]]
|
|
165
|
+
name = "target-lexicon"
|
|
166
|
+
version = "0.12.16"
|
|
167
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
168
|
+
checksum = "61c41af27dd6d1e27b1b16b489db798443478cef1f06a660c96db617ba5de3b1"
|
|
169
|
+
|
|
170
|
+
[[package]]
|
|
171
|
+
name = "unicode-ident"
|
|
172
|
+
version = "1.0.23"
|
|
173
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
174
|
+
checksum = "537dd038a89878be9b64dd4bd1b260315c1bb94f4d784956b81e27a088d9a09e"
|
|
175
|
+
|
|
176
|
+
[[package]]
|
|
177
|
+
name = "unindent"
|
|
178
|
+
version = "0.2.4"
|
|
179
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
180
|
+
checksum = "7264e107f553ccae879d21fbea1d6724ac785e8c3bfc762137959b5802826ef3"
|
gggp-0.1.0/Cargo.toml
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
[package]
|
|
2
|
+
name = "gggp-core-py"
|
|
3
|
+
version = "0.1.0"
|
|
4
|
+
edition = "2021"
|
|
5
|
+
license = "MIT"
|
|
6
|
+
repository = "https://github.com/olkhovoy/gggp"
|
|
7
|
+
description = "Minimal GGGP core Python extension"
|
|
8
|
+
readme = "README.md"
|
|
9
|
+
|
|
10
|
+
[lib]
|
|
11
|
+
name = "gggp_core_py"
|
|
12
|
+
crate-type = ["cdylib"]
|
|
13
|
+
|
|
14
|
+
[dependencies]
|
|
15
|
+
pyo3 = { version = "0.22", features = ["extension-module"] }
|
|
16
|
+
|
gggp-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Alexander Olkhovoy
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
22
|
+
|
gggp-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: gggp
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Classifier: Programming Language :: Python :: 3
|
|
5
|
+
Classifier: Programming Language :: Rust
|
|
6
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
7
|
+
Classifier: Operating System :: OS Independent
|
|
8
|
+
License-File: LICENSE
|
|
9
|
+
Summary: Rust-powered GGGP core with Python bindings
|
|
10
|
+
Author: Alexander Olkhovoy
|
|
11
|
+
License: MIT
|
|
12
|
+
Requires-Python: >=3.9
|
|
13
|
+
Description-Content-Type: text/markdown; charset=UTF-8; variant=GFM
|
|
14
|
+
|
|
15
|
+
# gggp
|
|
16
|
+
|
|
17
|
+
Minimal Rust Library + Python SDK foundation for GGGP.
|
|
18
|
+
|
|
19
|
+
This repository currently provides a small Python extension module (built from Rust via `pyo3` + `maturin`) to establish release and CI/CD workflows.
|
|
20
|
+
|
|
21
|
+
## Install (local)
|
|
22
|
+
|
|
23
|
+
```bash
|
|
24
|
+
python -m pip install -U pip maturin
|
|
25
|
+
maturin build --release -o dist
|
|
26
|
+
python -m pip install dist/*.whl
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
## Python API
|
|
30
|
+
|
|
31
|
+
```python
|
|
32
|
+
import gggp
|
|
33
|
+
|
|
34
|
+
print(gggp.version())
|
|
35
|
+
child_a, child_b = gggp.one_point_crossover([1, 2, 3], [10, 20, 30], split=1)
|
|
36
|
+
mutated = gggp.mutate_gene([1, 2, 3], index=2, value=99)
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
## Release
|
|
40
|
+
|
|
41
|
+
1. Configure PyPI Trusted Publisher for this repository and workflow file:
|
|
42
|
+
- Repository: `olkhovoy/gggp`
|
|
43
|
+
- Workflow: `.github/workflows/publish.yml`
|
|
44
|
+
2. Create and push a version tag:
|
|
45
|
+
|
|
46
|
+
```bash
|
|
47
|
+
git tag v0.1.0
|
|
48
|
+
git push origin v0.1.0
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
The `publish` workflow will build wheel + sdist and upload to PyPI.
|
|
52
|
+
|
|
53
|
+
|
gggp-0.1.0/README.md
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
# gggp
|
|
2
|
+
|
|
3
|
+
Minimal Rust Library + Python SDK foundation for GGGP.
|
|
4
|
+
|
|
5
|
+
This repository currently provides a small Python extension module (built from Rust via `pyo3` + `maturin`) to establish release and CI/CD workflows.
|
|
6
|
+
|
|
7
|
+
## Install (local)
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
python -m pip install -U pip maturin
|
|
11
|
+
maturin build --release -o dist
|
|
12
|
+
python -m pip install dist/*.whl
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## Python API
|
|
16
|
+
|
|
17
|
+
```python
|
|
18
|
+
import gggp
|
|
19
|
+
|
|
20
|
+
print(gggp.version())
|
|
21
|
+
child_a, child_b = gggp.one_point_crossover([1, 2, 3], [10, 20, 30], split=1)
|
|
22
|
+
mutated = gggp.mutate_gene([1, 2, 3], index=2, value=99)
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
## Release
|
|
26
|
+
|
|
27
|
+
1. Configure PyPI Trusted Publisher for this repository and workflow file:
|
|
28
|
+
- Repository: `olkhovoy/gggp`
|
|
29
|
+
- Workflow: `.github/workflows/publish.yml`
|
|
30
|
+
2. Create and push a version tag:
|
|
31
|
+
|
|
32
|
+
```bash
|
|
33
|
+
git tag v0.1.0
|
|
34
|
+
git push origin v0.1.0
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
The `publish` workflow will build wheel + sdist and upload to PyPI.
|
|
38
|
+
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["maturin>=1.7,<2.0"]
|
|
3
|
+
build-backend = "maturin"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "gggp"
|
|
7
|
+
description = "Rust-powered GGGP core with Python bindings"
|
|
8
|
+
readme = "README.md"
|
|
9
|
+
requires-python = ">=3.9"
|
|
10
|
+
license = { text = "MIT" }
|
|
11
|
+
authors = [{ name = "Alexander Olkhovoy" }]
|
|
12
|
+
classifiers = [
|
|
13
|
+
"Programming Language :: Python :: 3",
|
|
14
|
+
"Programming Language :: Rust",
|
|
15
|
+
"License :: OSI Approved :: MIT License",
|
|
16
|
+
"Operating System :: OS Independent",
|
|
17
|
+
]
|
|
18
|
+
dynamic = ["version"]
|
|
19
|
+
|
|
20
|
+
[tool.maturin]
|
|
21
|
+
bindings = "pyo3"
|
|
22
|
+
python-source = "python"
|
|
23
|
+
module-name = "gggp._core"
|
|
24
|
+
features = ["pyo3/extension-module"]
|
gggp-0.1.0/src/lib.rs
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
use pyo3::exceptions::PyIndexError;
|
|
2
|
+
use pyo3::prelude::*;
|
|
3
|
+
|
|
4
|
+
#[pyfunction]
|
|
5
|
+
fn version() -> &'static str {
|
|
6
|
+
env!("CARGO_PKG_VERSION")
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
#[pyfunction]
|
|
10
|
+
#[pyo3(signature = (chromosome_a, chromosome_b, split=None))]
|
|
11
|
+
fn one_point_crossover(
|
|
12
|
+
chromosome_a: Vec<i64>,
|
|
13
|
+
chromosome_b: Vec<i64>,
|
|
14
|
+
split: Option<usize>,
|
|
15
|
+
) -> PyResult<(Vec<i64>, Vec<i64>)> {
|
|
16
|
+
let min_len = chromosome_a.len().min(chromosome_b.len());
|
|
17
|
+
if min_len == 0 {
|
|
18
|
+
return Ok((chromosome_a, chromosome_b));
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
let cross_at = split.unwrap_or(min_len / 2);
|
|
22
|
+
if cross_at > min_len {
|
|
23
|
+
return Err(PyIndexError::new_err(format!(
|
|
24
|
+
"split={} is out of bounds for min_len={}",
|
|
25
|
+
cross_at, min_len
|
|
26
|
+
)));
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
let mut child_a = chromosome_a.clone();
|
|
30
|
+
let mut child_b = chromosome_b.clone();
|
|
31
|
+
child_a[cross_at..min_len].copy_from_slice(&chromosome_b[cross_at..min_len]);
|
|
32
|
+
child_b[cross_at..min_len].copy_from_slice(&chromosome_a[cross_at..min_len]);
|
|
33
|
+
Ok((child_a, child_b))
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
#[pyfunction]
|
|
37
|
+
#[pyo3(signature = (chromosome, index, value))]
|
|
38
|
+
fn mutate_gene(chromosome: Vec<i64>, index: usize, value: i64) -> PyResult<Vec<i64>> {
|
|
39
|
+
if index >= chromosome.len() {
|
|
40
|
+
return Err(PyIndexError::new_err(format!(
|
|
41
|
+
"index={} is out of bounds for chromosome length={}",
|
|
42
|
+
index,
|
|
43
|
+
chromosome.len()
|
|
44
|
+
)));
|
|
45
|
+
}
|
|
46
|
+
let mut out = chromosome;
|
|
47
|
+
out[index] = value;
|
|
48
|
+
Ok(out)
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
#[pymodule]
|
|
52
|
+
fn _core(_py: Python<'_>, module: &Bound<'_, PyModule>) -> PyResult<()> {
|
|
53
|
+
module.add_function(wrap_pyfunction!(version, module)?)?;
|
|
54
|
+
module.add_function(wrap_pyfunction!(one_point_crossover, module)?)?;
|
|
55
|
+
module.add_function(wrap_pyfunction!(mutate_gene, module)?)?;
|
|
56
|
+
Ok(())
|
|
57
|
+
}
|
|
58
|
+
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import gggp
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
def test_version_is_string():
|
|
5
|
+
v = gggp.version()
|
|
6
|
+
assert isinstance(v, str)
|
|
7
|
+
assert v.count(".") >= 1
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
def test_one_point_crossover():
|
|
11
|
+
a = [1, 2, 3, 4]
|
|
12
|
+
b = [10, 20, 30, 40]
|
|
13
|
+
c1, c2 = gggp.one_point_crossover(a, b, split=2)
|
|
14
|
+
assert c1 == [1, 2, 30, 40]
|
|
15
|
+
assert c2 == [10, 20, 3, 4]
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def test_mutate_gene():
|
|
19
|
+
result = gggp.mutate_gene([1, 2, 3], index=1, value=99)
|
|
20
|
+
assert result == [1, 99, 3]
|
|
21
|
+
|