Skip to content

Instantly share code, notes, and snippets.

@nbro
nbro / knuth_problem.py
Last active January 22, 2021 21:41
Knuth Problem (described in section 3.2 of the 3rd edition of the book "Artificial Intelligence: A Modern Approach", p. 73) with Tree-based Genetic Programming
# You need to have graphviz installed
# if you want to plot the trees (commented for now).
# It doesn't seem to find the right solution.
# The best solution seems to be: floor(sqrt(float(sqrt(4)))),
# which produces 1, so it has a fitness of 4.
import math
import operator
import random
import sys
@nbro
nbro / policy_iteration.py
Created December 16, 2017 15:59
Simple example of policy iteration on a grid/maze world (using Python/NumPy)
import numpy as np
E = EMPTY = 0
B = BLOCKED = 1
G = GOAL = 2
# The maze from the assignment
MAZE = np.array(
[[0, 0, 0, 0, 0, 0, 0],
[0, 0, B, 0, 0, 0, 0],
@nbro
nbro / double_bridge.md
Last active March 12, 2017 22:41
In-place double-bridge or 4-opt move

I've implemented a double-bridge (or 4-opt) move in place, i.e. without any additional containers than the original. I'm sharing it with you first because I would like to know if it's correct or if I can improve it and second because I thought it could be helpful to someone else (since I have not found anything on the web).

/*
 * DOUBLE-BRIDGE-COMPUTE-GAIN
 *
 * Computes and returns the gain of a random double-bridge move.
 * The indices of the vertices used are also returnd as 2nd, 3rd and 4th parametrs of the array returned.
 * */
std::array<int, 4> double_bridge_in_place_compute_gain(const MatrixGraph &m, const VectorTour &t) {