How to model conditional summation (like sumif in Excel) in CP-SAT? - optimization

I am trying to model a variant of Job Shop problem in CP-SAT. In this problem, the machines have capacities >=1 so I can NOT directly use AddNoOverlap constraint from CP-SAT.
The decision variables are START and FINISH times of job operations on machines.
I want to ensure that at any given time T on machine M, the number of active operations should be <= capacity at that time.
I am new to Constraint Programming field. Please guide me in how to use the most efficient way to model in Google CP-SAT solver.

Related

Pyomo Mixed Integer Linear Optimization Code to multiple set of variables with binary variables and prices

Helly everyone,
I am a new user to pyomo and have some problem at the moment.
I am trying to develop a multi-time period mixed-integer optimization problem in python with pyomo. I have 4 technologies for which I want to optimize the capacity over 12 periods (1-12). If technology 1 gets chosen in a period technology 2 is not chosen in that period. The same goes for technologies 3 and 4. Each of these technologies has its own price per period. I set up a list for all the variables for each technologies in each period(x11-x124), for the binary variable of each technology in each period and for the price of each technology in each period. However, I am unable to write a working objective function for all these variables.
I would appreciate any help!
Below is the image of the code I have tried. I have also tried. I however get the error: list indices must be integers or slices, not str.
I have also tried first transforming the lists into numpy.arrays. I however then get an error because I cannot use numpy in a pyomo optimization
enter image description here

Fewer Integer planning variables VS More Boolean planning variables

I am trying to model the following problem using Optaplanner:
A product is stored in a central distribution center (CDC) -> Supply
QuantityCDC is the available quantity in the CDC (10)
The product is required by a regional distribution center (RDC) ->
Demand
QuantityRDC is the required quantity by the RDC (5)
Model 1
Planning variable: CDCtoRDCquantity (quantity to be sent from CDC to
RDC) of type Integer
Value Range: [0, min (QuantityCDC, QuantityRDC) = 5]
Objective function: maximize the satisfaction of the RDC
Model 2
Planning variable: SelectOption Boolean variable
List of options: 1, 2, 3, 4 and 5
Constraint : only select one option at maximum (sum SelectOption <= 1)
Objective function: maximize the satisfaction of the RDC
My question is : which one of the models will be faster and scales better? Model 1 using fewer variables of type integer with well-defined value ranges or Model 2 using more variables of Boolean naturally modeling the ranges and having to add a constraint
(The example above is an oversimplification of the real problem that has more constraints: 10-20 regional distribution centers, 1000 products, not enough supply for all demand, not enough trucks,...)
Generally, if you can hard-code a constraint, you should prefer that.
Even the most efficient constraint will take time to process - if your data model makes it impossible for some constraints to be broken, you save time.
That said, both your options sound to me like there will be a whole lot of planning variables. While 2-3 planning variables is manageable, I definitely wouldn't go past that. Consider replacing variables with entities; such as RDCEntity, which has two variables - the CDC, and the quantity taken.

Optimizing Parameters using AI technique

I know that my question is general, but I'm new to AI area.
I have an experiment with some parameters (almost 6 parameters). Each one of them is independent one, and I want to find the optimal solution for maximum or minimum the output function. However, if I want to do it in traditional programming technique it will take much time since i will use six nested loops.
I just want to know which AI technique to use for this problem? Genetic Algorithm? Neural Network? Machine learning?
Update
Actually, the problem could have more than one evaluation function.
It will have one function that we should minimize it (Cost)
and another function the we want to maximize it (Capacity)
Maybe another functions can be added.
Example:
Construction a glass window can be done in a million ways. However, we want the strongest window with lowest cost. There are many parameters that affect the pressure capacity of the window such as the strength of the glass, Height and Width, slope of the window.
Obviously, if we go to extreme cases (Largest strength glass, with smallest width and height, and zero slope) the window will be extremely strong. However, the cost for that will be very high.
I want to study the interaction between the parameters in specific range.
Without knowing much about the specific problem it sounds like Genetic Algorithms would be ideal. They've been used a lot for parameter optimisation and have often given good results. Personally, I've used them to narrow parameter ranges for edge detection techniques with about 15 variables and they did a decent job.
Having multiple evaluation functions needn't be a problem if you code this into the Genetic Algorithm's fitness function. I'd look up multi objective optimisation with genetic algorithms.
I'd start here: Multi-Objective optimization using genetic algorithms: A tutorial
First of all if you have multiple competing targets the problem is confused.
You have to find a single value that you want to maximize... for example:
value = strength - k*cost
or
value = strength / (k1 + k2*cost)
In both for a fixed strength the lower cost wins and for a fixed cost the higher strength wins but you have a formula to be able to decide if a given solution is better or worse than another. If you don't do this how can you decide if a solution is better than another that is cheaper but weaker?
In some cases a correctly defined value requires a more complex function... for example for strength the value could increase up to a certain point (i.e. having a result stronger than a prescribed amount is just pointless) or a cost could have a cap (because higher than a certain amount a solution is not interesting because it would place the final price out of the market).
Once you find the criteria if the parameters are independent a very simple approach that in my experience is still decent is:
pick a random solution by choosing n random values, one for each parameter within the allowed boundaries
compute target value for this starting point
pick a random number 1 <= k <= n and for each of k parameters randomly chosen from the n compute a random signed increment and change the parameter by that amount.
compute the new target value from the translated solution
if the new value is better keep the new position, otherwise revert to the original one.
repeat from 3 until you run out of time.
Depending on the target function there are random distributions that work better than others, also may be that for different parameters the optimal choice is different.
Some time ago I wrote a C++ code for solving optimization problems using Genetic Algorithms. Here it is: http://create-technology.blogspot.ro/2015/03/a-genetic-algorithm-for-solving.html
It should be very easy to follow.

Stuck too early in local minima with tabu and simulated annealing

Trying to generate the Roster of employees however going doing at the level of hour. Resource hour requirements are like 1h at 8, 2 at 9, 7 at 1pm..
After assigning the first 3 resources, it keeps checking solutions are around them via assigning/reassigning them to Slots without trying to assign other employees.
How to troubleshoot this problem? couldn't it be the weights for each constraint/violation? Does it speed it if I implement a quick construction heuristic that fills the slots before handing to local search?
Current configuration consists of first_fit for construction heuristic, hill climbing as first phase till it get stuck then tabu with simulated annealing
Normally a CH assigns all employees before LS starts, which just moves them around but never unassigns anyone. See general phase sequence diagram in chapter "optimization algorithms" in the docs. That's presuming you don't apply overconstrained planning (nullable=true or null in the value range).
If you do apply overconstrained planning, that you need to make sure that the score cost of leaving an employee unassigned is worse than the score cost of however he's could be assigned.
Also set up a benchmarker config, so you have some benchmark report graphs to allow you to understand what's going on.

Can AMPL handle this recursively or is a remodeling neccessary?

I'm using AMPL to model a production where I have two particular constraints that I am not very sure how to handle.
subject to Constraint1 {t in T}:
prod[t] = sum{i in I} x[i,t]*u[i] + Recycle[f]*RecycledU[f];
subject to Constraint2 {t in T}:
Solditems[t]+Recycle[t]=prod[t];
EDIT: where x[i,t] is the amount of products from supply point i. u[i] denotes the "exchange rate" of the raw material from supply point i to create the product. I.E. a percentage of the raw material will become the finished products, whereas some raw material will go to waste. The same is true for RecycledU[f] where f is in F, which denotes the refinement station where it has been refined. The difference is that RecycledU[f] has a much lower percentage that will go to waste due to Recycled already being a finished product from f (albeitly a much less profitable one). I.e. Recycle has already "went through" the process of being a raw material earlier, x, but has become a finished product in some earlier stage, or hopefully (if it can be modelled) in the same time period as this. In the actual models things as "products" and "refinement station" is existent as well, but I figured for this question those could be abandoned to keep it more simple.
What I want to accomplish is that the amount of products produced is the sum of all items sold in time period t and the amount of products recycled in time period t (by recycled I mean that the finished product is kept at the production site for further refinement in some timestep g, g>t).
Is it possible to write two equal signs for prod[t] like I have done? Also, how to handle Recycle[t]? Can AMPL "understand" that since these are represented at the same time step, that AMPL must handle the constraints recursively, i.e. compute a solution for Recycle[t] and subsequently try to improve that solution in every timestep?
EDIT: The time periods are expressed in years which is why I want to avoid having an expression with Recycle[t-1].
EDIT2: prod and x are parameters and Recycle and Solditems are variables.
Hope anyone can shed some light into this!
Cenderze
The two constraints will be considered simultaneously (unless you explicitly exclude one from the problem). AMPL or optimization solvers don't have the notion of time steps and the complete problem is considered at the same time, so you might need to add some linking constraints between time periods yourself to model time periods. In particular, you might need to make sure that the inventory (such as the amount finished product is kept at the production site for further refinement) is carried over from one period to another, something like:
Recycle[t + 1] = Recycle[t] - RecycleDecrease + RecycleIncrease;
You have to figure out the expressions for the amounts by which Recycle is increased (RecycleIncrease) and decreased (RecycleDecrease).
Also if you want some kind of an iterative procedure with one constraint considered at a time instead, then you should use AMPL script.