'In R , a calculation question like Solver

There are warehouse figures in below dataframe, currently know the data ship_in and ship_out_forecast. Now I want to fix ship_out_forecast figures, make it's reasonable (the result as 'ship_out_forecast_checked' in attached image)

Below are the constraint condition:

  1. cumsum(ship_in)-cumsum(ship_out_forecast_checked)>= 0
  2. ship_out_forecast_checked>0
  3. 'ship_out_forecast_checked'<='ship_out_forecast'
library(tidyverse)
mdate <- c(44593,44594,44595,44596,44597,44598,44599,44600,44601,44602,44603,44604,44605,44606,44607,44593,44594,44595,44596,44597,44598,44599,44600,44601,44602,44603,44604,44605,44606,44607)
category <- c("A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","B","B","B","B","B","B","B","B","B","B","B","B","B","B","B")
ship_in <- c(20,0,0,0,5,0,0,0,0,7,0,0,0,0,0,1,0,0,7,30,0,0,0,0,25,0,0,0,0,0)
ship_out_forecast <- c(12,15,13,11,3,5,8,14,8,11,1,9,16,9,3,29,10,28,0,21,12,23,22,23,19,1,2,16,15,9)

warehouse_data <- data.frame(mdate,category,ship_in,ship_out_forecast) %>% 
  mutate(mdate=as.Date(mdate,origin='1900-1-1'))

enter image description here



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source