Skip to content

Instantly share code, notes, and snippets.

View phabee's full-sized avatar

Fabian Leuthold phabee

View GitHub Profile
@phabee
phabee / diet.m
Last active November 4, 2020 11:03
MATLAB GUROBI Diet Example Script
function diet()
% Copyright 2020, Gurobi Optimization, LLC
%
% Solve the classic diet model
% Nutrition guidelines, based on
% USDA Dietary Guidelines for Americans, 2005
% http://www.health.gov/DietaryGuidelines/dga2005/
ncategories = 4;
#' get next best pilot stop using nearest neighbor
#'
#' @param tour
#' @param dima
#'
#' @return
#' @export
get_best_pilot_stop <- function(tour, dima) {
stops <- 1:nrow(dima)
potential_stops <- stops[-tour]
# -*- 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
# 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')
# 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
# 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')
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
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