Skip to content

Instantly share code, notes, and snippets.

@270ajay
270ajay / n_queens.py
Created December 21, 2023 05:12
Constraint Programming solver module 1
# Course: https://www.edx.org/learn/computer-programming/universite-catholique-de-louvain-constraint-programming
# Backtracking search algorithm for solving n-queens problem
import time
from typing import Callable
class NQueensFilter:
def __init__(self, number_of_queens: int):
self._number_of_queens: int = number_of_queens
@270ajay
270ajay / knapsack_solver.py
Created December 21, 2023 04:59
Knapsack solver library from ortools in python
# Knapsack library by ortools
# Translated from c++ code:
# https://github.com/google/or-tools/blob/stable/ortools/algorithms/knapsack_solver.h
# https://github.com/google/or-tools/blob/stable/ortools/algorithms/knapsack_solver.cc
import bisect
import heapq
import math
from ortools.linear_solver import pywraplp
@270ajay
270ajay / abstract_factory_pattern.py
Created December 3, 2023 09:35
Design Patterns: Taken from Derek Banas's Design Patterns Playlist on YouTube
from abc import ABC, abstractmethod
class EnemyShipBuilding(ABC):
@abstractmethod
def make_enemy_ship(self, type_of_ship: str) -> "EnemyShip":
pass
def order_the_ship(self, type_of_ship: str) -> "EnemyShip":
the_enemy_ship = self.make_enemy_ship(type_of_ship)
@270ajay
270ajay / refactoring1.py
Created November 29, 2023 12:23
Code Refactoring: Taken from Derek Banas's Refactoring Playlist on YouTube
class _FootballPlayer:
def __init__(
self,
passer_rating: int,
rushing_yard: int,
receiving_yard: int,
total_tackles: int,
interceptions: int,
avg_punt: int,
avg_kick_off_return: int,
@270ajay
270ajay / python-formatter.bat
Created November 23, 2023 13:00
Batch file to format python files
@ECHO OFF
:: This batch file helps in automating tasks on cmd.
:: First, we sort inports using isort package.
:: Then, we remove unused imports using autoflake package.
:: Finally, we format using black package.
TITLE Python Files Formatter
ECHO /!\ Please make sure you have the following packages installed:
@270ajay
270ajay / 0VRPTWColGen.py
Created July 26, 2018 07:17
Using Column Generation for Optimizing Vehicle Routing Problem with Time Windows. The Objective here is only to minimize the number of vehicles used. Uses PuLP library in python.
import csv
import pulp #This contains LP Modeling and solver
import time #This is used to see how much time for solving
#######################################################################
#######################################################################
@270ajay
270ajay / utils.py
Last active April 3, 2022 09:41
logging, json, excel functions, py to exe
# Example of license header:
# Unauthorized copying of this file, via any medium is strictly prohibited
# Proprietary and confidential
"""This file contains utility classes and functions."""
import json
import logging
import openpyxl
@270ajay
270ajay / create_lp_sol_file.py
Created February 15, 2022 10:04
Lp sol file generator for debugging mathematical models
"""Creates a file which contains info from lp and sol file.
----------------
Sample sol file:
----------------
# Solution for model Mathematical Model
# Objective value = 140
x1 10
x2 20
x3 30
@270ajay
270ajay / advancedModeling2Week1.py
Created December 24, 2020 11:44
Advanced constraint programming modeling - 2
from ortools.sat.python import cp_model
''' course: https://www.coursera.org/learn/solving-algorithms-discrete-optimization#about
About how cp works and intro to solver strategies'''
def patchingThe9HeavensProblem(): #sudoku problem
model = cp_model.CpModel()
@270ajay
270ajay / VRPTWLNS.py
Created July 26, 2018 07:20
Uses Large Neighborhood Search for optimizing Vehicle Routing Problem with Time Windows. This algorithm can easily be improved.
import csv
import pulp #This contains LP Modeling and solver
import time #This is used to see how much time for solving
import random
#######################################################################
#######################################################################