Skip to content

Instantly share code, notes, and snippets.

Avatar

Prabod Rathnayaka prabod

View GitHub Profile
View routes.js
app.get('/', function(req, res) {
res.render('index');
});
// PROFILE SECTION =========================
app.get('/profile', isLoggedIn, function(req, res) {
res.render('profile', {
user: req.user
});
});
View passport.js
passport.use('local-login', new LocalStrategy({
// by default, local strategy uses username and password, we will override with email
usernameField: 'email',
passwordField: 'password',
passReqToCallback: true // allows us to pass in the req from our route (lets us check if a user is logged in or not)
},
function(req, email, password, done) {
if (email)
email = email.toLowerCase(); // Use lower-case e-mails to avoid case-sensitive e-mail matching
View User.js
// define the schema for our user model
var userSchema = mongoose.Schema({
local : {
first_name : String,
last_name : String,
email : String,
password : String,
},
facebook : {
View server.js
var express = require('express'),
bodyParser = require('body-parser'),
cookieParser = require('cookie-parser'),
cookieSession = require('cookie-session'),
serveStatic = require('serve-static'),
expressValidator = require('express-validator'),
passport = require('passport'),
crypto = require('crypto'),
flash = require('connect-flash'),
mongoose = require('mongoose'),
@prabod
prabod / crossover.js
Created Dec 3, 2016
Crossover Function
View crossover.js
/**
* Cross two Chromosomes and produce a child Chromosome
* */
function crossover(chromosome1,chromosome2,rate,geneSize,chromoSize) {
var rand = Math.random(); // Random value for check crossover chance
if (rand < rate){
var vString = [];
@prabod
prabod / selection.js
Last active Dec 3, 2016
Selection Procedure
View selection.js
/**
* Breed a New Generation of Chromosomes
* */
function breed() {
var totalFitness = 0;
var fittest;
var fit=0;
/**
@prabod
prabod / fitness.js
Created Dec 3, 2016
Fitness Function
View fitness.js
/**
* Fitness Function
* fitness = 1 - (Square of pixel difference between chromosome and reference Image)
* ____________________________________________________________________
* ( Resolution of the Image * count(RGBA) * Number of Possible Values)
*
* This fitness function stays inside [0,1]
* */
fitness(width,height){
View randomPopulation.js
/**
* Generate random DNA for the Initial Generation
* Value Encoding is used for DNA encoding
* DNA = [RED, GREEN, BLUE, ALPHA, X1, Y1, X2, Y2, ...]
* */
randomGenes(){
var bString = [];
for (var i = 0; i < this.chromoSize; i+= this.geneSize){
/**