 Welcome back. In the last segment, we discussed brute force search for solving problems for which we do not know anything cleverer. In this segment, we are going to see how to write programs to model the working of a system. We are going to start off with a simple and a problem that you might perhaps consider to be a fun problem. The learning of the bargaining that goes on when you go to a market. So pretend that you are a seller who wants to sell a certain object. Now you will be happy to accept to sell the object for say 500 rupees. So you will accept 500 as a price and of course if somebody says I am offering 600 you will certainly accept it but you do not, you are not going to accept a smaller price than 500. That is sort of your minimum, that is the minimum price that you want and you could say 500 and you could put a tag saying it is 500 but sometimes in some places the customers want to bargain. So if you just say 500 then the buyers will tell you oh please reduce, please reduce and they will not accept it and if you had said 600 maybe you could reduce and you could come down to 500 or something like that. That might be acceptable. It is just a psychological game I am sure which you must have seen happening in markets. So how do you deal with this as a seller? Well, so here is the strategy that we are going to employ in this program. So you are not going to tell your price at all. So you are going to say to the buyer look why do not you tell me how much you are willing to pay. So if the buyer offers a price bigger than 500 then you are done, you can just take it. You are happy, the buyer is happy and the whole thing ends over here. But if the buyer offers a price smaller than 500, some price P then what should you say? Well you are going to make, let us say that your strategy is to make a counter offer Q such that the average of P and Q is 500. So once you do that the buyer can accept it or maybe make another offer and this can go on. So here are some examples. So suppose the buyer says look I am willing to pay 400 rupees for it. What are you going to do? Well you will say 600, why is that? Because the average of 600 and 400 is 500. Why this strange thing? Why do not you say 500 right away? Again you want to keep yourself some leeway in case you need to reduce further. So this is how you are going to work. After this maybe the buyer says 450. So in which case you will say 550, why? Because again you are sticking to your rule, the average of 550 and 450 is 500. So you must have seen something like this happen. It is not always exactly like this but let us just say for fun that in fact well there could be a seller who uses this strategy. So what we are going to do is we are going to show, we are going to write a program to show what is going to happen if there is a seller like this and a buyer comes to that seller and of course this cannot go on ad infinitum. So maybe if this happens 5 times then the whole thing has to stop anyway. So maybe it means that you did not come to an agreement and then the buyer just walks away. So this is what you want your program to enact or simulate. So let us design that program. So clearly there can be at most 5 iterations and you can see that the iterations are independent. There is nothing from the first iteration that is necessary in the second iteration. Now we have to figure out one thing however that if the buyer demands a price P or rather the buyer offers to pay a price P then what price Q should you make as a counter offer? Well we said that P and Q should be such that their average is 500. So what does that mean? So the average of P and Q is P plus Q upon 2 and this must be 500. So let us simplify that. So let us take the 2 to the other side and P to the other side. So what do we get? So we get Q must be equal to 1000 minus P. So this in fact tells you precisely what your counter offer should be. So you could think of this as solving an algebraic equation. So you know the value of P and you are trying to figure out the value of Q. So that is exactly what this is. And all I am just alerting you to the possibility that when you write a program you may need to solve equations and this is just an example. So do not worry about it. Equations can be solved. We have shown you how to do it. So Q is this price you should name and that is about it and of course if the price was the P itself was already above 500 then you should stop. So how does this work? So we are going to have a loop which runs 5 times at most. So you are going to tell the user, tell the buyer to make an offer. So maybe the buyer is going to make an offer if the buyer is going to type something and that is going to go into P. If P is bigger than or equal to 500 then you are going to accept the offer. So you are going to print out accepted and you will break. So the loop ends. Otherwise you have to make a counter offer and how much offer, how much value should that counter offer be for? Well we just calculated that. So we are going to make an offer Q equals 1000 minus P. So you could say I am going to sell it for Q and that is the end of the iteration. So at this point the user, the buyer may make another offer, maybe the buyer, so in this protocol the buyer is not really allowed to say accepted. The buyer has to come back and make the same offer at which the seller says accepted. But anyway so if you make an counter offer then the buyer can accept at that price by repeating that offer. So that is the program. So let us see that. So this program I have coded as bargain.cpp and let us see it. So this is the program. So let us compile it. So it says make an offer. So what am I going to say? Maybe I will say 700. It is already accepted. Because why? Because we said that the program accepts if you quote a high price or a price 500 or more. So let us try something different. So this time let us be really annoying. Let us say 50. I mean if you are in the real market and if you see something which is worth 500 you know that this is a really low offer but sometimes you do that, you are fooling around. So at that time the seller also plays your game and he or she says no, no, no, no, not 50, 950. What are you talking about? So now you have to make another offer which is presumably more reasonable. So you might say 200. Or sorry 20 I said. So of course he will say 980 but I meant 200. So at that point the seller will say 800. So if you are reasonable then the seller will also try to be a little bit more reasonable. Maybe at this point you say 400 then the seller is even more reasonable. So if you say 600 at this point the seller will accept. If you had said something else then as you know the loop would have gone, would have continued and it would have stopped after 5 iterations because you cannot go on arguing ad infinitum. So let us get back to the presentation. So what have we discussed? So we discussed a fun problem. It is a very simple example of a real life problem. It has this notion that there is some system which is conversing with the user. So the program is conversing with the user just as the real life system is interacting with the user. But it is a really simple system. But that is okay. It is a little bit of fun. Next we are going to look at a slightly more interesting real life problem. So we will take a quick break.