+ Reply to Thread
Results 1 to 7 of 7

Find optimal values to satisfy constraints

  1. #1
    Registered User
    Join Date
    05-03-2021
    Location
    London
    MS-Off Ver
    Office 365
    Posts
    8

    Find optimal values to satisfy constraints

    Hello everyone!

    I am dealing with one problem in Excel. I am attaching a workbook. What I basically have is a column with measured physical data, column A. If you plot that data, it is not very smooth and has lot of spikes. So the goal is to make a new column with smoother data and that would be in column B. This would however follow the same pattern, so increasing where A increases and decreasing where A decreases, but it would be smoother. Maybe like a 6 point average. However, this cannot be just smoother however a person likes, because other values are calculated from column B. And the constraint is that column G (Slue) must be 0 at the last two values (row 44 and 45).

    What people currently do is that they try to manually make column B so that they get two 0s at the end of column G. They basically try different values until they get it right. However, this sometimes takes hours as even a little change causes the G column values to deviate by a lot. I was wondering if there is a way for Excel to calculate the best possible values for B column such that the last 2 values in G column are 0?

    B column has to be integers only. The current workbook already contains a B column with values that satisfy the constraint and this would be a good solution. I obtained it from MATLAB by looping through multiple permutations and was lucky enough to land on a good solution since I can only do permutations of 10 different values at any time. Let me know if you think there is a way to do this in Excel!

    Thank you

    Auxcron
    Attached Files Attached Files

  2. #2
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP and 2007
    Posts
    15,819

    Re: Find optimal values to satisfy constraints

    I am optimistic that it can be done in Excel. At this point, it seems that it is more about algorithm development (which is outside of Excel) than programming. If you can help us understand the algorithm you want to use, we are usually pretty good at helping you program that algorithm into the spreadsheet. We are not always as effective at coming up with the algorithm from scratch.

    It appears that you are currently using some kind of brute force try random permutations until you find one that you like. With 40ish different values to change over an undetermined range (only that it be "smoother" than the current set of values), there are a lot of possible permutations to try. Are you content with this kind of algorithm? What kind of algorithm do you use to generate new values? How are you measuring "smoothness"?

    Something involving Excel's Solver might work, but that would necessitate coming up with suitable objective and constraint formulas (like how to quantify smoothness).
    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
    05-03-2021
    Location
    London
    MS-Off Ver
    Office 365
    Posts
    8

    Re: Find optimal values to satisfy constraints

    Quote Originally Posted by MrShorty View Post
    I am optimistic that it can be done in Excel. At this point, it seems that it is more about algorithm development (which is outside of Excel) than programming. If you can help us understand the algorithm you want to use, we are usually pretty good at helping you program that algorithm into the spreadsheet. We are not always as effective at coming up with the algorithm from scratch.

    It appears that you are currently using some kind of brute force try random permutations until you find one that you like. With 40ish different values to change over an undetermined range (only that it be "smoother" than the current set of values), there are a lot of possible permutations to try. Are you content with this kind of algorithm? What kind of algorithm do you use to generate new values? How are you measuring "smoothness"?

    Something involving Excel's Solver might work, but that would necessitate coming up with suitable objective and constraint formulas (like how to quantify smoothness).
    Thanks for your message, I appreciate it. Indeed this current algorithm is not feasible. I have got another example where it simply did not find a solution. Right now my algorithm first computes a 6 point average to give it a good starting point from which further adjustments can be made (This is already quite smooth). This solution is pretty close to resulting in the last two S values being 0. Now the next thing I do is that I choose start from the first point and take 10 digits to the right of this array and compute all the possible permutations of these 10 numbers, keeping numbers 11-42 the same. The range through which I do permutations is array number +(-1, 0, 1) so it either adds one to already existing number in array, does nothing or takes away one. If it does not find a solution within these first 10 numbers, it moves to do permutations on numbers 2-11, keeping number 1 and numbers 12-42 constant. This goes all the way to the end and stops once a solution is found. The reason why I do the permutations over 3 values -1, 0 and 1 is that since 6-point average is quite a good approximation, it only needs a little bit of adjustment somewhere, one value down or up. However, this is not going to work all the time as sometimes the 10 permutations are simply not enough and the solution doesn't lie within this method.

    I was thinking of using least squares method to maybe compute a smooth graph but I wasn't able to do it in MATLAB using integers. I think a constraint for smoothness could be defined as how far a number is away from the 6-point moving average at a certain position. So for example I could compute 6-point average at the beginning and then say that the result can not lie more than +/-1 away from this average at any given point.

    I also think that the solver is a way to go but not sure how to implement it in Excel for this problem and to be honest I have never used it before.

  4. #4
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP and 2007
    Posts
    15,819

    Re: Find optimal values to satisfy constraints

    If you are unfamiliar with Solver, then maybe this will be a suitable introduction: https://www.mrexcel.com/excel-tips/i...ion-to-solver/

    As explained, you will need an "objective function" -- a cell containing a formula that you want Solver to find the max/min or target a value. In this case, it would be some formula that means "last two cells in column G (or E) are 0".
    You will need to identify your by changing cells. Your latest description suggests a column of adjacent cells that will take on the -1,0,+1 values for the deviation from the moving average values.
    You will need to set constraints on the by changing cells that will limit those cells to integers between +1 and -1.

    And then see how it does. With 45 variable cells constrained to integers, this could take a while for Solver to work through the different combinations and see if it can find a solution.

  5. #5
    Registered User
    Join Date
    05-03-2021
    Location
    London
    MS-Off Ver
    Office 365
    Posts
    8

    Re: Find optimal values to satisfy constraints

    Hi Mrshorty,

    thank you for your answer! I now have my objective function and all the constraints that I would need. Could you give me a hand on how I can write it into the Excel so I can use Excel solver?
    My objective function is: obj = 2 * sum((DV(2:N) - DV(1:N-1)).^2) + 1/2 * sum((EV - DV).^2);
    and my constraints are:
    constraints = [constraints, d == DV-EV];
    constraints = [constraints, d(1) == 0];
    constraints = [constraints, ED(1) == 0];
    constraints = [constraints, M(1) == 0];
    constraints = [constraints, S(1) == 0];
    % the constraint below is related to the -3 to 3 limit on delta DV
    constraints = [constraints, -2 <= DV(2:N) - DV(1:N-1) <= 2];
    constraints = [constraints, ED(2:N) == ED(1:N-1) + d(1:N-1)];
    constraints = [constraints, M(2:N) == ED(2:N) + M(1:N-1)];
    constraints = [constraints, S(2:N) == -2*(ED(1:N-1) + DV(1:N-1) - EV(1:N-1) + M(1:N-1))];
    constraints = [constraints, S(end-1) == 0];
    constraints = [constraints, S(end) == 0];
    constraints = [constraints, S(14:28) >= 30];
    constraints = [constraints, S <= 80];
    constraints = [constraints, S >= -80];

    It works well in MATLAB but need to put it into Excel somehow, I am not sure how to set it up. Any help will be much appreciated.

  6. #6
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP and 2007
    Posts
    15,819

    Re: Find optimal values to satisfy constraints

    Some of what you have here looks like it is written in a MATLAB type of notation, and I don't know MATLAB, so I'm not sure I can correctly interpret all of the functions and constraints. We may need to translate post #5 into something that we can all understand before we can translate it into Excel.

    I notice this expression DV(2:N)-DV(1:N-1) appears in one of the constraints, and then in the objective function. I'm guessing this means subtract each corresponding element from the two vectors, subtract them, then sum them up -- or, equivalently, take the sum of the first vector and subtract the sum of the second vector. In Excel, this would be =SUM(B4:B45)-SUM(B3:B44) in a convenient cell. I am not certain if the constraint is intended for this cell to be between -2 and +2, or if each individual difference is supposed to be between -2 and +2.

    The objective function appears to square that result, double it, then add one-half of the sum of the squared differences between column A and column B. The sum of the squared differences can be calculated using an SUMXMY2() function SUMXMY2(A3:A45,B3:B34), then complete the formula =2*previouscell^2+SUMXMY2(A3:A45,B3:B45)/2. My guess is that you want to minimize this cell?

    Some of the definitions and calculations (such as those for d, ED, M, S) appear to be defined and entered in your spreadsheet in post #1, so there may not need to be more done to those.

    I'm not sure what all of the other constraints mean, so I cannot comment on them. In a generic sense, setting up a Solver model involves (1) computing the objective function and other necessary constraint formulas somewhere in the spreadsheet, (2) Calling Solver and telling it what the Object function ("set target cell") is, what it should do with that function (max, min, or find value), by changing the decision variables (B3:B45 in this case would be my guess), and adding any constraints that are necessary.

    Looking at the relatively long list of constraints, I am concerned that this problem may exceed the limits of Excel's built in Solver (up to 100 or 200 decision variables [documentation seems to vary] and up to 100 constraints [may depend on model details] see here: https://www.solver.com/standard-exce...em-size-limits ). It may be necessary to pay attention to these limitations and look for ways to control the size of the Solver model. It may even be necessary to upgrade the limited built in Solver to one of the extended versions or use a third party solver like OpenSolver (opensolver.org ).

    Help us understand your constraints and other formulas and we should be able to help translate those into Excel and then into a suitable Solver model.

  7. #7
    Registered User
    Join Date
    05-03-2021
    Location
    London
    MS-Off Ver
    Office 365
    Posts
    8

    Re: Find optimal values to satisfy constraints

    Hi MrShorty,

    thank you for your help, I have been trying to work in Excel with this and i think I am getting somewhere thanks to you! Hopefully should be able to make it work.

    Auxcron

+ 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. Using Excel Solver Add-in to find optimal hiring, firing, and layoff values
    By ImportedBanana in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 08-14-2022, 03:15 PM
  2. Need a formula to find optimal arrivaltime
    By Enermion in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 11-25-2015, 03:25 PM
  3. want find out the number if it satisfy certain conditions
    By balundl in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 12-08-2014, 06:39 AM
  4. Find some numbers that satisfy a given condition
    By Atoli in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 09-14-2012, 01:58 AM
  5. Trying to find a minimum value to satisfy IF statement
    By hanhj in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 08-19-2011, 07:58 PM
  6. Find an optimal combination
    By Totoleheros in forum Excel Programming / VBA / Macros
    Replies: 17
    Last Post: 10-05-2010, 01:30 AM
  7. Find optimal sum from list of numbers
    By vegasguy in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 10-21-2008, 06:03 PM

Tags for this Thread

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