Building an objective function involving counting in pyomo for assignment problem - python

My Pyomo model is trying to solve a task assignment problem where 4 workers needs to be assigned to 8 tasks, so that's 2 tasks per worker.
One of the objective function model.obj2 tries to minimize the sum of the types of materials used by each worker worker. The reason is because every truck transporting materials to the worker can only carry 1 type of material, so there is efficiency gains to minimize the total number of truck visits.
This is currently being done using len(set(...)) to find number of unique materials used by both tasks assigned to a worker, and sum() to add up this number for all 4 workers.
def obj_rule(m):
# Minimize the total costs
obj1 = sum(
costs[i][j] * model.x[w, t] for i, w in enumerate(W) for j, t in enumerate(T)
)
# Minimize the number of unique materials used per worker
obj2 = len(
set(
material
for w in W
for t in T
for material in materials_used[t]
if value(model.x[w, t]) == True
)
)
return 5 * obj1 + 2 * obj2
However, removing model.obj1 (for debugging purposes), such as
def obj_rule(m):
# Minimize the number of unique materials used per worker
obj2 = len(
set(
material
for w in W
for t in T
for material in materials_used[t]
if value(model.x[w, t]) == True
)
)
return obj2
results in the warning
WARNING: Constant objective detected, replacing with a placeholder to prevent
solver failure.
This might explain why model.obj2 does not seem to be minimized for in the initial code. The objective expression might have been converted into a scalar value?
Can I get some help to rewrite this objective function the proper way for Pyomo? Thank you!
Code to reproduce problem
from pyomo.environ import *
import numpy as np
# 4 workers X 8 tasks
costs = np.array(
[
# workerA
[1, 2, 3, 4, 5, 6, 7, 8],
[1, 2, 3, 4, 5, 6, 7, 8],
# workerB
[8, 7, 6, 5, 4, 3, 2, 1],
[8, 7, 6, 5, 4, 3, 2, 1],
# workerC
[1, 3, 5, 7, 9, 11, 13, 15],
[1, 3, 5, 7, 9, 11, 13, 15],
# workerD
[15, 13, 11, 9, 7, 5, 3, 1],
[15, 13, 11, 9, 7, 5, 3, 1],
]
)
# "stone", "wood", "marble", "steel", "concrete"
materials_used = {
"taskA": ["stone", "wood"],
"taskB": ["marble", "wood"],
"taskC": ["marble", "stone"],
"taskD": ["steel", "stone"],
"taskE": ["marble", "steel"],
"taskF": ["marble", "steel"],
"taskG": ["concrete", "marble"],
"taskH": ["concrete", "steel"],
}
W = [
"workerA1",
"workerA2",
"workerB1",
"workerB2",
"workerC1",
"workerC2",
"workerD1",
"workerD2",
]
T = ["taskA", "taskB", "taskC", "taskD", "taskE", "taskF", "taskG", "taskH"]
model = ConcreteModel()
model.x = Var(W, T, within=Binary, initialize=0)
def obj_rule(m):
# Minimize the total costs
# obj1 = sum(
# costs[i][j] * model.x[w, t] for i, w in enumerate(W) for j, t in enumerate(T)
# )
# Minimize the number of unique materials used per worker
obj2 = len(
set(
material
for w in W
for t in T
for material in materials_used[t]
if value(model.x[w, t]) == True
)
)
return obj2
# return 5 * obj1 + 2 * obj2
model.obj = Objective(
rule=obj_rule,
sense=minimize,
)
def all_t_assigned_rule(m, w):
return sum(m.x[w, t] for t in T) == 1
def all_w_assigned_rule(m, t):
return sum(m.x[w, t] for w in W) == 1
model.c1 = Constraint(W, rule=all_t_assigned_rule)
model.c2 = Constraint(T, rule=all_w_assigned_rule)
opt = SolverFactory("glpk")
results = opt.solve(model)

I think there are 2 things I can add here that might be your missing links...
First, as mentioned in comments, the stuff you feed into the model must be legal expressions that do not depend on the value of the variables at time of creation, so len() etc. are invalid. Solution: use binary variables for those types of counting things and then sum them over appropriate indices.
Second, you are indexing your first variable correctly, but you have a second variable you need to introduce, namely, the decision to send worker w some material matl. See my example below that introduces this variable and then uses a big-M constraint to link the two decisions together.... Specifically, ensure the model delivers a required material to a worker for a task in which it is required.
Code:
# worker-task-material assignement
# goal: assign workers to tasks, minimze cost of sending them materials
# individually with a weighted OBJ function
import pyomo.environ as pyo
# some data
tasks = list('ABCDEF')
matls = ['stone', 'steel', 'wood', 'concrete']
big_M = max(len(tasks), len(matls)) # an upper bound on the num of matls needed
# this could be expanded to show quantities required...
material_reqts = [
('A', 'stone'),
('A', 'steel'),
('A', 'wood'),
('B', 'stone'),
('B', 'concrete'),
('C', 'concrete'),
('D', 'steel'),
('D', 'concrete'),
('E', 'stone'),
('E', 'wood'),
('F', 'steel'),
('F', 'wood')]
# convert to dictionary for ease of ingestion...
matls_dict = {(task, matl) : 1 for (task, matl) in material_reqts}
workers = ['Homer', 'Marge', 'Flanders']
# a little delivery cost matrix of matl - worker
worker_matl_delivery_costs = [
[1, 2, 4, 5], # Homer
[2, 3, 1, 1], # Marge
[4, 4, 3, 2]] # Flanders
wm_dict = { (w, m) : worker_matl_delivery_costs[i][j]
for i, w in enumerate(workers)
for j, m in enumerate(matls)}
# salary matrix
worker_task_costs = [
[2.2, 3.5, 1.9, 4.0, 3.8, 2.1],
[1.5, 3.0, 2.9, 4.0, 2.5, 1.6],
[1.4, 4.0, 2.3, 4.4, 2.5, 1.8]]
wt_dict = { (w, t) : worker_task_costs[i][j]
for i, w in enumerate(workers)
for j, t in enumerate(tasks)}
# build model components...
m = pyo.ConcreteModel()
# SETS
m.W = pyo.Set(initialize=workers)
m.T = pyo.Set(initialize=tasks)
m.M = pyo.Set(initialize=matls)
# PARAMS
m.delivery_costs = pyo.Param(m.W, m.M, initialize=wm_dict)
m.salary_costs = pyo.Param(m.W, m.T, initialize=wt_dict)
# note: need a default here to "fill in" the non-requirements...
m.matl_reqts = pyo.Param(m.T, m.M, initialize=matls_dict, default=0)
# VARS
m.Assign = pyo.Var(m.W, m.T, domain=pyo.Binary) # assign worker to task decision
m.Deliver = pyo.Var(m.W, m.M, domain=pyo.Binary) # deliver material to worker decision
# build model
# OBJ
# some conveniences here.... we can make model expressions individually
# for clarity and then combine them in the obj.
# pyo.summation() is a nice convenience too! Could also be done w/generator
delivery = pyo.summation(m.delivery_costs, m.Deliver)
salary = pyo.summation(m.salary_costs, m.Assign)
w1, w2 = 0.5, 0.6 # some arbitrary weights...
m.OBJ = pyo.Objective(expr=w1 * delivery + w2 * salary)
# CONSTRAINTS
# each worker must do at least 2 tasks. (note: this is an extension of your reqt.
# in this in conjunction with constraint below will pair all 6 tasks (2 ea. to workers)
# if more tasks are added, they'll be covered, with a min of 2 each
def two_each(m, w):
return sum(m.Assign[w, t] for t in m.T) >= 2
m.C1 = pyo.Constraint(m.W, rule=two_each)
# each task must be done once...prevents tasks from being over-assigned
def task_coverage(m, t):
return sum(m.Assign[w, t] for w in m.W) >= 1
m.C2 = pyo.Constraint(m.T, rule=task_coverage)
# linking constraint.... must deliver materials for task to worker if assigned
# note this is a "for each worker" & "for each material" type of constraint...
def deliver_materials(m, w, matl):
return m.Deliver[w, matl] * big_M >= sum(m.Assign[w, t] * m.matl_reqts[t, matl]
for t in m.T)
m.C3 = pyo.Constraint(m.W, m.M, rule=deliver_materials)
solver = pyo.SolverFactory('glpk')
results = solver.solve(m)
print(results)
print('Assignment Plan:')
for (w, t) in m.Assign.index_set():
if m.Assign[w, t]:
print(f' Assign {w} to task {t}')
print('\nDelivery Plan:')
for w in m.W:
print(f' Deliver to {w}:')
print(' ', end='')
for matl in m.M:
if m.Deliver[w, matl]:
print(matl, end=', ')
print()
print()
Yields:
Problem:
- Name: unknown
Lower bound: 18.4
Upper bound: 18.4
Number of objectives: 1
Number of constraints: 22
Number of variables: 31
Number of nonzeros: 85
Sense: minimize
Solver:
- Status: ok
Termination condition: optimal
Statistics:
Branch and bound:
Number of bounded subproblems: 53
Number of created subproblems: 53
Error rc: 0
Time: 0.008975028991699219
Solution:
- number of solutions: 0
number of solutions displayed: 0
Assignment Plan:
Assign Homer to task A
Assign Homer to task F
Assign Marge to task B
Assign Marge to task E
Assign Flanders to task C
Assign Flanders to task D
Delivery Plan:
Deliver to Homer:
stone, steel, wood,
Deliver to Marge:
stone, wood, concrete,
Deliver to Flanders:
steel, concrete,
[Finished in 562ms]

Related

Calculate total number of batteries used

Assume you have a phone, and several spare batteries.
arr1 => denotes the minutes for which every battery can last.
arr2 => denotes the minutes that every battery takes to be fully charged.
Once the battery is used up, you can charge it immediately and switch to the next fully charged battery.
You must use the batteries in the order the array denotes.
Suppose you will use the phone for x minute, return the number of batteries you will use.
If it is not possible to use the phone, then return -1
Example:
arr1 = [12, 3, 5, 18]
arr2 = [8, 1, 4, 9]
x = 16
output: 3
My Code:
arr1 = [12, 3, 5, 18]
arr2 = [8, 1, 4, 9]
x = 46 # getting correct result when x=16 but not when x=46
def solution(arr1, arr2, x):
import heapq
ready,charge=list(range(len(arr1))),[]
heapq.heapify(ready)
cur=res=0
while cur<x:
while charge and charge[0][0]<=cur:
heapq.heappush(ready, heapq.heappop(charge)[1])
if not ready:
return -1
i=heapq.heappop(ready)
res += 1
cur+=arr1[i]
heapq.heappush(charge,(cur+arr2[i],i))
return res
solution(arr1, arr2, x)
The code is giving an output 7.
But, the correct output is 5.
Here's an alternate function which doesn't involve iterating to find the solution. It computes the number of batteries required by looking at the total runtimes of the array of batteries, dividing x by the total runtime of all the batteries and then looking up the index of run time which will cover the balance (x % total_runtime). I've given a few ways of doing that lookup, dependent on what libraries (if any) are available.
In terms of whether the call can be completed, it looks at whether there is sufficient charge time (in the run time for the other batteries) for each battery before it has to be used again. If not, and the battery has to be used more than once, the call cannot be completed.
def solution(arr1, arr2, x):
# how many batteries?
numb = len(arr1)
# make cumulative sum of battery runtimes
runtimes = [sum(arr1[:i+1]) for i in range(numb)]
total_runtime = runtimes[numb-1]
# figure out how many batteries we need
batts = x // total_runtime * numb
x = x % total_runtime
if x > 0:
batts += bisect.bisect_left(runtimes, x) + 1
# or
# batts += np.searchsorted(runtimes, x) + 1
# or
# batts += next(idx + 1 for idx, value in enumerate(runtimes) if value >= x)
# check if any battery we use has a charge time greater than the total runtime of the other batteries
excess_charge_times = [total_runtime - runtime - arr2[idx] for idx, runtime in enumerate(arr1)]
# if a battery has to be used more than once and doesn't have enough charge time, fail
if any(batts > idx + numb and excess_charge_times[idx] < 0 for idx in range(numb)):
return -1
return batts
It seems you can implement this by simply looping through the sum of arr1 values until you reach x, maintaining a list of ready times for each battery so you can check whether the battery is currently available to use:
def solution(arr1, arr2, x):
numb = len(arr1)
count = 0
now = 0
ready = [0, 0, 0, 0]
while now < x:
batt = count % numb
if ready[batt] > now:
return -1
now += arr1[batt]
ready[batt] = now + arr2[batt]
count += 1
return count
solution([12, 3, 5, 18], [8, 1, 4, 9], 16)
# 3
solution([12, 3, 5, 18], [8, 1, 4, 9], 46)
# 5
solution([12, 3, 2], [6, 1, 1], 20)
# -1

Transportation optimization task with carrier constraints

I have a transportation problem with constraints on the load a robot can carry.
There are robots in different places in a warehouse.
And I am optimizing based on distances from robot to stations where the robot can collect some boxes.
The constraint is that a robot can carry 2 boxes IF the height of the first box is below a threshold.
Otherwise it can carry only a single box.
And every station has only 1 box to take.
I succeeded in planning for robots without the load constraint.
So that every robot can carry 2 boxes.
In the example below robot 1 gets station 2 and 3 and robot 2 gets station 1.
So this is fine.
But I am struggling on how to add the box_heights constraint which affects the loadable boxes on the robot.
How would I design this in pulp?
This is my test inout data:
optimizer costs: [[1, 2, 3], [4, 5, 6]]
optimizer boxes_to_take: {1: 1, 2: 1, 3: 1}
optimizer box_heights: {1: 1, 2: 2, 3: 1}
optimizer available_space: {1: 2, 2: 2}
optimizer station_ids: [1, 2, 3]
Costs explained:
# Stations
# 1 2 3
[1, 2, 3], # S1 Robots
[4, 5, 6], # S2
So the cost from S2 to Station 3 is 6.
And this is my script: ( copy - paste executable python 3.8 code)
import logging
import threading
import typing
from pulp import *
logger = logging.getLogger()
logger.level = logging.INFO
stream_handler = logging.StreamHandler(sys.stdout)
logger.addHandler(stream_handler)
class TransportOptimizer:
def __init__(self):
logging.info(f"Initializing TransportOptimizer")
# Takes stations and robots to optimize and returns a new set of optimized stations
def optimize(self):
station_ids = [1, 2, 3]
robot_ids = [1, 2]
# Creates a dictionary for the number of units a robot can load
available_space = {}
for robot_id in robot_ids:
available_space[robot_id] = 2
# Creates a dictionary for the number of available boxes
boxes_to_take = {}
box_heights = {}
for station_id in station_ids:
boxes_to_take[station_id] = 1
box_heights[station_id] = 1 if station_id % 2 else 2
logging.debug(f"boxes_to_take: {boxes_to_take}")
# Creates a list of costs of each transportation path
costs = [ # Stations
# 1 2 3
[1, 2, 3], # S1 Robots
[4, 5, 6], # S2
]
logging.info(f"optimizer costs: {costs}")
logging.info(f"optimizer boxes_to_take: {boxes_to_take}")
logging.info(f"optimizer box_heights: {box_heights}")
logging.info(f"optimizer available_space: {available_space}")
logging.info(f"optimizer station_ids: {station_ids}")
# The cost data is made into a dictionary
costs = makeDict([robot_ids, station_ids], costs, 0)
# Creates the 'problem' variable to contain the problem data
problem = LpProblem("FullCostOptimizer", LpMinimize)
# Creates a list of tuples containing all the possible routes for transport
Routes = [(robot_id, station_id) for robot_id in robot_ids for station_id in station_ids]
# A dictionary called 'Vars' is created to contain the referenced variables(the routes)
vars = LpVariable.dicts("Route", (robot_ids, station_ids), 0, None, LpInteger)
# The objective function is added to 'problem' first
problem += (
lpSum([vars[robot_id][station_id] * costs[robot_id][station_id] for (robot_id, station_id) in Routes]),
"Sum_of_Transporting_Costs",
)
# The maximum loadable boxes constraints are added to problem for each robot
for robot_id in robot_ids:
problem += lpSum([vars[robot_id][station_id] for station_id in station_ids]) <= available_space[
robot_id], f"sum_of_boxes_robot {robot_id}_can_load"
# The minimum boxes constraints are added to problem for each station
for station_id in station_ids:
problem += lpSum([vars[robot_id][station_id] for robot_id in robot_ids]) >= boxes_to_take[
station_id], "sum_of_boxes_on_station_%s" % station_id
station_id = 1
robot_id = 1
logging.info(
f"robot {robot_id} and station {station_id}: {vars[robot_id][station_id]}, sum={lpSum([vars[robot_id][station_id] for station_id in station_ids]) <= available_space[robot_id]}")
logging.info(f"optimizer problem: {problem}")
# The problem is solved using PuLP's choice of Solver
problem.solve(PULP_CBC_CMD(msg=False))
# The status of the solution is printed to the screen
logging.debug(f"Status: {LpStatus[problem.status]}")
# Each of the variables is printed with it's resolved optimum value
for v in problem.variables():
logging.debug(f"{v.name} = {v.varValue}")
# The optimised objective function value is printed to the screen
logging.debug(f"Total Cost of Transportation = {value(problem.objective)}")
logging.debug(problem.sol_status)
logging.info(f"Total cost: {problem.objective.value()}")
logging.info(f"vars: {problem.variables()}")
for v in problem.variables():
route, robot, station = str.split(v.name, sep='_')
logging.info(f"robot {robot} to station {station} = {v.varValue} {'valid' if v.varValue == 1 else ''}")
if problem.sol_status != 1:
raise RuntimeError(f"Solver failed with status {problem.sol_status}")
if __name__ == '__main__':
optimizer = TransportOptimizer()
optimized_tasks = optimizer.optimize()

Is there a statistical test that can compare two ordered lists

I would like to get a statistical test statistic to compare two lists. Suppose my Benchmark list is
Benchmark = [a,b,c,d,e,f,g]
and I have two other lists
A = [g,c,b,a,f,e,d]
C = [c,d,e,a,b,f,g]
I want the test to inform me which list is closer to the Benchmark. The test should consider the absolute location, but also the relative location for example it should penalize the fact that in list A 'g' is at the start but in the benchmark it is at the end(how far is something from its true location), but also it should also reward the fact that 'a' and 'b' are close to each other in list C just like in the Benchmark.
A and C are always shuffled Benchmark. I would like a statistical test or some kind of metric that informs me that the orderings of list A , B and C are not statistically different from that of the Benchmark but that of a certain list D is significantly different at a certain threshold or p-value such as 5%. And even among the lists A,B and C, the test should perfectly outline which ordering is closer to the Benchmark.
Well, if you come to the conclusion that a metric will suffice, here you go:
def dist(a, b):
perm = []
for v in b:
perm.append(a.index(v))
perm_vals = [a[p] for p in perm]
# displacement
ret = 0
for i, v in enumerate(perm):
ret += abs(v - i)
# coherence break
current = perm_vals.index(a[0])
for v in a[1:]:
new = perm_vals.index(v)
ret += abs(new - current) - 1
current = new
return ret
I've created a few samples to test this:
import random
ground_truth = [0, 1, 2, 3, 4, 5, 6]
samples = []
for i in range(7):
samples.append(random.sample(ground_truth, len(ground_truth)))
samples.append([0, 6, 1, 5, 3, 4, 2])
samples.append([6, 5, 4, 3, 2, 1, 0])
samples.append([0, 1, 2, 3, 4, 5, 6])
def dist(a, b):
perm = []
for v in b:
perm.append(a.index(v))
perm_vals = [a[p] for p in perm]
# displacement
ret = 0
for i, v in enumerate(perm):
ret += abs(v - i)
# coherence break
current = perm_vals.index(a[0])
for v in a[1:]:
new = perm_vals.index(v)
ret += abs(new - current) - 1
current = new
return ret
for s in samples:
print(s, dist(ground_truth, s))
The metric is a cost, that is, the lower it is, the better. I designed it to yield 0 iff the permutation is an identity. The job left for you, that which none can do for you, is deciding how strict you want to be when evaluating samples using this metric, which definitely depends on what you're trying to achieve.

Linear programming solution for minimum number of resources

I am trying to solve this problem using linear programming using Pulp in python.
We have mango packs with each having different number of mangoes.
We should be able to serve the demand using the minimum number of packets and if possible serve the whole bag.
# Packet Names and the count of mangoes in each packet.
mangoe_packs = {
"pack_1": 2,
"pack_2": 3,
"pack_3": 3,
"pack_4": 2
}
For example,
Based on the demand we should get the correct packets. Ie., if the demand is 2, we give the packet with 2 mangoes. If the demand is 5, we serve packets with 2 and 3 mangoes. If your demand is 2 and we don't have any packet with 2 mangoes we can serve packet with 3 mangoes. In this case, we will have one remnant mango. Our purpose is to have the least number of remnant mangoes while serving the demand.
# Packet Names and the count of mangoes in each packet.
mangoe_packs = {
"pack_1": 2,
"pack_2": 3,
"pack_3": 3,
"pack_4": 2
}
Based on the data provided above,
If the demand is 2, The solution is pack_2 (can be pack_4 also).
If the demand is 4, The solution is pack_2 + pack_4.
If the demand is 5, The solution is pack_1 + pack_2
I am new to Linear programming and stuck at the problem. Tried few solutions and they are not working.
I am unable to come up with the correct objective function and constraints to solve this problem. Need help with that. Thank you.
Here is the code I tried.
from pulp import *
prob = LpProblem("MangoPacks", LpMinimize)
# Number of Mangoes in each packet.
mangoe_packs = {
"pack_1": 2,
"pack_2": 3,
"pack_3": 3,
"pack_4": 2
}
# Define demand variable.
demand = LpVariable("Demand", lowBound=2, HighBound=2, cat="Integer")
pack_count = LpVariable.dicts("Packet Count",
((i, j) for i in mangoe_packs.values() for j in ingredients),
lowBound=0,
cat='Integer')
pulp += (
lpSum([
pack_count[(pack)]
for pack, mango_count in mangoe_packs.iteritems()])
)
pulp += lpSum([j], for pack, j in mangoe_packs.iteritems()]) == 350 * 0.05
status = prob.solve()
Thank you.
Here are some considerations:
The variables of the problem are whether or not a pack should be opened. These variables are thus either 0 or 1 (keep closed, or open).
The main objective of the problem is to minimise the number of remnant mangoes. Or otherwise put: to minimise the total number of mangoes that are in the opened packs. This is the sum of the values of the input dictionary, but only of those entries where the corresponding LP variable is 1. Of course, a multiplication (with 0 or 1) can be used here.
In case of a tie, the number of opened packs should be minimised. This is simply the sum of the above mentioned variables. In order to combine this into one, single objective, multiply the value of the first objective with the total number of packets and add the value of this second objective to it. That way you get the right order in competing solutions.
The only constraint is that the sum of the number of mangoes in the opened packs is at least the number given in the input.
So here is an implementation:
def optimise(mango_packs, mango_count):
pack_names = list(mango_packs.keys())
prob = LpProblem("MangoPacks", LpMinimize)
# variables: names of the mango packs. We can either open them or not (0/1)
lp_vars = LpVariable.dicts("Open", pack_names, 0, 1, "Integer")
# objective: minimise total count of mangoes in the selected packs (so to
# minimise remnants). In case of a tie, minimise the number of opened packs.
prob += (
lpSum([mango_packs[name]*lp_vars[name] for name in pack_names]) * len(mango_packs)
+ lpSum([lp_vars[name] for name in pack_names])
)
# constraint: the opened packs need to amount to a minimum number of mangoes
prob += lpSum([mango_packs[name]*lp_vars[name] for name in pack_names]) >= mango_count
prob.solve()
In order to visualise the result, you could add the following in the above function:
print("Status:", LpStatus[prob.status])
# Each of the variables is printed with it's resolved optimum value
for i, v in enumerate(prob.variables()):
print("{}? {}".format(v.name, ("no","yes")[int(v.varValue)]))
Call the function like this:
# Packet Names and the count of mangoes in each packet.
mango_packs = {
"pack_1": 10,
"pack_2": 2,
"pack_3": 2,
"pack_4": 2
}
optimise(mango_packs, 5)
Output (when you added those print statements)
Status: Optimal
Open_pack_1? no
Open_pack_2? yes
Open_pack_3? yes
Open_pack_4? yes
See it run here -- give it some time to temporarily install the pulp module.
Here is a simple model that minimizes the total number of remnant mangoes. Instead of specifying the exact packages available the model just specifies the number of packages available per size (here 5 of size 2 and 15 of size 4):
from pulp import *
# PROBLEM DATA:
demand = [3, 7, 2, 5, 9, 3, 2, 4, 7, 5] # demand per order
packages = [0, 5, 0, 15] # available packages of different sizes
O = range(len(demand))
P = range(len(packages))
# DECLARE PROBLEM OBJECT:
prob = LpProblem('Mango delivery', LpMinimize)
# VARIABLES
assigned = pulp.LpVariable.dicts('assigned',
((o, p) for o in O for p in P), 0, max(demand), cat='Integer') # number of packages of different sizes per order
supply = LpVariable.dicts('supply', O, 0, max(demand), cat='Integer') # supply per order
remnant = LpVariable.dicts('remnant', O, 0, len(packages)-1, cat='Integer') # extra delivery per order
# OBJECTIVE
prob += lpSum(remnant) # minimize the total extra delivery
# CONSTRAINTS
for o in O:
prob += supply[o] == lpSum([p*assigned[(o, p)] for p in P])
prob += remnant[o] == supply[o] - demand[o]
for p in P:
# don't use more packages than available
prob += packages[p] >= lpSum([assigned[(o, p)] for o in O])
# SOLVE & PRINT RESULTS
prob.solve()
print(LpStatus[prob.status])
print('obj = ' + str(value(prob.objective)))
print('#remnants = ' + str(sum(int(remnant[o].varValue) for o in O)))
print('demand = ' + str(demand))
print('supply = ' + str([int(supply[o].varValue) for o in O]))
print('remnant = ' + str([int(remnant[o].varValue) for o in O]))
If the demand cannot be fulfilled this model will be infeasible. Another option in this case would be to maximize the number of orders fulfilled with a penalty for remnant mangoes. Here is the adapted model:
from pulp import *
# PROBLEM DATA:
demand = [3, 7, 2, 5, 9, 3, 2, 4, 7, 5] # demand per order
packages = [0, 5, 0, 5] # available packages of different sizes
O = range(len(demand))
P = range(len(packages))
M = max(demand) # a big enough number
# DECLARE PROBLEM OBJECT:
prob = LpProblem('Mango delivery', LpMaximize)
# VARIABLES
assigned = pulp.LpVariable.dicts('assigned',
((o, p) for o in O for p in P), 0, max(demand), cat='Integer') # number of packages of different sizes per order
supply = LpVariable.dicts('supply', O, 0, max(demand), cat='Integer') # supply per order
remnant = LpVariable.dicts('remnant', O, 0, len(packages)-1, cat='Integer') # extra delivery per order
served = LpVariable.dicts('served', O, cat='Binary') # whether an order is served
diff = LpVariable.dicts('diff', O, -M, len(packages)-1, cat='Integer') # difference between demand and supply
# OBJECTIVE
# primary objective is serve orders, secondary to minimize remnants
prob += 100*lpSum(served) - lpSum(remnant) # maximize served orders with a penalty for remnants
# CONSTRAINTS
for o in O:
prob += supply[o] == lpSum([p*assigned[(o, p)] for p in P])
prob += diff[o] == supply[o] - demand[o]
for p in P:
# don't use more packages than available
prob += packages[p] >= lpSum([assigned[(o, p)] for o in O])
for o in O:
# an order is served if supply >= demand
# formulation adapted from https://cs.stackexchange.com/questions/69531/greater-than-condition-in-integer-linear-program-with-a-binary-variable
prob += M*served[o] >= diff[o] + 1
prob += M*(served[o]-1) <= diff[o]
prob += lpSum([assigned[(o, p)] for p in P]) <= M*served[o]
for o in O:
# if order is served then remnant is supply - demand
# otherwise remnant is zero
prob += remnant[o] >= diff[o]
prob += remnant[o] <= diff[o] + M*(1-served[o])
# SOLVE & PRINT RESULTS
prob.solve()
print(LpStatus[prob.status])
print('obj = ' + str(value(prob.objective)))
print('#served = ' + str(sum(int(served[o].varValue) for o in O)))
print('#remnants = ' + str(sum(int(remnant[o].varValue) for o in O)))
print('served = ' + str([int(served[o].varValue) for o in O]))
print('demand = ' + str(demand))
print('supply = ' + str([int(supply[o].varValue) for o in O]))
print('remnant = ' + str([int(remnant[o].varValue) for o in O]))

Tensorflow : How to perform an operation parallel on every n elements?

What should I do if I want to get the sum of every 3 elements?
test_arr = [1,2,3,4,5,6,7,8]
It sounds like a map function
map_fn(arr, parallel_iterations = True, lambda a,b,c : a+b+c)
and the result of map_fn(test_arr) should be
[6,9,12,15,18,21]
which equals to
[(1+2+3),(2+3+4),(3+4+5),(4+5+6),(5+6+7),(6+7+8)]
I have worked out a solution after reviewing the official docs: https://www.tensorflow.org/api_docs/python/tf/map_fn
import tensorflow as tf
def tf_map_elements_every(n, tf_op, input, dtype):
if n >= input.shape[0]:
return tf_op(input)
else:
return tf.map_fn(
lambda params: tf_op(params),
[input[i:i-n+1] if i !=n-1 else input[i:] for i in range(n)],
dtype=dtype
)
Test
t = tf.constant([1, 2, 3, 4, 5, 6, 7, 8])
op = tf_map_elements_every(3, tf.reduce_sum, t, tf.int32)
sess = tf.Session()
sess.run(op)
[Out]: array([ 6, 9, 12, 15, 18, 21])
It's even easier: use a list comprehension.
Slice the list into 3-element segments and take the sum of each.
Wrap those in a list.
[sum(test_arr[i-2:i+1])
for i in range(2, len(test_arr))]
Simply loop through your array until you are 3 from the end.
# Takes a collection as argument
def map_function(array):
# Initialise results and i
results = []
int i = 0
# While i is less than 3 positions away from the end of the array
while(i <= (len(array) - 3)):
# Add the sum of the next 3 elements in results
results.append(array[i] + array[i + 1] + array[i + 2]
# Increment i
i += 1
# Return the array
return results

Categories

Resources