A no-GUI module of mechanism synthesis system and a 2D geometric constraint solver.
The submodule is located at
pip install pyslvs
Pyslvs-UI is using global
pyslvs module as kernel.
Please make sure the version is same with it.
Build and Test¶
Execute the unit test script after the kernel compiled.
pip install -e . python test
Pyslvs libraries is divided into two following sections:
Geometric solver and verification functions.
Mechanism synthesis system that including several random algorithm and enumeration algorithm, dependent with geometric solver.
Most of classes and functions can be work with a generic Python format (just like a list of coordinates or string-like expression), and you also can cut in at any step.
Analysis expression from strings, turn into symbols object.
Including PMKS expression object classes.
Python wrapper of Sketch Solve. A simple and fast constraint solver with BFGS algorithm.
Particular solution takes more faster then constraint solving.
Autometic configuration algorithm for particular solution function in “tinycadlib”.
Kinematic Graph Synthesis¶
Graph expression came from NetworkX. Also contains graph verification functions.
Planar graph checking came from NetworkX.
Structural synthesis algorithm.
Layout algorithm for graphs.
metaheuristics module: Cython algorithms libraries provide evolution designing.
Provide base fitness function class for algorithm.
Real-coded genetic algorithm for dimensional synthesis.
Firefly algorithm for dimensional synthesis.
Differential Evolution for dimensional synthesis.
Dimensional synthesis for multi-link mechanisms. (defect allowable)
Dimensional synthesis for normalized four-bar linkages. (defect free)
This function is not implemented yet.
Pyslvs provides some search algorithms for optimization problems. The algorithm requires an objective function F(x), which accept variables x, and returns a fitness value E to minimize. The objective function is subject to several constraints g_i(x) that provides a sum of penalty values on the fitness value.
As shown in the example, objective function is created through inheriting a base class
The upper bound and lower bound are set by initialization method
and the fitness value is defined by
The final result is defined by
result method, which usually is your variables.
cimport cython from numpy import array, float64 as f64 from pyslvs.metaheuristics cimport ObjFunc @cython.final cdef class TestObj(ObjFunc): """ f(x) = x1^2 + 8*x2 """ def __cinit__(self): self.ub = array([100, 100], dtype=f64) self.lb = array([0, 0], dtype=f64) cdef double fitness(self, double[:] v) nogil: return v * v + 8 * v cpdef object result(self, double[:] v): """x = (0, 0)""" return tuple(v)
The function object can be created with specific optionals by designing initialization function, but there are no options in this example.
We choose a algorithm called “Real-coded Genetic Algorithm” (RGA) to solve this problem.
This example is recorded in
from numpy import array from matplotlib.pyplot import plot, show from pyslvs.metaheuristics import AlgorithmType, algorithm, default from pyslvs.metaheuristics.test import TestObj t = AlgorithmType.RGA a = algorithm(t)(TestObj(), default(t)) # Show answer ans = a.run() # Plot convergance graph h = array(a.history()) plot(h[:, 0], h[:, 1], 'ro-') show()