By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,768 Members | 2,054 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,768 IT Pros & Developers. It's quick & easy.

Cobyla help needed

P: 2
Hi there guys

I am an engineering student using python to try and optimize an Abaqus FEA simulation and am having problems using the cobyla, basically I havn't a clue. I was wondering could someone illustrate the many problems with the script below?

Cheers
Dan

import numpy
import os
import scipy.optimize.cobyla
import string

def minimise_mass(mass):
os.system("abq661 cae script=bottlesim2ndstep.py") # Function
myfile=open('totalbottle', 'r')
totalbottle = myfile.readline()
print "Total Bottle",totalbottle,"g"

#myfile=open('topload', 'r')
#topload = myfile.readline()
#print "Topload", topload

myfile=open('mainbody', 'r')
mainbody = myfile.readline()
print "Mainbody",mainbody,"mm"

myfile=open('lid', 'r')
lid = myfile.readline()
print "Lid",lid,"mm"

return totalbottle

def constraint_topload(thickness):
myfile=open('topload', 'r')
topload = myfile.readline()
print "Topload",topload,"N"
return topload-200 # constraint saying topload >=200

def constraint_thickness(thickness):
return thickness-2 # constraint saying thickness >=2

thickness=5 # initial guess for thickness
x_opt=scipy.optimize.fmin_cobyla(minimise_mass, thickness, [constraint_topload,
constraint_thickness], consargs=None, rhobeg=1.0, rhoend=1e-10, iprint=1,
maxfun=1000000)
print "optimised thickness", x_opt
#print x
#print y
Mar 12 '08 #1
Share this Question
Share on Google+
2 Replies


jlm699
100+
P: 314
In the future you should try to use [ code ] tags to make your code more readable

Ok so, when you do your readline ops, Python pulls in the end of line operator as well.
Basically your problem is that topload, totlebottle, lid, etc... all these values are in string form, and have a line separator attached (ie, instead of lid = 5 you have lid = '5\n'). Cobyla is expecting numbers, so:

A simple fix would be to simply do the following:
Expand|Select|Wrap|Line Numbers
  1. totalbottle = int(myfile.readline())
  2. # ORRR
  3. totalbottle = float(myfile.readline())
  4. # Depending on what the possible values are, which I'm assuming you'll want float
To further ensure you don't have the new line characters you can do this:
Expand|Select|Wrap|Line Numbers
  1. totalbottle = myfile.readline().strip()
  2. # This will strip off the '\n' but leave your totalbottle value as a string
  3. # So in order to use cobyla with this valueyou'll still need to convert to float or int
  4. totalbottle = float(myfile.readline().strip())
  5. # this last one is redundant, since float() will ignore the white space character (\n) anyway
  6.  
After simply adding the float() conversion and making up random values for the text files I got the following:
Expand|Select|Wrap|Line Numbers
  1. C:\DOCUME~1\ADMINI~1\Desktop\PYTHTE~1>python cobyla.py
  2. Total Bottle 540 g
  3. Mainbody 15 mm
  4. Lid 20 mm
  5. ...
  6. # Doesn't change
  7. ...
  8. Topload 300 N
  9. Total Bottle 540 g
  10. Mainbody 15 mm
  11. Lid 20 mm
  12. Topload 300 N
  13. Total Bottle 540 g
  14. Mainbody 15 mm
  15. Lid 20 mm
  16. Topload 300 N
  17.  
  18.    Normal return from subroutine COBYLA
  19.  
  20.    NFVALS =   13   F = 5.400000E+02    MAXCV = 0.000000E+00
  21.    X = 5.000000E+00
  22. optimised thickness 5.0
  23.  
  24. C:\DOCUME~1\ADMINI~1\Desktop\PYTHTE~1>
Mar 12 '08 #2

P: 2
jlm669

Thanks for the reply was very helpful

Daniel
Mar 14 '08 #3

Post your reply

Sign in to post your reply or Sign up for a free account.