简体   繁体   中英

Constrained Optimization Solver in Python

I need help formulating my problem as a constrained optimization problem in Python.

Suppose I have a Pandas DataFrame of videos that have the following columns

id, views, score

  1. id is a unique identified for each video
  2. views is the number of times a video has been viewed
  3. score is the output of a function f that computes a quality score for a video. Its implementation is not important for this problem.

I now have 2 separate but related optimization functions I want to implement

  1. Given a max number of videos C , and a minimum total number of views V find the set of videos that maximize the average score.
  2. Given a max number of videos C and a minimum quality score Q find that set of videos that maximize the total number of views.

For instance, assume the following data

+----+-------+-------+
| ID | Views | Score |
+----+-------+-------+
| X  |     1 |   0.9 |
| Y  |     2 |   0.8 |
| Z  |     3 |   0.7 |
+----+-------+-------+

If we were doing optimization 1 above with constraints the following table would summarize results with different criteria:

+-------------+----------------+------------------+
| C Less Than | V Greater Than | Resultant Videos |
+-------------+----------------+------------------+
|           3 |            0.5 | X                |
|           3 |            2.5 | X, Y             |
|           3 |            4.5 | Y, Z             |
+-------------+----------------+------------------+

If we were doing optimization 2 above with constraints the following table would summarize results with different criteria:

+-------------+----------------+------------------+
| C Less Than | Q Greater Than | Resultant Videos |
+-------------+----------------+------------------+
|           3 |           0.85 | X                |
|           3 |           0.75 | X, Y             |
|           3 |           0.95 | No Solution      |
+-------------+----------------+------------------+

I feel like the answer lies somewhere within scipy's optimize library. It should be noted that this is a 0-1 knapsack problem and not a fractional knapsack problem .

Thanks

Your problem does not seem to be well formulated. In terms of linear programming (optimization) you could formulate it as

\max b_1 \sum_i c_i/N+b_2 \sum_i v_i

such that \alpha c\geq \bar{c}
          \beta v\leq \underline{v}
          \gamma q\leq \underline{q}

For fixed scalars b_1, b_2, N and vectors alpha, beta, gamma.

In the first line, you maximize simultaneously the average score c_i and the total number of views v_i.

Now, you can solve this problem using scipy's optimize.

Ps.: Sorry for the formatting, I am quite new on Stack Exchange

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM