Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8243439
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T21:31:16+00:00 2026-06-07T21:31:16+00:00

I’m sorry for the title; frankly speaking, I don’t even know whether my question

  • 0

I’m sorry for the title; frankly speaking, I don’t even know whether my question is related to the Knapsack problem. Was reading some stuff about genetic algorithms and found this “Knapsack Problem”.

I need someone to kick me in the right direction:

I am developing a python web application for a factory. So in a factory, they have something called an order. An order contains one or many products. There’s a concept of mismatch, which is actually a negative number used to indicate how much less (in terms of quantity) a particular product will appear in an order.

Think of a matrix with the columns being products and the rows being orders. Just assume that all orders(rows) contain all products(columns). Again, there are 8 orders Orders 1 to Orders 8 and 5 products, Product 1 to Product 5.

Supposing, now I have a mismatch of 6 for Product 1. I need to split the number six equally amongst all 8 of the orders, randomly. So obviously 2 orders won’t have mismatch quantities. Then I have a mismatch of 9 for Product 2. I split the mismatch quantity over 8 Orders as evenly as possible, randomly. This goes on for each Product. Now here comes the kicker, while I am in the process of splitting the mismatches randomly across all Orders, I need to ensure that the total mismatches per order (meaning for that row) are kept at a minimal. This means the total mismatches across an order needs to be the lowest possible number.

    |-----|-----|-----|-----|-----|
    |  P1 |  P2 |  P3 |  P4 |  P5 |
    -------------------------------
O1  |  2  |  1  |  1  |  0  |  2  |  6
    -------------------------------
O2  |  1  |  2  |  1  |  1  |  1  |  6
    -------------------------------
O3  |  2  |  2  |  1  |  0  |  1  |  6
    -------------------------------
O4  |  1  |  2  |  0  |  1  |  1  |  5
    -------------------------------
       6     7     3     2     5

Do you get it? I need to code this in Python, and I have no idea where to start.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T21:31:17+00:00Added an answer on June 7, 2026 at 9:31 pm

    So the OP’s problem has two requirements: randomly and evenly. It’s a bit conflicting though, so I guess “truly random” is impossible.

    Here is my try

    Using OP’s example, we have 4 orders and 5 products. Starting from the first product, we split the numbers randomly, so each product will at least have floor(6/4)=1 mismatches. Then we randomly put the remaining 2 mismatches to 2 products.

        |-----|
        |  P1 |
        -------
    O1  |  2  | 2
        -------
    O2  |  1  | 1
        -------
    O3  |  2  | 2
        -------
    O4  |  1  | 1
        -------
           6  
    

    Next, we take care of the second product. Similarly, we split the numbers randomly first, so each product will at least have floor(7/4)=1 mismatches. Now for the remaining 3 mismatches, to make it as even as possible, we first assign them to O2 and O4, since last time they have 1 less mismatch than others. For the remaining 1 mismatches, we again randomly assign it to one of the product.

        |-----|-----|
        |  P1 |  P2 |
        -------------
    O1  |  2  |  1  | 3
        -------------
    O2  |  1  |  2  | 3
        -------------
    O3  |  2  |  2  | 4
        -------------
    O4  |  1  |  2  | 3
        -------------
           6     7  
    

    Repeat this process for all the products.

    Using this way, you can guarantee that it’s as even as possible (the biggest difference is 1), also you get some degree of random

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
Does anyone know how can I replace this 2 symbol below from the string
I have an MVC Razor view @{ ViewBag.Title = Index; var c = (char)146;
I am currently running into a problem where an element is coming back from
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.