Miscellaneous‎ > ‎

### Code (details)

 K-means Clusteringk-means clustering is a method of vector quantization, originally from signal processing, that is popular for cluster analysis in data mining. k-means clustering aims to partition n observations into k clusters in which each observation belongs to the cluster with the nearest mean, serving as a prototype of the cluster. This results in a partitioning of the data space into Voronoi cells.DownloadGithubUsageMATLAB - Unzip the file and use the test function as follows:`>>test_kmeans`Python / MATLABClone the repository:`>> git clone `git@github.com:rezaahmadzadeh/K-means.gitPython `>>python k-means.py`Expectation-Maximization AlgorithmThe Expectation–Maximization (EM) algorithm is an iterative method to find maximum likelihood or maximum a posteriori (MAP) estimates of parameters in statistical models, where the model depends on unobserved latent variables. The EM iteration alternates between performing an expectation (E) step, which creates a function for the expectation of the log-likelihood evaluated using the current estimate for the parameters, and a maximization (M) step, which computes parameters maximizing the expected log-likelihood found on the E step. These parameter estimates are then used to determine the distribution of the latent variables in the next E step.DownloadUsageUnzip the file and use the test function as follows:`>>test_em`GithubUsageclone the repository:`>> git clone `git@github.com:rezaahmadzadeh/Expectation-Maximization.gitRamer-Douglas-Peucker AlgorithmThe Ramer-Douglas-Peucker algorithm is a line-simplification algorithm. Given a curve specified with N points and an approximation tolerance, the algorithm selects a number of points M (M<=N) that approximate the original curve with the given tolerance. The Ramer–Douglas–Peucker algorithm (RDP) is an algorithm for reducing the number of points in a curve that is approximated by a series of points. This algorithm is also known under the names Douglas–Peucker algorithm, iterative end-point fit algorithm, and split-and-merge algorithm. DownloadUsageUnzip the file and use the function as follows:`>>testDouglasPeucker`Collatz ConjectureThe conjecture can be summarized as follows. Take any positive integer n. If n is even, divide it by 2 to get n / 2. If n is odd, multiply it by 3 and add 1 to obtain 3n + 1. Repeat the process (which has been called "Half Or Triple Plus One", or HOTPO) indefinitely. The conjecture is that no matter what number you start with, you will always eventually reach 1.Downloadcollatz.zipUsageUnzip the file and use the function as follows:`>>collatz(7)``>>checkCollatz(2:1000)`            Use the 'collatz.m' to check a single number. And use the 'checkCollatz.m' to check a set of numbers.When a function was released by MATLABThis is a handy function which MATLAB should have done it before. It returns the version of MATLAB that the input function was released with.DownloadUsageUnzip the file and use the function as follows:`>>when('rand')``>> ##[rand] is a built-in Matlab function (Introduced before 2006a).`Boyer-Moore Majority Vote AlgorithmThe Boyer-Moore Vote Algorithm solves the majority vote problem in linear time O(n) and logarithmic space O(\log n).DownloadUsageUnzip the file and use the function for instance`>> MajorityVote('aaaccbbcccbcc') `Visuospatial Skill Learning (VSL) (new)A demo of Visuospatial Skill Learning approachRepositoryUsageClone the repository on your machine`>> demonstration.m``>> reproduction.m`Global Image ThresholdComputes the global threshold level using Otsu's methodDownloadUsageUnzip the file and use the function for instance:`>> otsu(imhist(Igray),numel(Igray))`Sorting AlgorithmsSimple Sorting Algorithms including:1- Bubble Sort2- Cocktail Sort3- Odd-Even Sort4- Insertion SortDownloadUnzip the file and use the algorithms for instance:`>> A = rand(1,100);``>> SortedA = bubbleSort(A);`Toolbox Check in MATLABCheck to see if a specific toolbox is installed.DownloadisToolboxAvailable functionUsageUnzip the file and run isToolboxAvailable.m in MATLAB, for instance`>> result = isToolboxAvailable('Image Processing Toolbox','warning')``>> result =``>>    1`Stabilized Gram-Schmidt Orthonormal MethodThis function receives a set of linearly independent vectors and converts them to a set of orthonormal vectors. DownloadUnzip the file and run GramSchmidt.m in MATLAB, for instance:`>> GramSchmidth([3 2;1 2])``>>ans =`` ````0.9487 -0.3162 ```` 0.3162 0.9487` Function Approximation using Radial Basis NetworksIn order to approximate a function defined by a set of data-points, this code utilizes a Radial Basis Network (RBN). The RBN consists of radial basis neurons in the hidden layer and linear neurons in the output layer. The weights and biases of each neuron in the hidden layer define the position and width of a radial basis function. With the correct weight and bias values for each layer, and enough hidden neurons, a radial basis network can fit any function with any desired accuracy. In order to use this function approximation in a closed-loop, in each iteration a number of new data-points are added to the previous data and the process of approximation (i.e., training the network) is repeated.DownloadFunction approximation using radial basis networks [MATLAB]UsageUnzip the file and run:`>>faRBN.m` Particle Swarm Optimization (PSO) Algorithm (Vectorized) This program is a simple but vectorized implementation of the Particle Swarm Optimization in MATLAB. Particle swarm optimization (PSO) is a computational method that optimizes a problem by iteratively trying to improve a candidate solution with regard to a given measure of quality. PSO optimizes a problem by having a population of candidate solutions, here called particles, and moving these particles around in the search-space according to simple mathematical formula over the particle's position and velocity. Each particle's movement is influenced by its local best known position but, is also guided toward the best known positions in the search-space, which are updated as better positions are found by other particles. This strategy is expected to move the swarm toward the best solutions.DownloadUsageUnzip the file and run`>>vpso.m`Van Valedhuizen's Test Suite Problems for Multi-objective OptimizationThe package includes 7 standard functions of the Van Valedhuizen's test suite. These functions are one of the benchmarks for testing multi-objective optimization algorithms. A Multi-objective function is a function that has more than one objective to be optimized. The functions are written in vectorized form that helps the algorithm with more performance and speed. All the functions are written for MINIMIZATION except for MOP3 that is written for MAXIMIZATION. DownloadMulti-Objective Optimization Test Suit (vectorized implementation)Usageunzip the file and you can run each function with an input in MATLAB, for instance:`>> X=rand(3,2); ``>> J=MOP7(X)`Model-based Policy Iteration AlgorithmThis code is a very simple implementation of a model-based policy iteration algorithm applied to a deterministic cleaning robot problem, which makes it a useful start point for beginners in the field of Reinforcement learning and dynamic programming. The deterministic cleaning-robot has to collect a used can and recharge its batteries. the state describes the position of the robot and the action describes the direction of motion. The robot can move to the left or to the right. The first and the final states are the terminal states. The goal is to find an optimal policy that maximizes the return from any initial state.DownloadModel-based Policy Iteration Algorithm for Deterministic Cleaning RobotUsageUnzip the file and run `>>``model_based_deterministic_policy_iteration.m`Q-learning (Model-free Value Iteration Algorithm) with ε-greedy ExplorationQ-learning with epsilon-greedy exploration algorithm for a deterministic cleaning robot. The deterministic cleaning robot has to collect a used can and recharge its batteries. The state describes the position of the robot and the action describes the direction of motion. The robot can move to the left or to the right. The first and the final states are the terminal states. The goal is to find an optimal policy that maximizes the return from any initial state. Here the Q-learning epsilon-greedy exploration algorithm (in Reinforcement learning) is implemented in MATLAB.DownloadQ-learning with ε-greedy ExplorationUsageUnzip the file and run`>>qlearning.m`Model-based Value Iteration Algorithm (Stochastic)Model-based value iteration Algorithm for Stochastic Cleaning Robot. This code is a very simple implementation of a value iteration algorithm, which makes it a useful start point for beginners in the field of Reinforcement learning and dynamic programming. The stochastic cleaning-robot has to collect a used can and recharge its batteries. The state describes the position of the robot and the action describes the direction of motion. The robot can move to the left or to the right. The first and the final states are the terminal states. The goal is to find an optimal policy that maximizes the return from any initial state. Here the Q-iteration (model-based value iteration DP).DownloadModel-based Value Iteration Algorithm (Stochastic)UsageUnzip the file and run`>>stochastic_robot_cleaning_v1.m`Model-based Value Iteration Algorithm (Deterministic)Model-based value iteration Algorithm for Deterministic Cleaning Robot. This code is a very simple implementation of a value iteration algorithm, which makes it a useful start point for beginners in the field of Reinforcement learning and dynamic programming. The deterministic cleaning-robot has to collect a used can and recharge its batteries. The state describes the position of the robot and the action describes the direction of motion. The robot can move to the left or to the right. The first and the final states are the terminal states. The goal is to find an optimal policy that maximizes the return from any initial state. Here the Q-iteration (model-based value iteration DP).DownloadModel-based Value Iteration Algorithm (Deterministic)UsageUnzip the file and run: `>>``deterministic_robot_cleaning_v1.m`Homogeneous 3D TransformationThis function is used to perform transformation in 3D space. The inputs to this function are the rotation angles and the translation values (roll : rotation about x axis (rad), pitch : rotation about y axis (rad), yaw : rotation about z axis (rad), translation values: tx, ty, tz, sequence : is the sequence of the rotation in an string e.g. 'xyz'). The outputs are the total homogeneous 3d transformation matrix and individual matrices (Rx : rotation matrix around x axis, Ry : rotation matrix around y axis, Rz : rotation matrix around z axis).DownloadHomogeneous 3D TransformationUsageDownload the file and use the calculateTransformationMatrix function in MATLAB as follows:`>> [T,Transl,Rx,Ry,Rz] = calculateTransformationMatrix(roll,pitch,yaw,tx,ty,tz,sequence)`Trajectory Generation (3rd and 5th order)In this package there are two functions that can be used to create smooth 3rd order and 5th order trajectories. Provide boundary conditions for the functions, the output vector can be used as a polynomial vector in MATLAB. There is also a test file that shows how to use the two functions.DownloadTrajectory generation UsageUnzip the file and run test.m in MATLAB to see a demoEdit Distance Algorithm - A Standard Dynamic ProgrammingEdit Distance is a standard Dynamic Programming problem. Given two strings s1 and s2, the distance between s1 and s2 is the minimum number of operations required to convert string s1 to s2. The following operations are typically used: replacing one character of string by another character, deleting a character from string, adding a character to string.DownloadEdit Distance AlgorithmUsageUnzip the file and use the EditDistance function in MATLAB as follows:`>> s1='article'``>> s2='ardipo'``>> EditDistance(s1,s2)`Random Search Algorithm (RSA)Random search belongs to the fields of Stochastic Optimization and Global Optimization. Random search is a direct search method as it does not require derivatives to search a continuous domain. This base approach is related to techniques that provide small improvements such as Directed Random Search, and Adaptive Random Search.DownloadRandom Search AlgorithmUsageUnzip the file and run: `>>RSA_visual.m`Z-N PID Controller Tuning (Zigler-Nichols)This package can be used to tune a PID controller according to Zigler-Nichols methods. It includes two methods: method 1 uses step response and delay time and time constant parameters, and method 2 uses proportional controller action only. you should find critical gain and corresponding critical period. All results come in 2 different output shape. 1- Controller with (Kp - Ti - Td) 2- Controller with (Kp - Ki - Kd)These methods aimed at obtaining 25% maximum overshoot in step response. So the Z-N method just finds an initial PID Controller coefficients. For a better controller you have to optimize the coefficients using other existing methods.DownloadZ-N PID Controller TuningUsageUnzip the file and run:`>>pidtune1.m`Note: All the source-codes are free to use. You may modify it as you want. The author does not have any responsibility for any consequence of using these codes. Contact the author if you encountered any problem. Please provide references to the authors' publications in works using part of these codes.