 Welcome all, here I have Daniel and Surya with me on how to improve your diet and save money with Patan. Give them some applause. Hi everyone. My name is Surya, here's Daniel. We are both students from the University of Alicante and we are both chemical engineering students. So we're not programmers. So if you see something in a code, feel free to send a pull request to JITAB. That's another thing. You can find word documentation and notebooks in JITAB, which I will post in Twitter later. Something about us. We are both active members of Kachema, which is a non-profit organisation by a university. And, well, something maybe about your personalities. He is in love with interactive widgets and he loves them. I actually don't like them so much. And, well, we are programmer science 2014, so we are beginners in this. And something about the talk. We are chemical engineers and we love reactors and distillation columns. But I don't think you are much chemical engineers, so maybe this will not be interesting for you. So we were thinking what is everything everyone of us eats. And, well, everyone of us wants to eat the most but pay the minimum of money. So, but all of us have the objective now it's summer, so we want to fit and be healthy. So we are going to show you how you can optimise your food with the minimum cost, but having the nutritional concerns that you want to have. And, well, now I'm going to let you, Daniel, who is going to tell you something more about the theory of this optimisation problem. Thank you. There are no interactive widgets here, so don't be afraid. Optimisation deals with finding a minimum or a maximum or zero of a given function. And we are going to see some concepts using the diet problem. This problem was announced in the early 40s when the US Army wanted to minimise the cost of food and give their shoulders a balanced diet. To understand this is better, I give you an example. Imagine we move the diet problem to the present time. At this date, it is common to go to a fast food establishment to eat and we want a cheaper meat and, of course, we want a healthy meal. So this is our problem. When you go to this establishment, you can choose some products like fries, like hamburgers, and these are all variable, naming as eggs in our mathematical problem. Each product has a price and the total price is the multiplication of all coefficients by this variable and the deletion of all of them is our final cost. What we want to do is minimise this cost subject to our constraints and nutritional constraints. So this is our mathematical problem and we are going to optimise this using Python. But first, we have to see some concepts of optimisation. I was my professor from the very beginning and I'm going to show you in 10 minutes. So first, what is a linear function and a non-linear function? You can see here, this is a linear function, it's aligned, yes. And, well, this is a non-linear function. You see, it's not a lane. Okay, thank you. You can perform this in three dimensions. This is a plane and a non-linear function looks like this in three dimensions. Well, a smooth version known as smooth. If you have something like this, this is a function where you can calculate the tangent in all the points of this domain. So this means that this is a smooth function, right? And, for example, in the vertex of the solar function, you cannot calculate the tangent. So this is a non-smooth function. This is important and later we will see it. And also in 3D, in three dimensions, this is a smooth function. And as you can see, also a vertex, this is a non-smooth function. Many of you may use the gradient of the function to solve the problem. In more cases, this gradient is not given. So you have to calculate it numerically. If you have a non-smooth function, this is so complicated and you will not be sure that you don't have a mistake. So, to optimize, we prefer the green graph, the exit function, so non-smooth function. Well, if you have something like this, it's a convex function. When the tangent in a point is completely horizontal, you will have a minimum. In this case, you only have one point. It's the lowest point, so it's a global minimum. And, of course, if you have more than one point that has the horizontal tangent, you have more than one minimum. In this case, you have the lowest value for the function. It's the global minimum, and you have other minimum that are the local minimum. This is so important. And why is this important? Well, when you are solving this type of problem, you don't actually know the value of all the points in your function. So it's like you are in a fog-covered mountain, and you only have the information of the nearest region. So it could be possible that if the fog is going out, it could be that you are not in the global point, in the global optimal. Well, what is a convex and non-convex function? This is a convex function. Imagine if you choose any two points in the graph, and you trace a similar line between these two points. If the graph is always down the segment, you have a convex function. If not, you have a non-convex function. Yes, easy. And also in three dimensions, convex and non-convex functions can be down. So this is an important subject. What is a constraint? Well, constraint is a function that limits your search space to find a minimum. Imagine we have this function, this is the control of the function, and the lowest point is the black point, the global minimum. If we have no any constraint, our optimum point is the black point, the global minimum. Okay? So we can have different types of constraints. Like this, it's a quality constraint. That means that the optimum point must be inside our restrictions. So the solution for this problem is in the red point. This is the optimum. And as you can see, it's the clear point to the global minimum inside these restrictions. You can have inequality constraints that allows you to search your optimum point over the restriction or under the restriction. In this case, as you can say, you can search your optimum point. Over the inequality constraint. And of course, the optimum point is the global minimum of the function. So it's fantastic. And you can have the other case. You are only allowed to search the optimum point under the constraint. So your solution is this red point. And you can ask me, well, it's a little far of the global minimum yet. But this is your problem. And this is the better solution for this case. Because you are a constraint. And this is so important. Of course, you have more than one constraint. You can perform a little search space to find the minimum. And as you can see, the red point is another point, right? Of course, this region that you find with the constraint should be a convex region to be sure you are in a global optimum point. Because if you have some like this, you never could be sure you are in a global optimum. You only can assure the solution is a local minimum or a local maximum, but not the best solution. Well, most of the figure and script from WebAssembly and Cypillecture. Thank you very much. Well, attending to this, the termination problem can be divided by using these ideas. If you pay attention of your objective function or your retention, you can ask, well, it's a linear. It's a nonlinear. Or we have a decision variable. That means that you can do a choice. For example, I only want a vegetarian minimum or not. It's like one or zero. There are half my problem binary variable, yes or no? So we can have linear programming program, mixed-integral linear programming problems, nonlinear programming problem, and the most difficult mixed-integral nonlinear programming problem. And each one of these type of problems have a main algorithm to solve it. For example, we'll use the simplex algorithm to solve linear programming. Well, an optimization model can be defined mathematically as follows. This is our problem, the data problem. And as you can see, there are only letters, but don't be afraid in PiOMO. This is not that abstract model because we put the coefficient and other parameters externally. And this is a concrete model in PiOMO where you put specificity, the value of the parameters of other coefficient in the problem. So now I'm going to change. Okay. Well, the optimization problem could be solved through a solver. A solver is the implementation of the main algorithm so we before. And some of these solvers could be that you saw here. For linear programming, we can use GLPK, PULP, COWICS, OLD. For example, for mixed-integral nonlinear programming, we use VARON and SIPLEX. SIPLEX is fantastic. Really, really. Well, we can use this solver through an algorithm through the algebraic modeling language that sees high-level computer languages that support formulation and analysis of large mathematical problems. Some of them are AMPEL, GAMS, Ames, and of course PiOMO. PiOMO is interesting because it allows the formulation of algebraic models using Python. And this is great. And it communicates with principal solvers even Neo server. And why PiOMO? Well, why not PiOMO? Yeah. PiOMO is an open source, has a solver integration, and has several advantages over quantum, it's a robot language, has extensive documentation, a rich set of standard libraries, and more. So why not PiOMO? It's a good option. So for studying PiOMO, we have to thank Juan Luis Cano for his help building the Cona Resafe for PiOMO. Juan Luis is also in the conference. Thank you, Juan Luis. For installing, you only have to type this line code, and you have to install the solvers separately because they don't have implemented installation, but it's not a problem. If you want to install GLPKI to solve a problem and mix an internal programming, type this. And to solve non-linear programming, you can use E-pop, type in this other line. And you can install some extra, like Nail Server, that is the free internet-based service for solving numerical optimization problem. And it's so interesting for more information, click the link. And now I give the award, I don't know what to say, to 30 that is going to explain the problem we perform. We are sorry for that much of theory, but if we don't show you that, you will not understand what I'm going to show you now. So I'm now going to show you the practical part of this. So now we're going to improve our diet. And, well, our problem is that, like Danny explained before, but in, we have two cases. The first case is we set our menu from a fast food restaurant, you want. And we want to minimize the cost, but eat the healthiest way. So we have a lot of constraints. We have to minimize our function option. And, well, all that have to be the optimum solution for us. So how I did this, I actually am not good with guideline daily amounts and this stuff, but I was searching on the internet. And, well, I find a lot of data about the input of daily nutrients you should eat a day. What you can see here for different woman, man, children, that we'll just shoot you today. It's a little bit, I don't know if someone does that. But, well, using this, I have done the problem. I've solved it. First, we need to import pyoma. You say we are importing everything, but it's like, it's convention and pyoma. So he does this because it's like this. And then I got the data. I have to say the data, it's not totally real. So everything could be real. It's fiction. And you will say here is my data. And we have, I don't know if they will let me show you. But in this data, we have all menus, the costs, and then on that, I will show you later if you want, we set all parameters. We will be all constrained values. It means in this case, we have calories, sugar, proteins and some of this stuff. And I've settled maximum and minimum values I want in my diet. So solving this problem, first we need to input the model. This type of problem is a linear problem and an abstract model. So we set the sets. The sets is the data I used to get the model in. And in this case, we have the products and the nutrients with the parameters, which are data which depends on the sets I have set before. And we have cost, nutrients, and a maximum and minimum of each nutrients. So which, of course, we will default zero and infinite. Then we have all variables. We'll be, in this case, a number of servings, what we're going to take. Our objective function is to minimize the cost of the food. And it's the normal structure of our object function. All constrained with is the, I think, the most difficult part of the code because you have to look what you're going to do and it's, they don't want to show you a more difficult problem like that. And well, this is very easy. We have maximum and minimum, nothing more. And solving the problem, that's what Paimo gives you as a solution. You have one solution, which is great. We have a function value. It means we have minimized the cost of 369 euros. And, well, he finished everything done. And the solution he does for us is this. But it's not showing my solution. I don't know why. On there it, what you don't see, he gives you the answer. It makes, in this case, we get, you can eat one Big Mac and you have optimistic your diet. You see, that's everything you can eat in one meal, of course, because you have, but only the hamburger, you can't eat the fries. Only hamburgers. You have to say, here, you see, that's a normal graph of what we've gone with Big Mac. You see, this black one is the daily amount in one meal you should eat, but this type of food has a lot of salt, a lot of fat. So, well, if you want to eat healthy, you go there and eat a Big Mac. It won't be great. Now, I'm going to show you something very different. I like this problem a lot, because I was thinking what can do more with the diet problem. And I was thinking, well, now we're going to do the opposite. It means how much calories I can eat in one meal for only five euros. And it was like, wow, I go there and eat a lot. So, no. Well, how do I do that? Yeah, it's worth it. Let's eat. And, well, we dig the same. We do the import of firemen. We get the data, which you will not find that there is a dead link. I don't know what's happened. Well, and to solve that, it's an abstract model. We set our sets or parameters. But in this time, you see it's not the nutrients that we're going to set to maximum or minimum. This time is the cost. Zero and five euros in this case. Our variables are the same. And our objective function now is the calories. But we want the maximum. You see, payam is very visual and very easy. You see there the sense, when you want to minimise, you put there minimise, and when you maximise, you put maximise. You don't have to do anything more. And, well, our constraint now will be on the cost and our solution we got. You see, you can eat a warm meal, 2,690 kilo calories for five euros. So you eat that what you should eat in one day. Which I think is respectable for five euros. And the solution is this. You should eat three hamburgers for one euro, 29, and one package of grilled onion rings. Which I've never seen. Now, I'm sorry for it. We are chemical engineers and we love reactors. And distillation coming. So now you're going to say the problem we solve normally. And, well, that's a problem or example. In this case, we solve chemical engineering, chemical industry problems. So we have to maximise our ways to get a product and to minimise our environmental impact because we deal with a lot of chemical products and something like that. So to make it easy for you, we have products which we can do, pass with a reactor to a final product, which will be beer in this case. And then we can separate water from the alcohol with distillation and we'll get whiskey and water. But we have conduit with two types, reactors. We have butt and continuaries with main to butt reactor. It's what you're doing home. You put your water in and stop the reaction, eat it, and then put another boiling water on it. Continues, it will be there how much time you wanted. And distillation is the same. You have a butt reactor which is, I think it's on a Simpsons capitol. Yeah, they explode the thing. But you can do it in short times or in long time for continuaries. And well, that's not why I'm going to show you. We have this superstructure. We have A want to get it to B and then to C. And we have two ways to got it. We have a reactor. We have a lot of distillation columns. And well, we get to say us what we can do with the minimum environmental impact. And well, we have a lot of reactions. Everyone is interested and you can read them all of it. I didn't think he would want that. Here, we have interesting fact. We have disjunctions. This is the first time you will see them. Disjunctions, you have to imagine it as like two situations. We have situation one, which is when we have situation one, the cost and every product will be this data. But if this situation is not going to happen, we have the other. It means everything is zero. So this part is not existing anymore. And this is the hardest part to deal with it on Payamor, which if everyone is interested, you can come to me outside and I will explain you how to do it. So now we are going to introduce binary variables, which is everyone, I think, knows what the binary variable is. The one of zero, well, it exists or it doesn't exist. So optimizing or finding objection, you see this time it's not a lovely linear problem. This time we have a real expression with everything math stuff. And now I'm going to show you the results because the code is very, very long. These are the results, what we should do. So you say, Payamor, you can use it for everything, to go to McDonald's or to make your own reactor at home and settle, I don't know, whiskey, beer, what you want. So you can use everything of this for your own choice. And last but not least, I have to say it. I mean, I said before, because we are both part of Catemi, which is a non-profit organization from the University of Alicante, which encouraged chemical engineers to use programming because we actually don't learn to program in our studies. And well, we have to thank, if you want to see the home page. And we have to thank a lot to our teachers, Fran Navarro and Rue Femenia, which have helped us to adjust this. And I hope you enjoyed the speech. I wanted to know if you know what are the methods for optimization that are used inside Piomo or any of the tools that you are calling from it? Like, is it iterative? Is it iterative way? Is it Newtonian? Is it conjugate gradient? What kind of method of optimization is done to arrive to the minimum? It's, normally it's an iterative way, so he tries everything and he wants to choose how the solution will fit everything. But normally, you have seen a very simple problem, but before I showed you, you have, I think, 29 equations. You have to optimize it because the solution needs to fit every of them. So sometimes it will be a good solution what you get, but sometimes not. So you have to play a little bit with that. We normally use GAMs and you will see the code is on there, too, that they give very similar, but not the same solution for you to the same problem because every solver had his own stuff. I don't know if that's the answer to your question. Yeah. No, it's okay. I can shout. We won't be recording. Thank you very much for the talk and the optimization of calories for five euros. Did you have any constraints as in non-toxic level of salt or fat? So can you survive those calories or did you have no constraints? I was looking at that and I saw on the internet that you should eat two grams of salt a day, which is, I think, a lot more. So I was looking at it and I think if you eat that much of calories and of salt a day, maybe you go to the hospital on the next. So I don't think you can eat that much for one meal. It will be too much for you to get that. We have burgers and one... No, there are the 2,600 calories, I mean. Yeah, the problem is, no, the food had a lot of salt and sugar and that's too much you should have in your body. So you will have problems later, maybe. I don't know if maybe not. You should try it. You should try it. For science. Please send me a message if you have tried, if you are okay. Hi, thank you for the talk. I have a question. Why can't you drink this water? It's whiskey water, basically. Drink and drive, you know. Why? There is water waste. Don't drink that. It's water. It's whiskey water, right? I will buy it. Okay. That's... Well, that's... That's because you have to study a little bit about distillation. You have a lot of types of different alcohol types in whiskey and you have to see there are some types you can't get behind from it. So that's what you are extracting and that's in the water. So maybe if you... Yeah, it's water. There may be some molecules of water in it. Normally, everything what we got in the chemical engineering prediction, you don't shoot, eat it or taste it. Well, I didn't recommend to drink that. So does that mean if you mix wastewater with whiskey, then you get beer out of it? No, no. It will be great, it will be great. Okay, my actual question... My actual question is it possible to have the two objectives? I think you implemented now the nutrition constraints as constraints. But can there be two objectives which are somehow weighted that you can say... Well, that you can compromise between nutritional value and money spent. So it's fine if the nutritional value is not quite as high if you save two euro or something like that. Well, yes, you can perform a multi-objective exercise but not using this technique. For example, if you are interested in optimizing the cost and the medical care, there are two objectives that are different. So you have to use Pareto on other techniques. No using these techniques. But yes, you could do it but using other things. This is only for one objective function. Yeah, you can do it. If you want later, we can talk about it. What you can do will be easier if you want to do it like this. You can transform one of the function objectives in a constraint. So if they're satisfied, you have two function objectives. You only have to do what are you dealing with. It's to know what you're doing. Normally, we don't know that. The speech should be 45 minutes but I was a little bit fast, so I'm sorry for that. Any more questions? No, so we can have our free meal. Thanks for coming. Thanks for our speakers.