mardi 25 septembre 2018

Dynamically generate numbers that meet criteria

I feel like there is an elegant Python solution for this problem that I've been trying to solve with VBA. Can someone please help?

How can I generate a list of numbers that meet the following criteria:

  1. Are all integers
  2. The total is first split between types.
  3. The numbers are further split into subtypes and optimized to meet the required percentages.
  4. The total sum does not exceed the specified Total.

An example of this problem: You plan to build a maximum of 102 residential units.

40% of them will be Studios and 60% will be Townhouse type of construction type. (Could have more or less types)

There are two Priority lists for the future occupants for the apartments: Assisted and Unassisted. The unit allocation percentages in the Assisted(A) list are required to be fully met and the Unassisted(U) list is flexible.

We'd like the Unassisted(U) list to be up to 20% of the Total units if possible, but we definitely need a minimum of 80% of the Total units to be Assisted(A) units.

The Assisted(A) units are a total of 102x80%= 81.6 units. (Already not an integer...) A minimum of 10% of the Assisted(A) list units must be for elderly individuals. A minimum of 40% of the Assisted(A) list units must be for families. The remaining 50% of the Assisted(A) list units are for aspiring data scientists.

The remaining 20% of the Total units are Unassisted(U) occupancy units.

So:

  • Total: 102 units.
  • Construction_type_categories=[.4, .6] (102 units split into Studios and Townhouses: 40% and 60%)
  • Assisted=[.1,.4,.5] (80%+ of the total units are Assisted, further categorized as elderly, family, other occupancy according to the percentages in the list)
  • Unassisted units are the remaining units that were not part of the 80%+ of the Assisted list. (up to 20% of total units)

Result:

[4,16,12,8,7,25,19,11]

Breakdown:

  • 4 Studio Units Assisted for elderly
  • 16 Studio Units Assisted for families
  • 12 Studio Units for other Assisted occupancy type
  • 8 Studio Units (Unassisted)
  • 7 Townhouse Units Assisted for elderly
  • 25 Townhouse Units Assisted for families
  • 19 Townhouse Units for other Assisted occupancy type
  • 11 Townhouse Units (Unassisted).

I thought of first generating a preliminary array of rounded numbers and then looping through and making adjustments. It looked so tedious I've started considering generating a large matrix of numbers using numpy and filtering by the outlined criteria.

It has been time consuming to generate and optimize the numbers manually so I appreciate any help with a better solution.

Thank you




Aucun commentaire:

Enregistrer un commentaire