简体   繁体   中英

Dynamic Programming in Python - Optimal sequence

I am trying to solve this problem and unable to come up with a robust solution. Any idea, pseudo-code or a python implementation would be greatly appreciated. For sake of simplicity, consider a small matrix like in Figure 1. The rows in the matrix represent days and the columns represent minutes. We can assume that a bus travels between two points that takes 10 minutes and stops at a particular cell defined by a letter in that cell at each minute. Given the historical pattern (day 1 thru 5), we want to find the best sequence of letters. To do that we need to follow certain rules:

  1. We want to select the most frequently observed letter per minute interval. If there is more than one letter with the same frequency, we can select any of them.
  2. We want to maintain the continuity.
  3. We want to preserve the original sequence the best we can.

We are not looking for the shortest distance (most straight line, etc.)

Here are a couple examples: 图1 The sequence in Figure 1 satisfies all these rules. The highlighted sequence is just for visualization purpose. There are other ways of visualizing this sequence in Figure 1.

图2 The sequence in Figure 2 is discontinuous. Hence the most frequent letters can't be stitched together. For that reason, we select the second most frequent letter in minute 3, one of the C, A, D instead of B. With that we can satisfy the rules. However, keep in mind, when 365 days used along with 100+ minutes, it gets complex. For instance, using the second most frequent letter may have resulted in rewiring the rest of the sequence.

Any guidance is highly appreciated.

This sounds like a relative straightforward dynamic programming task.

  • Start at the end: each cell in the last column gets 0 if it is the most frequent letter or 1 otherwise.
  • Move on to the second last column. Each cell gets 0 if it is the most frequent letter or 1 other + min(cell_above, cell_directly_right, cell_below). Note which cell you selected.
  • Repeat until you reach the end.
  • You will now have in the first column one or more cells with minimal value. Follow the cells you noted in step 2.
  • You now have a path from the beginning to the end which is continous and minimizes sum([0 if cell.most_frequent else 1 for cell in cells])

You might have to tweak the target function eg the last most frequent and the second most frequent letter are treated the same. Maybe you want to give a score based on how frequent they are.

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