Crappy Traffic Sim (C++)

I wrote this late one night where I went down the rabbit hole of simulating traffic. It has each vehicle as its own thread, which is completely unnecessary for what it currently does. Right now the vehicles are given an average speed and its speed is modified with some noise, and does some basic collision detection. The idea is to eventually develop and train a simple neural network to drive each vehicle. …

Posted on

Single Neuron Perceptron (C++)

This is a basic implementation of a single neuron perceptron that can learn to act as AND, OR, and NAND gates in C++. neuron.h #ifndef __NEURON_H__ #define __NEURON_H__ #include <vector>#include <tuple> using namespace std; class Neuron { public: Neuron(int numberOfInputs); ~Neuron() {}; double forward(vector<bool> inputs); bool forwardBinary(vector<bool> inputs); bool forwardBinaryCheck(vector<bool> inputs, bool answer); bool train(vector<bool> inputs, bool answers); private: vector<double> weights; // input weights vector<Neuron> inputs; // for future backpropagation vector<Neuron> outputs; // for futude backpropagation double threshold; // trigger threshold double learningRate; // how aggressive to tune the above values }; #endif // __NEURON_H__ neuron. …

Posted on

Async Monte Carlo Pi (C++)

This program uses C++11’s futures to estimate pi. // Compile with the flags "-O3 -std=c++11" // might need -pthread #include <iostream>#include <future>#include <random>#include <chrono> using namespace std; bool monteCarloPi(double x, double y) { if (x*x + y*y < 1.0) return true; else return false; } long int piThread(long int reps) { long int count = 0; unsigned int seed = (unsigned int)chrono::system_clock::now().time_since_epoch().count(); default_random_engine engine(seed); uniform_real_distribution<double> rand(0, 1); for (long int i = 0; i < reps; i++) { if (monteCarloPi(rand(engine), rand(engine))) count++; } return count; } int main (int argc, char** argv) { auto cores = thread::hardware_concurrency(); cout << "cores: " << cores << endl; long int samples = 0; if (argc > 1) samples = atoll(argv[1]); cout << "samples: " << samples << endl; future<long int>* results = new future<long int>[cores]; auto start = chrono::steady_clock::now(); for (unsigned i = 0; i < cores; i++) results[i] = async(launch::async, piThread, samples/cores); long int total = 0; for (unsigned i = 0; i < cores; i++) total += results[i]. …

Posted on

Monte Carlo Estimation of Pi (Rust)

This program uses futures to calculate pi using a monte carlo method. It randomly generates points and checks if they are inside of a unit circle. The proportion of the points inside and the total number of points are then used to estimate Pi. extern crate rand; extern crate time; extern crate num_cpus; extern crate futures; extern crate futures_cpupool; use std::env; use rand::Rng; use time::PreciseTime; use futures::Future; use futures_cpupool::CpuPool; fn main() { let start = PreciseTime::now(); let args: Vec<String> = env::args(). …

Posted on

Monte Carlo Estimation of Pi (GoLang)

This program uses goroutines to calculate a Monte Carlo estimation of Pi. It randomly generates points and them checks if its inside the circle. The proportion of the points inside and the total points is then used to estimate Pi. On an Intel 3770k, it accurately estimates Pi to 4 decimal digits using 1 trillion random points in just over 5 seconds. package main import ( "fmt" "math" "math/rand" "os" "runtime" "strconv" "sync" "time" ) func monte_carlo_pi(radius float64, reps int, result *int, wait *sync. …

Posted on

Mutithreaded Sumation (GoLang)

This was my first attempt to write something that uses the simplicity of threading in Go using goroutines. The number of threads is determined in the program and the run time is calculated. The performance of Go is really shown off when I compared the times to a similar single threaded program written in Python. For the sum of 1 through 1 billion, Python’s time was measured in minutes, and Go took less then a 10th of a second. …

Posted on

HTML Link Grabber (GoLang)

This program takes in a web address as a command line argument. Then goes and gets that page and then prints out all of the links to other pages. While not all that useful, it was a good first step into web programming. Credit for this goes to Jack Canty’s website because I’ve never written anything like this. His can be found here. package main import ( "fmt" "flag" "net/http" "os" "io" "golang. …

Posted on