Sabrina
Sabrina

Reputation: 1

How do I set these particular OptaPlanner Constraints for my situation?

I am wanting to set two Constraints in my OptaPlanner example:

  1. Match each Orders Product SKU and assign it to a Stock Location holding that Product SKU.
  2. Have a maximum of 30 orders at one Stock Location at a time, and if there are over 30 orders with the same Product SKU, then assign the rest to another Stock Location with the same Product SKU.

How can I achieve this? Here is one constraint I have tried but doesn't work for me (Shopify is my Orders table):

   Constraint productSKU(ConstraintFactory constraintFactory) {
        
        return constraintFactory
                .from(Shopify.class).join(Shopify.class, Joiners.equal(Shopify::getProductSKU),
                        Joiners.equal(Shopify::getStockLocation))
                .filter((shopify, stockLocation) ->
                        stockLocation.getStockLocation().getProductSKU() == shopify.getProductSKU())
                .reward("Correct Allocated SKU", HardSoftScore.ONE_SOFT);

    }

Also it takes over 10 minutes to solve, how can i speed up solving time?

Any help is appreciated!

Upvotes: 0

Views: 140

Answers (1)

Radovan Synek
Radovan Synek

Reputation: 1029

Assuming you always want the Product SKU to match, I would rather penalize by a hard score level if it doesn't, as such a solution is most likely not feasible.

Now, further assuming that Shopify is your @PlanningEntity, the constraint might be simplified in the following way:

return constraintFactory
            .from(Shopify.class)
            .filter((shopify) ->
                    !Objects.equals(shopify.getStockLocation().getProductSKU()), shopify.getProductSKU()))
            .penalize("Correct Allocated SKU", HardSoftScore.ONE_HARD);

Please note that the code excerpt above is just for an illustration and may not be compatible with your domain classes.

The second constraint you described could be implemented as penalizing by how much any StockLocation exceeds the limit of 30 orders. From the description, it's not clear to me whether this constraint should be hard or soft (is it a must-have or rather an attempt to balance orders across stock locations?).

As for speeding up the solving, please submit a separate question providing more information about the issue, e.g. INFO logging output, solver configuration, or (estimated) number of @PlanninEntity instances and corresponding value instances.

Upvotes: 1

Related Questions