Skip to content

Commit

Permalink
Merge pull request #5 from tilleyd/feature/nd-optimization
Browse files Browse the repository at this point in the history
Optimize all functions for 2D arrays
  • Loading branch information
tilleyd authored Nov 23, 2022
2 parents 5b47cc3 + 2464a28 commit baf0ba3
Show file tree
Hide file tree
Showing 11 changed files with 616 additions and 445 deletions.
2 changes: 1 addition & 1 deletion LICENSE.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2020 Duncan Tilley
Copyright (c) 2022 Duncan Tilley

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
32 changes: 23 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# CEC 2017 Python

> **⚠ 23 Nov 2022**: Breaking changes were made to all function signatures. See the changelog for details.
Python 3 module containing a native implementation of the CEC 2017 benchmark functions (single objective optimization). The implementation is adapted from Awad's original C implementation, available on [Suganthan's GitHub repo](https://github.com/P-N-Suganthan/CEC2017-BoundContrained), along with the problem definitions [1].

Although there are wrappers for the C code, this module is easier to use, natively supports numpy arrays and is (_much_) more readable than the C implementation.
Expand All @@ -14,9 +16,9 @@ As per the problem definitions, functions are defined for 10, 30, 50 and 100 dim
## Features

- Native implementation of all CEC 2017 single objective functions
- Pre-defined rotations, shifts and shuffles for 2, 10, 20, 30, 50 and 100 dimensions
- Allows custom rotations, shifts and shuffles
- Native implementation of all CEC 2017 single objective functions optimized for multiple simultaneous evaluations
- Pre-defined rotations, shifts, and shuffles for 2, 10, 20, 30, 50 and 100 dimensions
- Allows custom rotations, shifts, and shuffles
- Convenient surface plot utility
- Easy access to basic functions f1 to f19 (e.g. Ackley, Discus, etc.)

Expand All @@ -30,26 +32,38 @@ python3 setup.py install

## Usage

Below is a simple example of executing either a single function or all functions. See [example.py](example.py) for more advanced use cases.
Below is a simple example of executing either a single function or all functions. Note that each function takes a 2D array and returns a 1D array. See [example.py](example.py) for more advanced use cases.

```py
# Using only f5:
from cec2017.functions import f5
x = np.random.uniform(-100, 100, size=50)
samples = 3
dimension = 50
x = np.random.uniform(-100, 100, size=(samples, dimension))
val = f5(x)
print('f5(x) = %.6f' %val)
for i in range(samples):
print(f"f5(x_{i}) = {val[i]:.6f}")

# Using all functions:
from cec2017.functions import all_functions
for f in all_functions:
x = np.random.uniform(-100, 100, size=50)
x = np.random.uniform(-100, 100, size=(samples, dimension))
val = f(x)
print('%s(x) = %.6f' %( f.__name__, val ))
for i in range(samples):
print(f"{f.__name__}(x_{i}) = {val[i]:.6f}")
```

## Changelog

### 23 Nov 2022
- All functions have been reimplemented using numpy vector operations instead of native python loops.
- **Breaking change:** Functions now expect a 2D input array of shape (m, D) where D is the dimensionality, and m is an arbitrary sample size.

The above updates have allowed a substantial increase in performance when evaluating multiple parameter samples simultaneously, which previously would have had to be done as consecutive calls. See [PR 5](https://github.com/tilleyd/cec2017-py/pull/5) for more details.

## License

Copyright © 2020 Duncan Tilley
Copyright © 2022 Duncan Tilley
See the [license notice](LICENSE.txt) for full details.

## Issues
Expand Down
Loading

0 comments on commit baf0ba3

Please sign in to comment.