+ Reply to Thread
Results 1 to 31 of 31

Programming with Solver

  1. #1
    Registered User
    Join Date
    10-09-2006
    Posts
    21

    Smile Programming with Solver

    Any help on this would be GREATLY appreciated. I am trying to have a solver program solve problems based on certain constraints placed on the cells. This is the code that has been generated:

    -----------------------------------------------------------------------
    Sub Format_Solver_DRP()
    ' Format_Solver_DRP Macro
    ' Macro recorded 09/14/2006 by Lynn Holt
    Dim row_num As Double
    'Set row_num to starting row in worksheet
    row_num = 2
    SOLVER.Auto_open
    Do
    'SOLVER.SolverReset
    'Run solver for each row independently
    'Check to see if row/cell in B column has a value or not
    If Range("B" & row_num).Value = "" Then
    Else
    Worksheets("Sheet2").Activate
    'Clear solver parameters
    'Set parameters
    SolverOk SetCell:=Range("P" & row_num).Address, MaxMinVal:=2, ValueOf:="0", ByChange:=Range("F" & row_num, "G" & row_num).Address
    'Set constraints

    SolverChange CellRef:=Range("F" & row_num).Value, Relation:=4, FormulaText:="integer"
    SolverChange CellRef:=Range("G" & row_num).Value, Relation:=4, FormulaText:="integer"
    SolverChange CellRef:=Range("M" & row_num).Value, Relation:=2, FormulaText:=Range("N" & row_num).Value
    SolverChange CellRef:=Range("G" & row_num).Value, Relation:=3, FormulaText:="0"
    SolverChange CellRef:=Range("F" & row_num).Value, Relation:=3, FormulaText:="0"
    SolverOk SetCell:=Range("P" & row_num).Value, MaxMinVal:=2, ValueOf:="0", ByChange:=Range("F" & row_num, "G" & row_num).Address
    'SOLVER.SolverSave
    'Run solver
    SolverSolve UserFinish:=True
    'Keep results
    'SOLVER.SolverFinish KeepFinal:=1
    row_num = row_num + 1
    End If
    'Increase row number by 1

    'Break loop if row/cell is null or blank
    Loop Until Range("B" & row_num).Value = ""
    Dim exit_loop As Boolean

    -----------------------------------------------------------------------

    The problem is, the first row (f2 and g2) will run correctly, but the following rows don't seem to follow the same constraints. The other odd thing, is if you start the macro at the f3 and g3, it will work for that row, but not the rows that follow. Is there something wrong with the code that isn't allowing the constraints to follow the cells? Please help!

  2. #2
    Registered User
    Join Date
    10-09-2006
    Posts
    21
    bump.....................

  3. #3
    Forum Contributor
    Join Date
    11-11-2005
    Posts
    267
    Check the formula in the Target cell Range("P" & row_num).Address. If unresolved post file.
    Last edited by Myles; 10-10-2006 at 10:20 AM.
    HTH
    Myles

    ...constantly looking for the smoother pebble while the whole ocean of truth lies before me.

  4. #4
    Registered User
    Join Date
    10-09-2006
    Posts
    21
    I am not sure what im looking for in that expression that could be wrong? Please advise. Thank you for replying!

  5. #5
    Registered User
    Join Date
    10-09-2006
    Posts
    21
    attaching code as a txt.
    Attached Files Attached Files

  6. #6
    Forum Contributor
    Join Date
    11-11-2005
    Posts
    267
    Try this edited version. You had SOLVERCHANGE instead of SOLVERADD in setting the constraints. I have recast the ranges using Cells Method for clarity and also supplied a range to copy solver results to. Make sure rolling Cells(i,"P") contain formulas linking your constrained ranges.

    Sub Format_Solver_DRP()
    Dim i As Long
    SolverReset
    'Set i to starting row in worksheet
    i = 2
    'SOLVER.Auto_open
    Do
    If Cells(i, "B").Value = "" Then
    Else
    Worksheets("Sheet1").Select
    'Set parameters
    SolverOk SetCell:=Cells(i, "P"), MaxMinVal:=2, ValueOf:="0", ByChange:=Range(Cells(i, "F"), Cells(i, "G"))
    'Set constraints

    SolverAdd Cells(i, "F").Value, 4, "integer"
    'SolverAdd Range(i, "F").Value, 3, "0" '===> this "duplicates" the constraints on Cells(i,"F"); are the 2 compatible?
    SolverAdd Cells(i, "G").Value, 4, "integer"
    'SolverAdd Cells(i, "G").Value, 3, "0" '===> again, this "duplicates" the constraints on Cells(i,"G"); are the 2 compatible?
    SolverAdd Cells(i, "M").Value, 2, Cells(i, "N").Value

    SolverSolve UserFinish:=True

    'Collect results in columns X and Y
    Range(Cells(i, "X"), Cells(i, "Y")).Value = Range(Cells(i, "F"), Cells(i, "G")).Value
    'SOLVER.SolverFinish KeepFinal:=1
    i = i + 1
    End If
    Loop Until IsEmpty(Cells(i, "B"))

    End Sub
    Last edited by Myles; 10-10-2006 at 02:25 PM.

  7. #7
    Registered User
    Join Date
    10-09-2006
    Posts
    21
    Attached is the code we used by using your baseline. We didn't get an output, so I dont know if it worked or not. Could you take a look at our modifications?



    Thanks again for helping!
    Attached Files Attached Files

  8. #8
    Forum Contributor
    Join Date
    11-11-2005
    Posts
    267
    Try attached code in txt.file. If you're still having problems send your spreadsheet.
    Attached Files Attached Files

  9. #9
    Registered User
    Join Date
    10-09-2006
    Posts
    21
    It is not outputing anything. It is adding the constraints though now for each row, but I think to get the correct results, wouldnt we only want the constraints to be listed in the constraints box for the row we are dealing with?


    How can I get the excel file to you? The forums wont let me upload an excel file.

  10. #10
    Forum Contributor
    Join Date
    11-11-2005
    Posts
    267
    Not knowing the struture of your layout and the interconnectivity between the various sets of ranges, I have trumped up a wild scenario to link the target cell to the Changing Range. The code outputs without any hitch.

    See my excel demo file attached, another way to debunk your suggestion that the forum forbids transmission of excel files. Make sure the file size is less than 100k. Use zip to reduce size.
    Attached Files Attached Files
    Last edited by Myles; 10-10-2006 at 03:05 PM.

  11. #11
    Registered User
    Join Date
    10-09-2006
    Posts
    21
    Thank you, I gave it to my boss for reference. Here is our spreadsheet as you have requested.
    Attached Files Attached Files

  12. #12
    Forum Contributor
    Join Date
    11-11-2005
    Posts
    267
    Code is running and delivering outputs. However, you may have to have a critical look at the reasonableness of any constraints you may wish to impose. I for instance, discovered that, given the current structure of your table, Solver has difficulty setting the values in Column G to "Integer". Some sort of overall rationalisation is needed based on the nature of data you are analyzing. see file attached
    Attached Files Attached Files

  13. #13
    Registered User
    Join Date
    10-09-2006
    Posts
    21
    might that be why we are getting negative values? Based on the constraints, we shouldnt be getting any negative values....

    My boss is looking at the code now. We are getting ready to leave for the day so I will respond in the morning. Thanks again for all of your help it is greatly appreciated!

  14. #14
    Registered User
    Join Date
    10-09-2006
    Posts
    21
    Also, i just noticed this, but production quantity and shipped quantity should be the same. I dont know if we have the constraint coded for that though.

  15. #15
    Registered User
    Join Date
    10-09-2006
    Posts
    21
    Also, you see the blue results box that says the first ilne should be 26 and 29? That is correct, but the lines below it are not. That has been our problem, the first line always runs correctly, but the following lines do not return the right values.

  16. #16
    Registered User
    Join Date
    10-09-2006
    Posts
    21
    F + G should = M which should = O

    this is solved by getting the best combination of F and G to get P to equal as close to 0 as possible.

  17. #17
    Registered User
    Join Date
    10-09-2006
    Posts
    21
    ready to work on this again!

  18. #18
    Registered User
    Join Date
    10-09-2006
    Posts
    21
    What version of excel are you running? The spreadsheet you gave me will not compile?

  19. #19
    Forum Contributor
    Join Date
    11-11-2005
    Posts
    267
    F + G should = M which should = O
    What is the formula in Column M and Column N? From the spreadsheet you sent Column N adds up Columns F & G. How does that stand against F + G should = M? Something of a mixup....

    Again, constraining the values in Columns F & G to be integers doesn't seem right. Do you mean to constrain their values to be GREATER THAN or EQUAL to zero? That sounds more meaningful. Get the basics right and everything will fall into place. It's not a difficult optimization case.

  20. #20
    Registered User
    Join Date
    10-09-2006
    Posts
    21
    Sorry about that, I meant F + G should = N which should equal M

    F and G should be greater than or equal to zero, as everything should.

  21. #21
    Forum Contributor
    Join Date
    11-11-2005
    Posts
    267
    Do you also know that column N contains a formula that adds up Columns F &G? Surely, you wouldn't need to have Columns M and N containing the same formula.

  22. #22
    Registered User
    Join Date
    10-09-2006
    Posts
    21
    You are right, it would be pointless to have two columns with the same formula. If you look, column M is static. It's values are typed in by the user. Column N is populated as a check.

    M is titled production quantity, which is how many pieces are produced. F and G are basically the columns showing what needs to be shipped based on what site they are coming from. Column N adds those two together.

  23. #23
    Forum Contributor
    Join Date
    11-11-2005
    Posts
    267
    See file attached for a solution to your problem. Post back any further problems, if any.
    Attached Files Attached Files

  24. #24
    Registered User
    Join Date
    10-09-2006
    Posts
    21
    Can you send us your solver.xla? No data comes up when we hit the button. The results look good though.

  25. #25
    Forum Contributor
    Join Date
    11-11-2005
    Posts
    267
    File attached again. See code as requested.

    Sub FormatSolver()

    Dim i As Long
    SolverReset

    i = 2
    Do

    Worksheets("Sheet2").Select
    'Set parameters
    SolverOk SetCell:=Cells(i, "P"), MaxMinVal:=2, ValueOf:="0", ByChange:=Range(Cells(i, "F"), Cells(i, "G"))
    'constraints
    SolverAdd Cells(i, "F"), 3, "0.0000000001"
    SolverAdd Cells(i, "G"), 3, "0.0000000001"
    SolverAdd Cells(i, "N"), 2, Cells(i, "M")

    SolverSolve UserFinish:=True
    'Collect results in columns Q and R
    Range(Cells(i, "Q"), Cells(i, "R")).Value = Range(Cells(i, "F"), Cells(i, "G")).Value
    'clear constraints
    SolverDelete Cells(i, "F"), 3, "0.0000000001"
    SolverDelete Cells(i, "G"), 3, "0.0000000001"
    SolverDelete Cells(i, "N"), 2, Cells(i, "M")

    i = i + 1
    Loop Until IsEmpty(Cells(i, "B"))
    SolverReset
    End Sub
    Attached Files Attached Files

  26. #26
    Registered User
    Join Date
    10-09-2006
    Posts
    21
    Still no dice. If we take out what is in cells f and g to run it, no results are displayed when we hit the button. Can you send us your solver.dll and solver.xla?

  27. #27
    Forum Contributor
    Join Date
    11-11-2005
    Posts
    267
    find attached.

  28. #28
    Forum Contributor
    Join Date
    11-11-2005
    Posts
    267
    sorry,

    file size of solver addin + dll exceed max limit for transmission even after zipping.

  29. #29
    Forum Contributor
    Join Date
    11-11-2005
    Posts
    267
    One more thing. Have you checked the reference to SOLVER in the VBEditor? As you well know, it needs to be set for the macro to run Solver.

  30. #30
    Registered User
    Join Date
    10-09-2006
    Posts
    21
    Yes, we have. Not sure what the problem is then...

  31. #31
    Registered User
    Join Date
    10-09-2006
    Posts
    21
    What version of Excel are you using?

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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