Skip to content

Instantly share code, notes, and snippets.

View AhmadElsagheer's full-sized avatar

Ahmad Elsagheer AhmadElsagheer

View GitHub Profile
@AhmadElsagheer
AhmadElsagheer / task.pl
Created December 31, 2017 10:48
Task Assignment (CLPFD)
:-use_module(library(clpfd)).
warehouse(Emps_Tasks_Limit, Tasks_Demands, Conflicts, AssignedEmployees, Conflicts_Count):-
length(Tasks_Demands, M), length(Emps_Tasks_Limit, N),
matrix(Assignment_Matrix, M, N),
% 1. Each task t is assigned to exactly Demands[t] employees
maplist(demands_constraint, Assignment_Matrix, Tasks_Demands),
% 2. Each employee e is assigned at most Max_Tasks[e] tasks
transpose(Assignment_Matrix, AssMatTrans),
maplist(employee_constraint, AssMatTrans, Emps_Tasks_Limit),
@AhmadElsagheer
AhmadElsagheer / Binpack.java
Created December 31, 2017 08:40
Binpacking (Java)
import java.io.IOException;
import java.util.Arrays;
import java.util.Scanner;
// Solves the optimization problem.
// Input: Number of items, Sizes, Number of packs, Capacity per pack
public class Binpack {
public static void main(String[] args) throws IOException {
@AhmadElsagheer
AhmadElsagheer / binpack.pl
Last active January 1, 2018 10:28
Binpacking (CLPFD)
:-use_module(library(clpfd)).
binpack(Sizes, Packs_Count, Capacity, Packs):-
length(Sizes, N), length(Packs, N),
Packs ins 1..Packs_Count,
packs_constraints(Packs, Sizes, Packs_Count, Capacity),
labeling([], Packs).
packs_constraints(_, _, 0, _).
packs_constraints(Packs, Sizes, Pack, Capacity):-
@AhmadElsagheer
AhmadElsagheer / Car.java
Created December 31, 2017 07:26
Car Production (Java)
import java.io.IOException;
import java.util.Scanner;
// Input: Investment Amount
public class Car {
static final int[][] prodCost = {{100, 70, 50, 35}, {200, 140, 65, 50}, {450, 300, 200, 100}};
static final int[] profit = {30, 40, 70};
@AhmadElsagheer
AhmadElsagheer / car.pl
Last active January 1, 2018 11:07
Car Production
:-use_module(library(clpfd)).
% Forgot to handle at least 3 cars of each type.
car_production(Invest, A, B, C, Profit):-
[A, B, C] ins 0..Invest,
Profit #= A * 30 + B * 40 + C * 70,
cost(A, (100, 70, 50, 35), CA),
cost(B, (200, 140, 65, 50), CB),
cost(C, (450, 300, 200, 100), CC),
CA + CB + CC #=< Invest,
labeling([max(Profit), ff, bisect], [A, B, C]).
@AhmadElsagheer
AhmadElsagheer / schedule.pl
Last active December 31, 2017 06:02
Exam Scheduling
:-use_module(library(clpfd)).
:-use_module(library(pairs)).
% System allows different exams in the same room.
% Each course has only one exam.
% Input
% =====
% Courses = [course(Course_Name, Exam_Type)]
% Resources = [room(Capacity, Type)]
@AhmadElsagheer
AhmadElsagheer / SeeSaw.java
Created December 31, 2017 03:46
See-Saw Balance
import java.io.IOException;
import java.util.Scanner;
// Takes a list of weights and prints all possible position configurations that makes
// the see saw balanced.
// Input: L (half length of seesaw), n (number of weights), W1, W2, ... Wn
public class SeeSaw {
public static void main(String[] args) throws IOException {
@AhmadElsagheer
AhmadElsagheer / seesaw.pl
Created December 31, 2017 03:23
See-Saw Balance
:-use_module(library(clpfd)).
% valid positions are in [-Saw_L, Saw_L].
% works for many variable weights and many variable positions.
seesaw_balance(Saw_L, Weights, Positions):-
length(Weights, N), length(Positions, N), NSaw_L is -Saw_L,
Weights ins 0..500, Positions ins NSaw_L..Saw_L,
angular_moment(Weights, Positions, 0),
apart_positions(Positions),
label(Positions), label(Weights).
@AhmadElsagheer
AhmadElsagheer / board.pl
Created December 31, 2017 02:54
Sawmill Board Production
:-use_module(library(clpfd)).
patterns([
pattern(2, 0, 1, 0),
pattern(1, 2, 0, 0),
pattern(1, 1, 1, 1),
pattern(1, 0, 3, 0),
pattern(0, 3, 0, 1),
pattern(0, 2, 2, 0),
pattern(0, 1, 3, 1),
@AhmadElsagheer
AhmadElsagheer / hospital.pl
Created December 31, 2017 00:32
Hospital Patients (CLPFD)
:-use_module(library(clpfd)).
% patient(ID, Name, YearAdmitted, MonthAdmitted,
% DayAdmitted, HourAdmitted, MinuteAdmitted, Status, Payment)
patient(1, 'Bob Jones', 2014, 10, 1, 4, 55, 0, 2).
patient(2, 'Sally Smith', 2014, 9, 29, 5, 15, 1, 0).
patient(3, 'Ted Overton', 2014, 9, 30, 14, 15, 0, 0).
patient(4, 'Arnold Abouja', 2014, 10, 1, 5, 0, 0, 0).
patient(5, 'Seth Humbolt', 2014, 10, 1, 5, 10, 0, 0).