+ Reply to Thread
Results 1 to 17 of 17

Solver - Adding Constraint to maintain the proportion of variables

Hybrid View

piku9290dgp Solver - Adding Constraint to... 11-08-2021, 04:59 PM
MrShorty Re: Solver - Adding... 11-09-2021, 12:18 PM
piku9290dgp Re: Solver - Adding... 11-09-2021, 04:42 PM
bsalv Re: Solver - Adding... 11-09-2021, 06:21 PM
piku9290dgp Re: Solver - Adding... 11-09-2021, 08:47 PM
MrShorty Re: Solver - Adding... 11-09-2021, 09:15 PM
piku9290dgp Re: Solver - Adding... 11-09-2021, 10:35 PM
bsalv Re: Solver - Adding... 11-09-2021, 10:07 PM
MrShorty Re: Solver - Adding... 11-09-2021, 10:50 PM
bsalv Re: Solver - Adding... 11-09-2021, 11:14 PM
Hydraulics Re: Solver - Adding... 11-10-2021, 09:59 AM
piku9290dgp Re: Solver - Adding... 11-10-2021, 08:47 PM
Hydraulics Re: Solver - Adding... 11-11-2021, 04:23 AM
piku9290dgp Re: Solver - Adding... 11-11-2021, 06:44 PM
Hydraulics Re: Solver - Adding... 11-12-2021, 08:41 AM
bsalv Re: Solver - Adding... 11-12-2021, 03:15 AM
piku9290dgp Re: Solver - Adding... 11-12-2021, 11:16 AM
  1. #1
    Registered User
    Join Date
    07-23-2017
    Location
    Peoria, USA
    MS-Off Ver
    2016
    Posts
    55

    Solver - Adding Constraint to maintain the proportion of variables

    Hello Experts,

    I have attached an excel sheet with objectives, variable and constraint defined. My Variable are from Cell C49 to C63. I want to add an constraint to make sure that the proportion of variables to the sum of variables in close to proportion of given forecast qty ( cell B27:B41). How can I add this constraint

    Thanks,
    Attached Files Attached Files
    Last edited by piku9290dgp; 11-09-2021 at 12:31 AM.

  2. #2
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP, 2007, 2024
    Posts
    16,391

    Re: Solver - Adding Constraint to maintain the proportion of variables

    After most of a day, I will venture to respond.

    You have already added a couple of constraints to your solver model, so I'm assuming that you don't have any question about the mechanics of adding constraints. Can you help us understand exactly what part of adding this constraint you are having trouble with? If it helps, I would expect to:

    1) Somewhere in the spreadsheet, calculate "closeness of match" between the two ranges. I did not take the time to fully explore your sheet, but this calculation did not immediately jump out at me.
    2) Because Solver does not include a "constraint to minimum or maximum" option, I would expect to choose a threshold value that makes sense to me, then add a constraint that the "closeness of match" value does not exceed the threshold. Details, obviously, depend on what you are using to calculate "closeness of match."
    3) Perhaps run the model a few different time with different values for the threshold until you understand how the main objective function and the constraint function interrelate.
    4) Evaluate the result(s) to see if progress is being made, or if a different approach needs to be taken.

    Help us understand exactly what you need help with, and we will try to help as best we can.
    Quote Originally Posted by shg
    Mathematics is the native language of the natural world. Just trying to become literate.

  3. #3
    Registered User
    Join Date
    07-23-2017
    Location
    Peoria, USA
    MS-Off Ver
    2016
    Posts
    55

    Re: Solver - Adding Constraint to maintain the proportion of variables

    Thanks Sir,

    Yea, I was trying myself with the constraints that I added. However, what I want is to populate the decision variables (C49:C63) in such a way that the proportion is similar/close to the proportion given in cell A27:A41. In English, I do not want the solver to allocate all available inventory to one/two final product. I want them to allocate in proportion to my need of final good.

    I thought about putting threshold, but i am afraid the solver might say there is no feasible solution.

    The model constraints I tried is working for this one, but I am not sure if it will work all the time. I have to put the model in for loop for about 30 different scenario. so wish for constraints that would work for sure. Hope my explanation is clear. Thanks again

  4. #4
    Forum Expert
    Join Date
    01-25-2011
    Location
    Belgium, Alveringem
    MS-Off Ver
    Excel 2003, 2007, 365
    Posts
    1,419

    Re: Solver - Adding Constraint to maintain the proportion of variables

    I suppose the yellows values need to be integer.
    I added an extra contraint for the max % difference, now it's up to you to finetune A67
    Attached Files Attached Files
    Remember, saying thanks only takes a second or two. Click the little star * below, to give some Rep if you think an answer deserves it.

  5. #5
    Registered User
    Join Date
    07-23-2017
    Location
    Peoria, USA
    MS-Off Ver
    2016
    Posts
    55

    Re: Solver - Adding Constraint to maintain the proportion of variables

    Thanks for response, but with max % as constraint, I might get an infeasible result, if my available is much less, I would have less qty in decision variable, which will increase the absolute difference. I wish to maximize my decision variable but by keeping minimum deviation from the Forecast qty. Thanks

  6. #6
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP, 2007, 2024
    Posts
    16,391

    Re: Solver - Adding Constraint to maintain the proportion of variables

    Max % is only one possible way to measure deviation from Forecast qty. As I asked before, how would you like to calculate deviation from forecast? If you can help us understand how you want to calculate devation from forecast, we should be able to help you program that into the sheet.

  7. #7
    Registered User
    Join Date
    07-23-2017
    Location
    Peoria, USA
    MS-Off Ver
    2016
    Posts
    55

    Re: Solver - Adding Constraint to maintain the proportion of variables

    Hello,

    Please see the attached file , I calculated the deviation. in range ( A49:A63)

    I wish to populate my decision variables D49:D63 by minimizing the deviation from forecast proportion in RangeB27:B41

    Hope that helps. Thanks again
    Attached Files Attached Files

  8. #8
    Forum Expert
    Join Date
    01-25-2011
    Location
    Belgium, Alveringem
    MS-Off Ver
    Excel 2003, 2007, 365
    Posts
    1,419

    Re: Solver - Adding Constraint to maintain the proportion of variables

    with VBA in a loop.
    First a solution without that max%-constraint.
    Then in a loop each time -0.5% and copy the results to line 75
    Attached Files Attached Files

  9. #9
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP, 2007, 2024
    Posts
    16,391

    Re: Solver - Adding Constraint to maintain the proportion of variables

    What is the largest value in A65 that you would accept? I don't have Excel to test with, but What happens if you add a constraint to your Solver model that A65 must be smaller than the largest value you would accept?

  10. #10
    Forum Expert
    Join Date
    01-25-2011
    Location
    Belgium, Alveringem
    MS-Off Ver
    Excel 2003, 2007, 365
    Posts
    1,419

    Re: Solver - Adding Constraint to maintain the proportion of variables

    then you use sum(A49:A63) instead of max(A49:A63).
    When you try to shrink that constraint, perhaps you get unwanted sideaffects.
    Attached Files Attached Files

  11. #11
    Valued Forum Contributor Hydraulics's Avatar
    Join Date
    07-15-2018
    Location
    Udine - Italy
    MS-Off Ver
    Office 365
    Posts
    396

    Re: Solver - Adding Constraint to maintain the proportion of variables

    Here is an alternate take on your problem.

    Instead of minimizing directly the deviation, we bracket our variables between an upper and lower bound (two constraints have been added), and then we seek to minimize this range, while maximizing production.

    Solver is still based on Simplex, the main loop uses the bisection method.

    'Initialization
    Set wks = ThisWorkbook.Worksheets("Sheet1")
    bracket_upp = wks.Range("C43").Value
    bracket_low = 0
    eps = 1 * 10 ^ -5
    Set delta = wks.Range("A46")
    delta.Value = (bracket_upp - bracket_low) / 2
    'First run
    result = SolverSolve(UserFinish:=True)
    Set max_prod = wks.Range("D65")
    best_prod = max_prod.Value
    'Bisection
    While Abs(bracket_upp - bracket_low) > eps
        result = SolverSolve(UserFinish:=True)
        If max_prod.Value <> 0 Then
    ' Save best_prod
            best_prod = max_prod.Value
    ' Save min delta
            best_delta = delta.Value
    ' We must decrease upper bound
            bracket_upp = bracket_upp - (bracket_upp - bracket_low) / 2
        Else
    ' We must increase lower bound
            bracket_low = bracket_low + (bracket_upp - bracket_low) / 2
        End If
        delta.Value = (bracket_upp + bracket_low) / 2
    Wend
    'Final iteration
    delta.Value = best_delta
    result = SolverSolve(UserFinish:=True)

    HTH,

    Francesco
    Attached Files Attached Files
    Aim high or don't even try.
    ---------------------------------
    If your question has been answered, don't forget to mark the thread as SOLVED.
    If you find an answer helpful, click on the star icon at the bottom of the post.

  12. #12
    Registered User
    Join Date
    07-23-2017
    Location
    Peoria, USA
    MS-Off Ver
    2016
    Posts
    55

    Re: Solver - Adding Constraint to maintain the proportion of variables

    Thank you so much for working on it. I just checked this with one scenario, however, I am finding that it does not actually maximizes the production. It started with high production , but with loops reduces the number of finished item produced. I have attached the excel sheet with example. It started wit 100 items but subsequenctly reduces it to 81
    Attached Files Attached Files

  13. #13
    Valued Forum Contributor Hydraulics's Avatar
    Join Date
    07-15-2018
    Location
    Udine - Italy
    MS-Off Ver
    Office 365
    Posts
    396

    Re: Solver - Adding Constraint to maintain the proportion of variables

    You are asking both for maximizing production, and keeping it proportional to forecasting (minimizing deviation).
    The point is, you have two objectives that cannot be compared to each other directly, and you must choose what is the "right" answer for you.
    A production of 125 with a deviation of 9% is better or worse than a production of 119 and a deviation of 6%? (I'm assuming both solutions are feasible, that is, al the other constraints are satisfied.)

    If we wanted only to maximize production, your original setup was fine.
    When we also wish to keep our production proportional to forecasting, we can add this constraint forcing variables to be within a range.
    We solve this maximization problem, find (hopefully) a solution, and then make this range smaller. However, this constraint limits the number of feasible solutions, therefore max production may change as well.
    At each iteration, the values of variables are closer and closer to the ideal proportion given by the forecast. Some are bigger and some smaller, and sum of the deviation keeps shrinking. Sooner or later, we reach a point where one variable will be at the edge of the interval and we cannot go further.

    In your workbook, this happens for the first variable in D49 at 0.3%. If we try to reduce its value from 4 to 3.9, the sum (81) will be smaller, but then second, third and fourth variables would be pushed out of their interval as well.
    So we are forced to reduce them, the sum becomes even smaller, and then it's a race to the bottom that leaves us with the only solution where each variable (and production) is zero. Please note that keeping variables proportional to forecasting may not give you the smaller deviation.

    Keeping your last workbook as reference, the first solution of 100 has a deviation of 43.9%. Is this better or worse then the final one?
    If we could find a solution that has a production of 99 and a deviation of 0%, would it be better or worse?
    And if the deviation of the final solution (81) was 0.8%, hence smaller than what was found, but due to only one variable (thus giving us a solution that is less "proportional"), would it be better or worse?

    Answering these questions may help us find a solution closer to your request.

    HTH,

    Francesco

  14. #14
    Registered User
    Join Date
    07-23-2017
    Location
    Peoria, USA
    MS-Off Ver
    2016
    Posts
    55

    Re: Solver - Adding Constraint to maintain the proportion of variables

    Hi Francesco,

    Thanks again for the detailed response, I understand here are two simaletnous things we are trying to optimize, but the priority is maximize the production build. I made a modification to your code and put an additional condition to exit the loop in case the max production value is less than earlier max production value. In that way, it can try to minimize the deviation while keeping the final production sum same as maximum. Let me know if what you think.

    Also, as I am new to programming , I am hoping that the while loop is not infinite loop, or please let me know if there is any case when it can go infinite.

    Thanks again. I used your code for my actual dataset and my manager loves it :D

  15. #15
    Valued Forum Contributor Hydraulics's Avatar
    Join Date
    07-15-2018
    Location
    Udine - Italy
    MS-Off Ver
    Office 365
    Posts
    396

    Re: Solver - Adding Constraint to maintain the proportion of variables

    Quote Originally Posted by piku9290dgp View Post
    I am hoping that the while loop is not infinite loop, or please let me know if there is any case when it can go infinite.
    I don't think so. Even if you set a negative value in C43, the loop will end. Anyhow, I do hope you are not interested in negative (or imaginary) production.
    If you want to be absolutely sure, add a new declaration
    Dim i as Long
    set it to zero at the first iteration (out of the loop), change the first line of the loop to
    While Abs(bracket_upp - bracket_low) > eps And i < 1000
    and then write
    i = i + 1
    just below the While instruction

    I used your code for my actual dataset and my manager loves it :D
    Glad to hear that!

    I would like to add only one thing. If the workbooks you attached are representative of your real problem, there is a very slim chance you will find a solution with the same production value and a smaller deviation.

    You may also want to check the solution offered by bsalv, maybe it's more suitable for your problem.

    If this takes care of your question, please mark the thread as solved.

    HTH,

    Francesco

  16. #16
    Forum Expert
    Join Date
    01-25-2011
    Location
    Belgium, Alveringem
    MS-Off Ver
    Excel 2003, 2007, 365
    Posts
    1,419

    Re: Solver - Adding Constraint to maintain the proportion of variables

    or you can do it my way.
    You write the results of every loop just beneath that range, compare visual and choice the best option.

  17. #17
    Registered User
    Join Date
    07-23-2017
    Location
    Peoria, USA
    MS-Off Ver
    2016
    Posts
    55

    Re: Solver - Adding Constraint to maintain the proportion of variables

    Hello,

    I like the approach, however, my concern is i need to run the solver for multiple weeks and different region, so not sure if it is effective to visually pick the result. All I need is the maximum production with the least deviation from the given proportion. the priority is to maximize the production, but in case of constraint, not allocate the constraint material to only one finished product.

    Thanks,

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 3
    Last Post: 02-10-2020, 03:06 PM
  2. [SOLVED] Adding Constraint to Solver
    By DAVE201992 in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 04-09-2019, 12:39 PM
  3. Excel Solver: adding ratio as a constraint
    By candirt in forum Excel General
    Replies: 7
    Last Post: 12-07-2015, 05:09 PM
  4. Require help with a solver constraint
    By Nakeysaw in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-30-2015, 03:06 AM
  5. Excel Solver Constraint Help
    By BauceArj in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 09-14-2014, 08:47 PM
  6. BUY-IN Constraint in Solver
    By WhamBam in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 06-07-2012, 02:30 AM
  7. solver constraint
    By jojo in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 02-18-2005, 10:06 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1