Welcome, guest | Sign In | My Account | Store | Cart

Economic order quantity (EOQ) model is used in stock control by many companies rather than other stock control systems such as the Just-in-Time model. Though, ACCA’s students will benefit from this program while preparing their ACCA course and exam in financial management towards their ACCA professional qualification, yet financial management professionals already holding posts within companies using this system will be able to compare different sets of item quantities and rates to reduce their company’s total cost of holding and ordering such items.

Python, 146 lines
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#On the name of ALLAH
#Author : Fouad Teniou
#Date : 12/02/09
#version :2.4

""" Economic order quantity model can help reducing the total cost of (Ch,holding cost)
and (Co, ordering cost) of stock. However, an annual demand (D), cost per item (Ci),
quantity of order (X), and discount (S) are used to compute the
EOQ and savings """

import math

#The user try a value equal to zero
class MyZeroException(ArithmeticError): pass

#The user try a negative value
class MyNegativeException(ArithmeticError): pass
 
def myError(value):
    """ Raises MyZeroException and MyNegativeException exceptions."""
     
    if value == 0:
        raise MyZeroException,\
              "<\nQuantity and discount rate should not be equal to zero "
    elif value < 0:
        raise MyNegativeException,\
              "<\nQuantity and discount rate values should be greater than zero "
        
class EOQ:
    """ Class that represent Economic Order Quantity """
    
    def __init__(self,D=0,Ch=0,Co=0,Ci=0,X=0,S=0):
        """ Constructor for class EOQ """
        
        myError(D)
        myError(Ch)
        myError(Co)
        myError(Ci)
        myError(X)
        myError(S)
        
        self.D = D
        self.Ch = Ch
        self.Co = Co
        self.Ci = Ci
        self.X = X 
        self.S = S

        try:
            
            f = [(lambda D,Ch,Co: "%2.2f" %math.sqrt(math.fabs(2*Co*D/Ch))),
                 (lambda Ch,Co,D: "%2.2f" %((Ch*self.A/2)+(Co*D/self.A))),
                 (lambda Ch,Co,D,X: "%2.2f" %((Ch*X/2)+(Co*D/X))),
                 (lambda Ci,D,S : "%2.2f" %(Ci*D*S/100))]
            self.A = float(f[0](D,Ch,Co))
            self.B = f[1](Ch,Co,D) 
            self.C = "%2.0f" %(float(f[2](Ch,Co,D,X)))
            self.E = float(self.C)-float(self.B)
            self.F = float(f[3](Ci,D,S))
            self.G = "%2.0f" %(Ch*X/2)
            self.H = "%2.0f" %(Co*D/X)
            self.I = "%2.0f" %(((self.A/2)*Ch)+((D/self.A)*Co))
            self.J = "%2.0f" %(-(float(self.C) - float(self.I)) + (float(self.F)*100))

        #EOQ raise ValueError if input is not numerical
        except ZeroDivisionError:
            print "Holding cost should not be equal to zero "
            
        except MyZeroException, exception:
            print exception
            
        except MyNegativeException, exception:
            print exception
                       
    def compare(self,K,L):
        """ Compare a different quantity and discount rate """

        myError(K)
        myError(L)
        
        self.K = K
        self.L = L
        
        try:
            g =[(lambda Ch,Co,D,K: "%2.2f" %((self.Ch*K/2)+(self.Co*self.D/K))),
                (lambda Ci,D,L : "%2.2f" %(self.Ci*self.D*L/100))]
            self.M = float(g[0](self.Ch,self.Co,self.D,K))
            self.N = float(g[1](self.Ci,self.D,L))
            self.O = "%2.0f" %(-(float(self.M) - float(self.I)) + (float(self.N)*100))
            self.P = float(self.O)- float(self.J)
            print "\n<The total cost of ordering batches of %s is: %s " % (self.K,int(self.M))

            if self.P >= 0:
                return "\n<The extra saving of ordering batches of %s is %s " % (K,int(self.P))
            else:
                return "\n<The extra loss of ordering batches of %s is %s " % (K,int(self.P))
                
        except MyZeroException, exception:
            print exception
            
        except MyNegativeException, exception:
            print exception
            
    def __str__(self):
         """ String representation of the EOQ, annual order and holding cost,
         total annual inventory cost to obtain discount and using EOQ, and the
         annual profit or loss arise """
         
         print "\n<EOQ = %2.0f \n \
                \n<The annual order cost = %s\n \
                \n<The annual holding cost = %s\n \
                \n<The total annual inventory cost to obtain discount  = %s\n\
                \n<The total annual inventory cost using EOQ  is = %s" \
                % (self.A,self.H,self.G,self.C,self.I)
         
         if int(self.J)> 0:
            return "\n<The annual profit of ordering in batches of %s is %s " % (self.X,self.J) 
         else:
            return "\n<The annual loss of ordering in batches of %s is %s " % (self.X,self.J)
                  
if __name__ == "__main__":
    
    test = EOQ(37000,0.70,300,3.4,3000,0.005)
    print test
    print test.compare(7000,0.0075)
#############################################################    
#c:\hp\bin\Python>python "C:\Fouad #Teniou\Documents\Programs\EOQModel7.py"

#<EOQ = 5632

#<The annual order cost = 3700

#<The annual holding cost = 1050

#<The total annual inventory cost to obtain discount  = 4750

#<The total annual inventory cost using EOQ  is = 3942

#<The annual loss of ordering in batches of 3000 is -179

#<The total cost of ordering batches of 7000 is: 4035

#<The extra saving of ordering batches of 7000 is 1030


#c:\hp\bin\Python>

10 comments

Rodney Drenth 15 years, 2 months ago  # | flag

You need to study the code of advanced programmers for awhile before you're ready to post your own code here.

Fouad Teniou (author) 15 years, 2 months ago  # | flag

I think you are out of order completely with your comment, the fact that EOQ is running and performing its task while catching errors and giving the right results, which you could test yourself from The ACCA approved book ( Financial management Paper 9). However, I studied software development ( ADA language ) in the past at South Bank University in London and gained my module in the subject., and I graduated at Dundee College in Scotland with a HND in Accounting plus a Merit in every module ( 9 Merits in total ) and a special recognition by the college. I realised that you started posting your own programs in January, yet you wrote only one program and the comment you made is not right and rude, because you do not need to study the code of advanced programmers to post a program, though, Lambda function is special and only used by cleaver programmers.

Alia Khouri 15 years, 2 months ago  # | flag

A few friendly points of advice:

Given the defensiveness of your response, perhaps you took Rodney's comment a bit too personally. Irrespective of what one's capabilities are as a programmer one should always strive to understand how to improve the formal and functional elegance of one's code. In the case above, your program may do what you want, but it is presumptuous to consider it to be of value unless you start paying attention to what actually constitutes well-written code. The program above is certainly not written in a pythonic (readable and idiomatic) style, and is therefore (in its current form) less ready to be shared to the wider public. Specifically:

  • Minimal documentation is an issue. Please provide more context for what this does.

  • Your error checking/messages is buggy. (myError(Ci) -> gives a message about quantity and discount rate??

  • Variable naming should be as meaningful as possible, so to this end try to avoid abbreviations and single letter variables as much as you can.

  • You can do away with almost all your line-endings and escapes by triple quoting your strings

  • over-frequent casting from int to float and back is probably not a good sign in a dynamic language like python.

  • lambdas are not intrinsically clever and are generally less readable than their more powerful 'def' cousins. In the context that you use them, they are even less appropriate.

As a further suggestion: being from the Middle East myself, I don't think it is necessary for you to preface every bit of code you write with a religious statement. I know you are simply following a social custom and that it is probably automatic for you, but one should also consider the social customs of people who read python, who are for the most part less interested in knowing what religious persuasion you belong to and will most likely judge you on the elegance, readability and usefulness of your code.

I will reiterate Rodney's advice as well. If you prefer to write in python, you would do well to study the python standard library for examples of well-written code. However, if you must write in a mathematical style, Haskell is also a very nice and advanced language to learn.

Best,

AK

Fouad Teniou (author) 15 years, 2 months ago  # | flag

I would like to advise you that I am not from the Middle east and that not every one living in the middle east is a Muslim, as some pretend to be but do have marks to prove they are not. However, I realised that you posted only one program in this site and I noticed your weaknesses in writing python programs, thus, it does not make sense advising people on how to write python code or their religion purpose, while it is an extreme ignorance to mention unless you do have a mark yourself, which could be checked. My programs are running and performing their task while free of errors and are written for people who want to achieve results in the subject such as Mathematics and Financial Management and not people who want to learn Python as your self may need. And if you still think that you do not have a mark and you are good in python, I advice you to check the 2438 recipes, because you will find more than 70% of the programs needs advice, so it will make you busy writing comments as you wish.

PS Thank you for your comment

Alia Khouri 15 years, 2 months ago  # | flag

Evidently, it is difficult for you to accept friendly constructive criticism, and I will let your response speak for itself.

In any case, you have chosen to post your Mathematics and Financials management program to a site dedicated to python recipes so it is perfectly reasonable for you to receive comments about the readability of your code, especially if someone finds it wanting. Don't take it personally and you will probably benefit in the long run.

Salam,

AK

Fouad Teniou (author) 15 years, 2 months ago  # | flag

You should know that in this site people are free to write python programs in any subject they wish, and Mathematics and Financial Management is my field. And people like yourself are free to choose to have copies or not, without any obligations. And I do accept comment because I believe that criticism and compliments help every person to do things better and achieve higher positions in their careers, but not comments of conspiracy and not comments on other people comments as your comment suggest. I looked at your program, and realised that it was not an expert in python code, while you criticized my documentation, and not fulfilling yours, there is a module called doctest and other tools that might help you progressing in the documentation field. I do have copies of every program I posted and it is running perfectly, however, no programmer on earth can affirm that their programs are 100% perfect, and just to remind you that I am not a Python tutor and I am not teaching Python in this site, but just trying to help ACCA’s Students and other Universities mathematics students using CALCULUS to solve their exams and practice questions, not students want to learn python. I hope you will appreciate. And if you think you are an expert in python, I suggest for you to apply for python’s tutor post, because they are scarce.

Alia Khouri 15 years, 2 months ago  # | flag

Again, I am not implying that there is anything wrong with the functionality of your program, and even if I wanted to make that assertion I wouldn't be in a position to anyway given the lack of context and references. In my limited capacity, I am merely giving you advice on how to improve the readability of your code such that you may increase the chances of it being better understood and hence of greater utility to others.

As for your comments about my little contributions: you are absolutely correct, I am by no means an expert in python or in programming in general. Nevertheless, given our medium of communication, may I suggest that you focus more on the content and tone of what is being said or advised rather than on ambiguous notions of expertise or authority. Sometimes the experts are not so expert (witness the current financial meltdown), and sometimes the experts are too busy, well, being experts.

In any case, please feel free to advise me on how to improve my code. I look forward to your comments.

Sincerely,

AK

teniou_25 15 years ago  # | flag

je nesuis pas assez fort en mathématiques mais je suis ton frére ok

Fouad Teniou (author) 14 years, 11 months ago  # | flag

Bonjour, j’espère bien que vous m’envoyer pas des messages ou me contacter a travers se réseaux, parce que c’est seulement pour les programmes d’informatique en anglais

Merci

Fouad Teniou

P.S Ne répondez pas a se message !!!

Fouad Teniou (author) 14 years, 11 months ago  # | flag

P.S.2 Et j’espère bien que vous suspendais votre nom immédiatement de se réseaux

Created by Fouad Teniou on Thu, 12 Feb 2009 (MIT)
Python recipes (4591)
Fouad Teniou's recipes (37)

Required Modules

Other Information and Tasks