opti_solve/lib.rs
1#![feature(test)]
2#![allow(unused)]
3#![feature(associated_const_equality)]
4#![feature(auto_traits, negative_impls)]
5
6
7pub mod prelude;
8
9use prelude::*;
10
11mod particle_swarm_optimization;
12// mod simulated_annealing;
13// mod ant_colony_optimization;
14// mod genetic_algorithm;
15
16use array_init;
17use send_wrapper::SendWrapper;
18
19
20
21use core::cmp::{PartialOrd,Ordering::{self, Equal, Greater, Less}};
22use core::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Sub, SubAssign};
23
24
25
26use pyo3::prelude::*;
27use rand::prelude::*;
28use rayon::prelude::*;
29use rust_decimal::prelude::*;
30
31use crate::particle_swarm_optimization::PSO;
32use num_bigint::BigInt;
33use num_complex::{Complex, ComplexDistribution};
34use rand::distributions::Uniform;
35use rust_decimal::Decimal;
36
37// #[pyclass(subclass)]
38// #[derive(Clone, Copy)]
39// pub struct OptiBase {
40// val1: usize,
41// }
42//
43//
44// #[pyclass(extends = OptiBase, subclass)]
45// pub struct LZQ {
46// val: usize,
47// }
48//
49// // #[pymethods]
50// // impl OptiBase {
51// // #[staticmethod]
52// // fn println(){
53// // println!("794613")
54// // }
55// // }
56//
57//
58// /// n:粒子个数,
59// /// c1,c2均为加速常数,通常在区间[0,2]内取值
60// /// m:迭代次
61// #[pyclass(get_all, set_all)]
62// #[derive(Clone, Copy)]
63// pub struct LiZiQunSuanFa {
64// n: usize,
65// m: usize,
66// c1: f64,
67// c2: f64,
68// }
69//
70//
71// #[pymethods]
72// impl LiZiQunSuanFa {
73// #[new]
74// #[pyo3(signature = ( n = 2usize, m = 2usize, c1 = 1.064, c2 = 1.0))]
75// fn new(n: usize, m: usize, c1: f64, c2: f64) -> Self {
76// Self { n, m, c1, c2 }
77// }
78// }
79//
80//
81// /// Formats the sum of two numbers as string.
82// #[pyfunction]
83// fn sum_as_string(x: Complex<f64>, y: Complex<f64>) -> Complex<f64> {
84// return x + y;
85// }
86//
87//
88/// A Python module implemented in Rust.
89#[pymodule]
90fn opti_solve(_py: Python, m: &PyModule) -> PyResult<()> {
91 m.add_class::<PSO>()?;
92 Ok(())
93}
94
95#[cfg(test)]
96mod tests {
97 extern crate test;
98
99 use test::Bencher;
100 use pyo3::types::PyString;
101
102 use crate::*;
103
104 #[test]
105 fn test() {
106 // use num_bigint::BigInt;
107 // use num_complex::Complex;
108 // use rust_decimal::Decimal;
109 // use rand::Rng;
110 // use std::ops::Range;
111 // use rust_decimal::prelude::FromPrimitive;
112 // use num_bigint::{ToBigInt, RandBigInt};
113 // use rand::distributions::{Distribution, Uniform};
114 // use std::str::FromStr;
115 // use num_complex::ComplexDistribution;
116 //
117 Python::with_gil(|py| {
118
119 let py1 = unsafe {py.new_pool()};
120
121 let one = py1.python().eval("1", None, None).unwrap();
122 let two = py1.python().eval("2", None, None).unwrap();
123 let res = 0;
124
125 println!("{:?}", res);
126 });
127
128
129 }
130
131 // #[bench]
132 // fn bencher(b: &mut Bencher) {
133 // use std::mem::size_of_val;
134 //
135 // #[bench]
136 // fn bencher(b: &mut Bencher) {
137 // use std::mem::size_of_val;
138 // let pso = particle_swarm_optimization::PSO::new(
139 // 10000, 1000, 1.0, 1.0,
140 // );
141 // b.iter(|| pso.min(
142 // |x| {
143 // let x = x.f64;
144 // (x[0] - 7.0).powi(2) + (x[1] + 3.0).powi(2) + (x[1] - 5.0).powi(2)
145 // },
146 // x_new().set_f64([-10.0, -10.0, -10.0]),
147 // x_new().set_f64([10.0, 10.0, 10.0]),
148 // ));
149 // }
150 // }
151}
152//
153//
154// use pyo3::prelude::*;
155//
156// #[pyclass(subclass)]
157// struct BaseClass {
158// val1: usize,
159// }
160//
161// #[pymethods]
162// impl BaseClass {
163// #[new]
164// fn new() -> Self {
165// BaseClass { val1: 10 }
166// }
167//
168// pub fn method(&self) -> PyResult<usize> {
169// Ok(self.val1)
170// }
171// }
172//
173// #[pyclass(extends = BaseClass, subclass)]
174// struct SubClass {
175// val2: usize,
176// }
177//
178// #[pymethods]
179// impl SubClass {
180// #[new]
181// fn new() -> (Self, BaseClass) {
182// (SubClass { val2: 15 }, BaseClass::new())
183// }
184//
185// fn method2(self_: PyRef<'_, Self>) -> PyResult<usize> {
186// let super_ = self_.as_ref(); // Get &BaseClass
187// super_.method().map(|x| x * self_.val2)
188// }
189// }
190//
191//
192// #[pyclass(extends = SubClass)]
193// struct SubSubClass {
194// val3: usize,
195// }
196//
197// #[pymethods]
198// impl SubSubClass {
199// #[new]
200// fn new() -> PyClassInitializer<Self> {
201// PyClassInitializer::from(SubClass::new()).add_subclass(SubSubClass { val3: 20 })
202// }
203//
204// fn method3(self_: PyRef<'_, Self>) -> PyResult<usize> {
205// let v = self_.val3;
206// let super_ = self_.into_super(); // Get PyRef<'_, SubClass>
207// SubClass::method2(super_).map(|x| x * v)
208// }
209// }
210//