Skip to content

Instantly share code, notes, and snippets.

View phabee's full-sized avatar

Fabian Leuthold phabee

View GitHub Profile
import matplotlib.pyplot as plt
def remove_duplicate_candidates(N):
'''
Accept a list of candidate solutions (being binary lists) and make sure, each (element)-list is contained only once.
:param N:
:return: revised list containing no duplicate candidates
'''
@phabee
phabee / sudoku_solver_milp.py
Last active December 8, 2023 15:26
Sudoku Solver using MILP
from ortools.linear_solver import pywraplp
import numpy as np
def solve(sudoku):
"""
Solve the sudoku passed
:param sudoku: a 9x9 sudoku field
:return: the completed 9x9 sudoku field
"""
# Create the model.
def construct_knapsack_solution(weights, values, capacity):
'''
Create a fair (initial) knapsack solution
:param weights: Weights of the objects in the knapsack problem
:param values: Values assigned to the knapsack items
:param capacity: Capacity of the knapsack
:return: an (initial) solution to start knapsack optimization with
'''
# enrich list with value/weight for each weight-value tupel
def remove_duplicate_candidates(N):
'''
Accept a list of candidate solutions (being binary lists) and make sure, each (element)-list is contained only once.
:param N:
:return: revised list containing no duplicate candidates
'''
unique_set = set(tuple(lst) for lst in N)
unique_list_of_lists = [list(tpl) for tpl in unique_set]
return unique_list_of_lists
# source based on code presented here https://developers.google.com/optimization/mip/integer_opt
from ortools.linear_solver import pywraplp
# example how to solve a MIXED integer linear program continuous and integer variables
# and solve it with a gurobi solver installed (or gurobi cloud).
def main():
# Create the mip solver with the SCIP backend.
solver = pywraplp.Solver.CreateSolver('GUROBI_MIP')
# source based on code presented here https://developers.google.com/optimization/mip/integer_opt
from ortools.linear_solver import pywraplp
# example how to solve a linear program having ONLY continuous variables
def main():
# Create the mip solver with the SCIP backend.
# HINT: GLOP cannot handle integer variables!
solver = pywraplp.Solver.CreateSolver('GLOP')
# source based on code presented here https://developers.google.com/optimization/mip/integer_opt
from ortools.linear_solver import pywraplp
# example how to solve a MIXED integer linear program continuous and integer variables
# x is continuous
# y is integer
def main():
# Create the mip solver with the SCIP backend.
solver = pywraplp.Solver.CreateSolver('SCIP')
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'addition_dialog.ui'
#
# Created by: PyQt5 UI code generator 5.15.4
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again. Do not edit this file unless you know what you are doing.
from PyQt5 import QtCore, QtGui, QtWidgets
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>452</width>
<height>247</height>
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'addition_dialog.ui'
#
# Created by: PyQt5 UI code generator 5.15.4
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again. Do not edit this file unless you know what you are doing.
from PyQt5 import QtCore, QtGui, QtWidgets