Creating Subsets Constrained Optimization

I am trying to create subsets from a list of data based on some constraints and optimize the subsets. I am trying to figure out how to go about it. I am hoping someone can help me come up with some equations, then Wolfram|Alpha can solve them, then I can follow Wolfram’s process and convert it into the programming language I am using. Here is an example of the problem:

Given:

  • X_POSITIONMID = 3.5
  • List where first item in each subgroup is the X_POSITION and the second item is the STRINGCOUNT: (0 2) (1 3) (2 3) (3 3) (4 3) (5 3) (6 3)

Constraints:

  • Sum of STRINGCOUNT in each subset created must be less than or equal to 9
  • Items in subsets created must be adjacent (based on X_POSITION, e.g. can’t make a subset of (0 2) and (2 3) without also having (1 3) in the subset)
  • Create as few subsets as possible
  • Lastly, this is hard to explain, but basically the subsets with the largest STRINGCOUNT sum shall be nearest to X_POSITIONMID

Example Solution:

  • Subset 1: (0 2) (1 3) (sum of STRINGCOUNT is 5)
  • Subset 2: (2 3) (3 3) (4 3) (sum of STRINGCOUNT is 9)
  • Subset 3: (5 3) (6 3) (sum of STRINGCOUNT is 6)

Subset 2 is closest to the X_POSITIONMID so it has the highest STRINGCOUNT sum. 3 subsets were created which is the lowest number possible. Each subset has a STRINGCOUNT sum less than or equal to 9.