emadehsan / csp

Algorithm for Cutting Stock Problem using Google OR-Tools. Link to the tool:
https://emadehsan.com/csp/
MIT License
129 stars 38 forks source link

Add limit #7

Closed parthshah27 closed 3 years ago

parthshah27 commented 3 years ago

image As per current flow, you have allowed multiple cuts on single rolls but I want to make it limited i.e I want to make only 7 cuts on a single roll after that it should throw an error. I had attached a screenshot where I highlighted a part where I need to put a limit of 7 instead of any number of cuts. So can you please suggest to me how can I achieve it? @emadehsan

parthshah27 commented 3 years ago

Can anyone please help me with how can I set fix limit on cuts?

emadehsan commented 3 years ago

Hi @parthshah27, At the moment the Cutting Stock Planner tool cannot support this.

You can implement this requirement in code. Actually you have to add a constraint to the solver that:

Find solutions in which the stock roll can have at max 7 cuts.

For example, this is the code that adds the max size constraint:

    # total width of small rolls cut from j-th big roll, 
    # must not exceed big (stock) roll's width
    solver.Add( \
        sum(demands[i][1]*x[i][j] for i in range(num_orders)) \
        <= parent_width*y[j] \
      )

Similar to the above code, you can add your max cuts constraint / requirement probably as follows:

In the code, you'll see that x is our decision matrix i.e. it will contain the decisions / solution given by our solver. The code explains x as:

  # x[i][j] = 3 means that small-roll width specified by i-th (customer) order
  # must be cut from j-th stock item, 3 tmies 
  # initialize x:
  x = [[solver.IntVar(0, b[i], f'x_{i}_{j}') for j in range(k[1])] \
      for i in range(num_orders)]

To add max cut limit, you can probably do this:

    solver.Add( \
        # all the customer orders cut from j-th stock road 
        # must be less than or equal to 7
        sum(x[i][j] for i in range(num_orders)) <= 7 \
      )

I highly recommend read the whole code and understand before making changes. I've tried my best to explain it through comments and video tutorial.

parthshah27 commented 3 years ago

Thank you so much @emadehsan for replying. Your code helped me a lot to fulfill my requirements and this limit problem solution is not worked for all use-case but will share my logic once I succeed. So in the future, if anyone gets this same issue then it can help them also.

emadehsan commented 3 years ago

@parthshah27 do share your solution :)

parthshah27 commented 3 years ago

@emadehsan Currently I'm working on a solution I'll share it once it is verified for all of the use-case.