syhpoon / nsga

Implementation of the multi-objective genetic optimization algorithm NSGA-II
MIT License
11 stars 2 forks source link

Example of how to use #1

Closed s4ksb closed 1 year ago

s4ksb commented 1 year ago

Hi there, I am trying to use your nsga code to solve a problem I have and I was wondering if you may provide an example script on how I can include your crate as a dependency and use it to solve my problem?

Thanks

syhpoon commented 1 year ago

:wave:

The README should have a pretty detailed explanation on the general usage of the library. Additionally, there are test files that have the full code in one place. If you maybe could share some details about the problem you're trying to solve, I'd be happy to provide you with the example of how to use nsga to solve it.

s4ksb commented 1 year ago

Hi, thanks for the quick reply. Atm I just want to use it to solve the Binh and Korn function at https://en.wikipedia.org/wiki/Test_functions_for_optimization. If you could help with this that would be great.

Thanks

syhpoon commented 1 year ago

Here's the example implementation: https://github.com/syhpoon/nsga-binh-korn You can run it with cargo run:

cargo run
   Compiling nsga-binh-korn v0.1.0 (/tmp/nsga-binh-korn)
    Finished dev [unoptimized + debuginfo] target(s) in 0.41s
     Running `target/debug/nsga-binh-korn`
x=2.415322634262468, y=2.263548138334197, f1(x, y)=43.82973440854719, f2(x, y)=14.168725876170146
x=1.6523325984499193, y=1.4505594020374932, f1(x, y)=19.33730237895853, f2(x, y)=23.805405589865508
x=1.014740038327778, y=1.221906831752651, f1(x, y)=10.091014603477046, f2(x, y)=30.156284950064972
x=0.23952461943139752, y=0, f1(x, y)=0.22948817325502327, f2(x, y)=47.662125848999786
x=0.4813006085205006, y=0.3674457391451308, f1(x, y)=1.4666665879124627, f2(x, y)=41.8792031703218
x=1.6236365517815723, y=1.4187964196676808, f1(x, y)=18.596715730971937, f2(x, y)=24.224849218250455
x=1.3421310387244314, y=1.03892923898058, f1(x, y)=11.522758754865153, f2(x, y)=29.070086911666174
x=0.627896220098317, y=0.5835086002691228, f1(x, y)=2.9389437992071406, f2(x, y)=38.62068774612739
x=0.7257836313471406, y=1.0495707650599144, f1(x, y)=6.513442681599585, f2(x, y)=33.87481670632935
x=1.2722814941607148, y=0.9597892097733212, f1(x, y)=10.159582110324468, f2(x, y)=30.21918848824076

it produces 10 first results from the Pareto front. You can adjust this here.

I plotted the first 100 items from the generated results, looks pretty similar to the original plot :) binh-korn

syhpoon commented 1 year ago

I've also added this function as an example to the crate itself: https://github.com/syhpoon/nsga/tree/master/src/examples/binh-korn

s4ksb commented 1 year ago

Hi, many thanks for the examples, they helped a lot with getting the code to work!!

syhpoon commented 1 year ago

Glad to hear! Please let me know if there's anything else I could help clarifying.