Video length is 32:00

Optimizing Scheduling and Blending Operations in the Process Industries

Learn how to perform inventory blending optimization to maximize profit and consistently meet product demand to specifications. With Optimization Toolbox™, you can formulate your blending optimization problem to maximize refinery margins by reducing off-spec blend and quality giveaway in addition to satisfying any product demand and feedstock availability limits.

Watch how to create an inventory blending model using Optimization Toolbox and Live Editor in MATLAB®. The focus is on optimized production of gasoline, but the strategy is also applicable to other multiproduct processes having typical criteria of blending processes. You will also learn how to create an interactive professional app using the MATLAB App Designer without having to be a professional software developer. This way, you can share your work with non-MATLAB users.

Published: 7 Feb 2021

Hello, everybody. Look forward to talking to you. As [INAUDIBLE] mention, I've only been at MathWorks for about a year and a half. I do manage a team of very talented consulting engineers that work on MATLAB consulting specifically, mainly in areas of data analytics, machine learning APIs and application development.

So one of my topics today is about a blending problem. And a blending problems is a class of problems where you have to, let's say, you got to create a production schedule for your products. You're blending rock components together to meet specific specifications.

And let's say you have to plan your schedule out. And you want to optimize something in terms of either reducing costs or maximizing profits. So what we'll talk about today will be something which will be something to maximize profits for this thing that you were trying to make. And we're subject to various constraints.

You can only make so much material per month. And you only can store. So much. So if I get into a specific example that we'll talk about. We'll talk about gasoline blending. So we have a problem where we have three grades of gasoline regular, mid grade, premium grades.

They have some few specifications we need to meet in terms of what we use. For this example is octane and vapor pressure. And then, we're going to make those grades by blending together rock components. And we have three components in this example.

And what we want to do is we want to calculate the monthly production rate and what levels of inventory storage we're going to have for each grade of gasoline over 24 months, that achieve the maximum profit that we can make in that thing. And we're going to be subject to both capacity constraints and inventory constraints on what we can make.

And in this case, we actually know what the costs are for the raw materials. So let's say we're in this thing where you actually we have fixed cost. And you actually know the market demands. So both the market demand and the costs are fixed, and the example.

So just to give you a refresher what's the class of problems this are from a mathematical standpoint? So these things where you're talking about, you have basically, a system of linear equations, which are basically, doing the blending for all the components. This falls into a linear programming.

And MATLAB has a toolbox. The optimization toolbox, which provides workflows for actually solving linear programming problems. Now, don't get worried about having all the math here, because you just be really quick thing on this thing.

We just give you a background on a linear programming problem. You have an objective function that you're trying to either minimize or maximize. In our case, we're going to write an equation that will define the profit that we're going to make for this project. We want to maximize that.

It's subject to constraints. In our problem, we have constraints that are limiting in terms of the product quality. So what the octane rating, has some ratings that's to be vapor pressure. And we also have bounds in terms of the fact that we only have capacity and inventory limits on what we can do on things in there.

So this is where we going to take this workflow that's in the optimization toolbox and apply this to actually solve this problem. The optimization toolbox has two workflows that you can use to solve these type of problems. One is a software based approach.

So in that way what you do is you spent a lot of time up framing your problem in terms of matrices and vectors such that it can then the inputted into the solver that you're going to particularly use. It's included in the optimization toolbox. So that's the problem solver based framework for the work problem workflow.

The other one is a problem based workflow. In a problem based workflow, we're actually going to symbolically, define optimization variables, and the expressions in terms for the constraints, and the objective. So then we can solve this. So we have a Simulink representation of that.

And then we'll take that symbolic representation and then it will solve the problem from there. This makes it much easier to understand when you're writing the problem. It easier up front and easier debugging. In terms of that, the trade-off are, it does require more code, more lines of code. To do that it's not as efficient is using the solver based approach.

But for our problem here, which is not very computationally intensive, problem based workflow is really the way to go. So what I'm going to do is I'll go into MATLAB. And actually show you how we actually go about solving this problem in terms of a live script. So you guys can get an idea of how you actually work on developing a problem inside of MATLAB.

So all right. We'll take a quick look. I'm going to switch over to MATLAB. So if you take a look, I'm actually in here. This is MATLAB. For somebody who's not familiar with MATLAB, it's basically, an integrated development environment where you can solve a very computationally complex problems in a an easy way.

We have a line item, a command window where you can enter in commands. All I'm showing you here is a live script were actually, can put in commands into MATLAB, write them out as a script, so then you can work on them. Do your debugging. Save [INAUDIBLE]

And then a live script you can also add in comments annotations, graphics, demographic representation of what kind of the problem we're doing. This is really good for useful for sharing. Other people who have MATLAB licenses or you could print this out for documentation.

And in this case, what we have to do is work out what the problem workflow is for this problem. So for this case, the first thing we need to do is we need to get the data that we have in terms of our market demand, product price, raw material price, into the workspace. So that we can then solve the optimization problem.

So for this particular example, we're getting our input data from an Excel spreadsheet. Let's give you a quick look at what that looks like. So this Excel spreadsheet consists of a bunch of tables, which defines, like this particular table defines the product composition requirements, in terms of the octave and vapor pressure rating, the raw material, composition, and product price.

So we have our three grades of gasoline. And the monthly demand in there. So it's a series of separate worksheets in here, that I'll have tables in them. And we need to import that into the workspace. Onto their. So excuse me. So what you can see here, is what I'm doing is actually this part that's in grey is actually MATLAB code.

This will execute and then import. And I can run that section. It will actually import in the data on there. So now we have the data in there. I'm going to run a few checks. It's always good to when you're importing data to validate it especially, from Excel spreadsheets.

So let's say you're going in you're modifying the problem you're editing it, maybe you goofed up one of the tables. This is a minimum check of just on the dimensions. So this is a dimensional check. And it's running. That didn't get any errors here. So that's good. Everything I put in is OK.

And now what we have is we have in the work space, we have these tables which define all the data we want to do that. So the next step after we have the data input it into the workspace is actually start to frame the optimization problem.

So in the optimization problem, you can see that we're going to frame this in terms of decision variables. This is all provided in terms of the optimization toolbox. So we'll first, have a few variables that define our various components, the number of raw materials we have, the component qualities we're looking for, the blends of gasoline.

And then we're going to make optimization variables. So that includes-- Essentially, if you're thinking about the problem, what we're doing is we're on a monthly basis for each plan we're making so much blend. We're going to sell so much blend. And we're going to use the raw components.

So that's where these are defined here in terms of these variables. In terms of here. And I'm using this function that's in the optimization toolbox to define these variables in there. So that we don't have to go and write out all the matrices that they represent.

We also need to have an inventory because we need to represent the inventory in there. And that's going to be subject to some constraints. And you can put constraints in on the variables. So if you take a look at one of these things. Essentially, you have--

The inventory is constructed by per month for each blend. You can't you can have inventory less than zero and then upper bound is we have upper storage limit on there. So that's all defined in terms of the variable that we have in here. So I'll run that one just exit that.

And then the next thing we want to do is actually, define the problem in terms of the optimization thing and work on the objective function, which is the profit, we're going to have on this thing. So we take a look at this, we have an inventory problem.

We're going to use this to actually create an object, which is going to store all the information we need to solve the problem in there. So this is created by a function in the optimization toolbox. And we're going to have our objective sense. We want to maximize that. So we have a maximization problem that we define it in that way.

We then need to define our objective function, which is going to be the profit. So to figure out a profit first, we need to figure out all right, how much revenue we're going to make? We need to know how much material we're going to use? The cost and the material? Or costs for storage.

And then our objective function, which is part of this object, which has one of the properties of that is the objective function is going to be essentially, equal to the revenue minus the ingredients, minus the storage cost. This is all built out of those variables we made before in terms of the, lights go back up here. In terms of the make selling use. These variables we defined up here.

So now we have an objective function defined. And I'm going though this work real quickly, just to give you a flavor of what this is like in there. But definitely, there's an example in the next product about fertilizer blending, which is very similar. One you can actually walk through to get a better understanding on there.

And then we have a few constraints. We start working on our constraints, now that we've defined our objective function. So that includes-- Here we go. We can show the expression for the objective function.

You can see we have, it's a big expression, it's a big matrix, including the amount of material used, or made, or sold for each month and for each bled.

So once you define the objective function, then you need to work on your constraints. So we have a couple of constraints we have to meet. We have an inventory. We got to have things that the input inventory. Inventory has to-- at the next month, has to equal the prior inventory plus production minus the sales. So we've got to make sure we do that.

And we have material balance, constraints that we have to say. Everything that we make and sell in the end, we're not creating any material so those have to balance out. So we have a mass balance constraint. We also have another, it's a long one.

We also have other constraints in terms of-- We have a final inventory, you have to specify. You have inventory limits. There's only so much we can store each month. There's process limits. There's only so much material we can make each-- So many blends blending we can make per month.

And then we need to keep track of how much material we use. And the amount of material we use that's to equal the amount we made. And then we have component quality blends, which is all a different one because that's in terms of a range. So we have a minimum quality metric and a maximum quality metric that sets those things.

So we know in the end, we're going to be our octane and vapor pressure specifications, which have ranges in them. So these are inequalities in there, because they have constraint in them. And we load all of those constraints into the problem. Let's quickly run this. End the section.

And then we can actually, inside of the problem, and we can actually write out this problem. And so you can actually go to review all of the equations on there. It's quite long. It has a function that you can actually write it out to a text file and take a look at it outside of MATLAB. If that's what you like.

And then we can go and solve the problem. So I've now defined the-- I set up the optimization object. It has the objective function. It has the constraints and the variables to use. So I will then run this. And if we're lucky, it'll say it'll solve. And it quickly solves the problem.

So it found an optimal solution, to give you some information detailing how that solution worked. And then we can display that output. So that you can take a look at the actual solution. So here's the actual solution in there. And here it's displayed in the tabular format.

The amount of material we made. The material we sold. The material we store. And then we have a graph showing, this is what the output of the optimization problem showed. How much material we need to make each month. The amount of material we're selling each month. And the amount stored to actually maximize our profit based on inputted market demand in the raw material prices.

So that gives you a quick flavor on how you would go through and solve an optimization problem for blending inside of MATLAB. And I demonstrated using a live script. So that's the basic framework on how you would actually go through and set up an example in there.

So the next phase on this thing might be what are you going to do next after you have a problem you've solved it and now you need to share this out amongst your team members that may or may not have MATLAB? And how you going to do that?

I'll go back to the presentation. So I'm going to talk about the next phase is like application sharing. And you might say, well, wait, I have my live script. Why would I want to have an application or a graphical user base? There's a couple of ways why, for your workflow that you may want a graphical UI. What is, is it might help you out in your own workflow to make yourself more efficient, rather than having to.

If you have stuff that's repeated, constantly repeating doing the same thing, but you're employing different data, you may want to have a user interface. So that it's easier to do that. You don't have to copy and paste errors going from scripts. Another thing is if you want to distribute your, as we'll talk about later, distribute your applications to people that maybe do not have MATLAB licenses but do need the functionality in your application.

And we'll talk about different ways you can actually distribute the applications on there. But first, what I want to do is a brief introduction on how you build a graphical UI. There's an integrated development environment inside of MATLAB called Designer. It's part base MATLAB. And that allows you to build a graphical user interfaces.

This is a more advanced version. Some of you may have been familiar with MATLAB, way in the past. There was a previous version called guide. And this is something which is replaced guide, is the way to build button you guys on there.

And when you talk about UI development, people will talk about components. The components are just little gauges axes display. What you can do is you can use Designer to put in components into there. You have a pallet of things you can pick. And then it'll do--

And what's great about app designers, you can plop all these things in there, and then have Designer in the background, will put in all the code you need to make that thing work. And so you don't have to go and craft programming and all that in that programmatically.

And it's all object based modern code that really can help to make it an efficient and easy to maintain application. The other key thing about designing your applications and art designer is then you can actually run it in a web browser. So this will allow you to run apps in MATLAB Online.

And allow you to run package apps using MATLAB compiler. And then host them using MATLAB Web Server. So then you can have internally hosted MATLAB Web Server. And then all of your users using your application, can then just access it from the website without having to download any software or doing any installation.

And also briefly, mentioned this point that in 2020 a, when we started to ship a guide to app Designer transition tool. So if you have old guide apps and you were saying like well, I need to keep this updated. And now's a good time to consider looking at that guide, the Designer transition tool and update your application. So that.

So I'm going to go and pop into the application that I built and did solve this problem. All right. So I'm in MATLAB. So art designer is actually an application that sets the actually launch. That you can go in apps and you can select in your apps an app Designer, in here. Application, and I'm missing that. But I'm actually, already have it up.

It just to the left, you can see a design app to first thing in-- more left even. Or all the way left.

[INTERPOSING VOICES]

Menu. More left, more to the left.

Oh there it is.

Yeah. There it is. Yeah. It's a bit hard. Got it.

Yeah. I actually never, have to say that's a liberal thing, because I actually never call it in there. I type in the app Designer into the-- I'm a command line kind of person, so I try to type everything command line. So anyway, this is the app Designer.

You can see right here I have-- What I built was a graphical interface to solve the problem, front end to solve that problem that we solved in the optimization tool box. And I think to give you a flavor on how this

works, I'll first give you a simple example by building a new application from scratch. Just showing you how the actual workflow is.

There's a great tutorials here. So you can actually see there's a couple of tutorials in here. It's actually a great way that actually walks through and teaches you how to build an app. But what I'll do first, is just start with a blank app. And the first part of our workflow, as I described, is why I need to I need to load in data in a spreadsheet.

So I'll just try and give a quick example of how to do that in terms of how you would do that for an application such as this. So we talk about-- Actually I picked on the wrong button. There we go. Canceled. So let me once again, build this new application.

So what I want to do is actually, have a button that will actually lower the spreadsheet. So based on a button. And then let's say I want to display it in a table. So paste in a table. So what's great is that app Designer, you put these in there, it actually has a component of browser which then defines these objects in here.

So that keeps track of all the properties, and callbacks, and things like that. So that is first button, let's change the name to load. So we know what it's doing. And what we want to do is when we press the button, we want it to actually then open up a file dialog box. And then we can click on our spreadsheet and loaded into the workspace.

So whenever you have an action that you want to actually do in there, that's called the callback. And so look at the callback, here's button foot, button pushed function for this mode button. And so we'll go here, and we'll add the callback. And what's good is then, it really pops you into code view.

And says, Oh, here's the function. This is what's going to be executed when you press that button. And then what we want to do for this button to be pushed, is we'd actually like to get a file dialogue box. And then displayed in that table.

So we take a look like, say, I need the function on how to call up a dialog box. So what I do, is I manually go in to help. And say, I need to file dialogue. This is a little bit hard because I've definitely done this before. And you can say that, we have--

And you notice that what's good about on hits on app Designer is that, you really want to use the stuff that starts with the UI. So if you see UI things, that's the things that you actually want to build in your Designer app to make sure it's compatible.

And you can see there's a couple of different file. So here's a UI open, open file dialogue box, load file, the workspace. So we don't learn the workspace, we want to actually get the file name for this particular example. So I will use UI get file, which will actually, just get the name of the file.

I actually want to read it. I'm reading it from an Excel spreadsheet not in MATLAB. Not a variable math file saved inside of MATLAB. So I will copy this into this function. This code will then update. And it will grab a file name in the path of that file. And then what I want to do is actually load in a spreadsheet thing.

And I'll go back to my live script that I wrote. Scroll back way up to the top. And then say-- So let's just copy and make it simple, this product composition table. So this has the table function which actually reads in the spreadsheet. Now this paste that back into my app Designer.

I need to make sure that now this variables no longer means anything in this context. So I'll just copy and paste this in there. And you get all sorts of helpful hints in app Designer, about what you're doing to the file. So this will actually now--

So open the file and then read it and put it into this variable product composition. The next thing is, I want it to go into another component in the UI. This UI table. Let me take it. I go back to Design view. I have this table, UI table in here. And I want to load that data into there.

That's defined in the component browser. You can see it's at .UI table. So I go back in here. Put an app which is the object which has all the things related to the window that we're working in. And then I can see it. It pops up. It gives me help. It says, all right. Here, I have a table.

All right. I've got my table in there. And then I want to load the data to the table. So I know there's properties of the table, which I can then set. I'm using that notation. And I can put data in. And that's actually when I want to re-define this product composition to.

Will just say, all right. And then we'll put that in there. And if you did this correctly, I'll save. This should actually then, load the data into the table. So let me run this. That I get our app, that's actually running now in mat-lab. Minimizes windows, so we can see what's going on.

I'll click the button. I get my file things. It's actually by default. And only looks at MATLAB file. So I'll have to go and inject everything. You can actually set that is the property on there. And the father was loading it too is this gasoline file. So open that up. And then we can see our data in there.

So that just to give you a flavor on how you actually start building components and then working with them inside of an application. So in our bigger application here that we have for this blending problem, we actually, have a thing that has a very complicated UI thing.

All this is kept track in this component things. Essentially, I have two functions of this UI that I design. One is to load the data in. It that validates it. It fills this in. And we can inspect what we're doing. And then we can run the data and solve it. So just give a quick info and how that works.

So if you take a look at this thing. I can load. And I'm alone in a different file because I changed the formatting property slightly for the spreadsheet to make it more compatible with the snap loading thing. So anyway, so I love that file in. It had all the data. It liked the data format. It did some basic checks on it. It's great.

I had a light turned green. I could go through and inspect the various properties on there. And then I can run the simulation and solve the problem. That'll pop up. It tell us our profit. It puts out our graph, just like before. I can actually click the save button and then save it to results.

So that's the overall functionality of the app. And to give you a little flavor on how that actually is all built on there, is that these things are all, as I said, they're all objects that were selected from here, arranged in various panels in UI configurations. And they're all there.

And then there's callbacks on there to then have a function. So if I'm on a load thing, right, there's a little button function I can call back that I find. I can go click this and move to that function in app Designer. And what you notice, not that I mention before, but a lot of the code straight out.

That's all the code, object orient code that the app Designer keeps track of that. You don't have to worry about. So then you can have the things, that this is what's happening in terms of the things that what a code is executed, when I press that button. So that's the way it's defined.

And one thing is OK, so what does this actually do? So when I'm actually, click in that button, now I have to execute all that code that I had before and a live script. And there's some work done on the back end to actually make that into an actual class object in MATLAB for this particular example.

And you don't have to use a class object. You could use a just functional form. But using class has some advantages in terms of application development because you can actually then define an object. And store all of the data related to that problem and all the methods, in one object.

Its easy to share between the components. And just to explain like how that works. So I just do a quick look at that class function. So this is essentially, the last functioning, which contains all the code that was in the script written in terms of that.

And it has the ability to create a default object, that would store all of the information related to the objective problems in the data variables and everything. And then there are several methods used in their. So I just want to import the problem, validate the data, and run the data. That's all the code, it's really executed inside of the application.

So in terms of app Designer, one really things were actually having a good app Designer capability to separate out the front end code from the back end code. So we take a look at. So I want to click back into the one thing that we go back into app Designer.

You can see that I'm calling these functions. These are inside that class. These are methods in the class to do that. So import the data. And validate the data. That's what happens. So that's all the code is related to the actual problem inside of app Designer. It's all done outside of the app designer.

And you might say, well, how do I share the data marked in this thing? I've made this object called PP. It allows you to go between all the various components in there to actually, share the data. And that gets defined at the start-up function.

So you have Q&A function at the start up. Try to start up by call of the constructor, to construct that object. This app .PP object that can use, to then do all the backward, back and work of the problem. So that's just to give you a really quick overview on how--

And that actually allows you, that you have not that much code inside the actual application. So that's just to give you a quick flavor on application development inside of mat-lab and app Designer. So now that you've got your app, you actually want to go through and share this out with people who let's say may not have a license.

So what do you do for the next step on here? That we take a look at, go back to my slides. So what you could do is, you actually, can share your laps using I'll show your work using MATLAB Web Server. And that, you can essentially, gab your app run as an interactive web app. There's no licensing for the actual user using that. Right of no installation.

So you purchased that web app server. You post your applications up to that. And then users inside your-- that have access to that server, can then use that application inside of a web browser. So I'll show you how you actually go to that workflow.

So first, you develop your application, which we already discussed about, an app Designer. You can then package your application with MATLAB Compiler. And you have a couple of options for there to. The most common ones are-- If you want to make an actual standalone application, you can do app use Application Compiler data standalone executable.

But then a user without MATLAB can then execute to run the functionality. You can also then do a web app compiler. That's another app in MATLAB. And then get a CTF file, which actually is taken by web app server. And then actually, runs it an inside of a browser.

So you go through. And then you can either stand alone there. And have a web application on there. So let me take a quick look. I can actually show you a demo for a-- or our particular app. So here I have the actual app. It's designing. And this is inside of a web browser here.

It's actually running in Chrome. And it's hosted by something inside a matrix here. So I have internal access to that. And that's something. This has all the functionality of the original problem. So I can actually load in, selected file.

And in this case, I can load in. I have another example that works on this thing in terms of making fertilizer. So I'll load that one and you can see a little different plotting in there. I'll load that in. It runs right in there. I can run the problem. And then we have the solution that came out with what the profit was and what the production plan is. And can also save those out. Then download those results.

So I have the functionality I had with the desktop app and running inside of this web browser. So it's really easy to share that around within your organization. The users that need to do that. Things are. So that's an overall summary of how you'd build the web app on there.

I'll just take this time right, at the end of the talk is to say that I have a team of consultants that have to build professional apps. We do a variety of industries and applications. So we look forward if you have any--

We can do very complicated applications in there. And if you need help to build your complicated apps in a professional way, my teams are ready and available. And also if you have questions involving analytics and data analytics, are things I can also work on that.

Thank you for the time. And here's some additional information you guys can look at. Thank you.