Python Two Dimensional Bin Packing - python

Alright so I am trying to make a program that prompts for a square bin dimension using a list of lists to fill this list of lists with squared blocks. It also prompts for a text file for example: blockList.txt:
3 1 2 1 3
I have a function that splits that up into a list and tries to fill in the space of the lists using the First Fit descending algorithm. The problem is is that the function only fills the highest valued item in the list and then stops and prints the grid. Can someone help me figure out why it isn't looping correctly? All help would be much appreciated
Here is my code:
https://gist.github.com/anonymous/1ac55a8fcb350d0992a4

I'm not 100% on python syntax, but it seems you called your placement() function in your pack() function before you defined your placement() function. That could be messing you up.

Related

Calculate the value of a function for each row in a matrix without iteration through all rows

I'm developing a genetic program and by now the whole algorithm appears to be fine. (Albeit slow...).
I'm iterating through lists of real values, one at a time and then applying a function to the list. The format is something like :
trainingset=[[3.32,55,33,22],[3.322,5,3,223],[23.32,355,33,122]...]]
Where each inner list represents a line in the set and the last item of that list is the result of the regression in that line/individual.
The function I use is some thing like:
def getfitness(individual,set):
...
for elem in set:
apply the function individual to it
fitness=fitness+(set[-1]-(result of individual with the parameters of the set))
fitness=RMS(fitness)
return fitness
So, what I'de like to know is , is there a way of calculating the function in one go, are there any libs that can do this ? I've been looking at matrices in numpy but to no avail.
Thanks in advance.
Jorge

Need help understanding some code (Beginner)

I am trying to learn about while and for loops. This function prints out the highest number in a list. But, I'm not entirely sure how it works. Can anyone break down how it works for me. Maybe step by step and/or with a flowchart. I'm struggling and want to learn.
def highest_number(list_tested):
x=list_tested[0]
for number in list_tested:
if x<number:
x=number
print(x)
highest_number([1,5,3,2,3,4,5,8,5,21,2,8,9,3])
One of the most helpful things for understanding new code is going through it step by step:
PythonTutor has a visualizer: Paste in your code and hit visualize execution.
What this is going form the first to the last number and saying:
Is this new number bigger than the one I have? If so, keep the new number, if not keep the old number.
At the end, x will be the largest number.
See my comments for step by step explanation of each line
def highest_number(list_tested): # function defined to take a list
x=list_tested[0] # x is assigned the value of first element of list
for number in list_tested: # iterate over all the elements of input list
if x<number: # if value in 'x' is smaller than the current number
x=number # then store the value of current element in 'x'
print(x) # after iteration complete, print the value of 'x'
highest_number([1,5,3,2,3,4,5,8,5,21,2,8,9,3]) # just call to the function defined above
So basically, the function finds the largest number in the list by value.
It starts by setting the large number (x) as the first element of list, and then keeps comparing it to other elements of the list, until it finds an element which is greater than the largest number found till now (which is stored in x). So at the end, the largest value is stored in x.
Looks like you are new to the programming world. Maybe you should start with some basic concepts, for/while loops are some among which, that would be helpful for you before jumping into something like this.
Here is one of the explanations you may easily find on the Internet http://www.teamten.com/lawrence/programming/intro/intro8.html

Using random function to find "cut off" values on python

I need help with this problem and I think the major reason I don't know how to do it is because I don't really understand what it is asking for.
The problem is asking to write a program that will find the curve of a class. It has to find the "cut off" values for A,B,C and D. 40% of the class will receive an A, as well as a B(40%), 10% will receive a C and the remaining 10% will get a D.
The grades have to be generated randomly between 20 - 100 and the number of student has to be an input. a tuple has to be returned with the cut off values of A,B,C and D.
It also gives me the hint to use list.sort() and min(list)
I honestly don't really get what this is asking for, so far I have this but I am not sure what I am doing.
import random
def Cutoffgrade():
students=float(input('How many students are in the class?'))
grades=list(range(20,101))
list.sort(grades)
Astudents=students*0.4
Bstudents=students*0.4
Cstudents=students*0.1
Dstudents=students*0.1
I also have this other function I did which I know is something similar to what I have to do, but I just don't get this problem.
Here is the other function I did for a different problem that is somewhat asking for a random list as well:
def createlist(v1,v2):
random.randint(v1,v2)
random.randrange(v1,v2)
numlist=[]
for i in range(5):
n=random.randrange(v1,v2)
numlist.append(n)
print(numlist)
So I have been trying to transform this function into what I would need for the new problem but don't know how.
Since this is a homework question and I imagine your professor isn't very likely to get back to you during the weekend I will give you some hints at how to tackle this:
First build a list of grades using random.randint(start, stop) and a list comprehension or standard for loop
Sort the list.
Determine how many students should be in each grade bracket.
Slice the list into the appropriate chunks and find the min() of each list.

Speed/structure optimization for a recursive tree

Edit -> short version:
In Python, unlike in C, if I pass a parameter to a function I -say: a dict-, the changes made within the function call will reflect outside (as if I passed a pointer instead of just the value)
I want to avoid this so:
-> I make a copy of my dict and pass the copy to my function
But the values of my dict can be some dict and this goes on until an undefinite depth
-> the recursive copy is very long.
Question: what is a pythonic way to go about this?
Long version:
I'm coding a master-mind playing robot with a n-digit code in Python.
You try to guess the code and for each try you get an answer in terms of how many white/black/none you have, meaning resp. "good digit good position"/"good digit wrong position"/"wrong digit" (but you don't know to which digit the whites/blacks/none refer)
I analyze the answers and build a tree of possibilities with a dictionary storing white/black/none.
I store a map of the possible positions of the numbers 0-9 within the code (a digit can appear more than once) in a list.
Ex: for a 3-digit game I will have [[x,y1,y2,y3][-1,0,1,4][...][...][][][][][][]] with:
x: the total number of times this digit appears in the code (default value being n+1, ie. 4 in the exemple) with positive meaning sure and negative "at least"
y1,y2,..,yn the position within the code: 1 means I know the digit is in this position, 0 I know it's not, and 4 (or anything) as default
In my exemple: I know that '1' appears at least once in the code (-1) that it is present in position 2 and that it is NOT present in position 1 and that position 3 is still hypothetically possible.
While I explore my tree of possibilities, I update this list. Which means that each branch of the tree will have its own copy of the list.
Since I recently discovered that, unlike in C, when I pass my list to a sub-method, any change made to it within the sub will reflect on the list outside, I manually copy my list each time with a small method:
def bak_symb(_s):
_b = [[z for z in _s[i]] for i in xrange(10)]
return _b
Now, I profiled my programm and noticed that 90% of the time is spent either in
append()
(the branches of my tree are nested dictionaries {w:{},b:0,n:{}} to which I append each branch of possibilities that I explore)For each branch : the programm has to find a n-digit code
or
my copying function
So I have three questions.
Is there a way to make this function faster?
Is there a something better adapted than the structures I chose (2-depth list for the symbols and nested dict for the hypothesis)
Is there a more adequate way of doing this than building this huge tree
All comments and remarks are welcome.
I'm self-taught in and might have missed some obvious pythonic way of doing some things.
Last but not least, I tried to find a good compromise between making this short and clear, here again don't hesitate to ask for more details.
Thanks in advance,
Matt

Python Grid Generator

I am trying to develop a code that solve a Dirichlet boundary value problem (Poisson's Equation). The main issue that I have is that I do not have any idea how to write a "grid generator" to generate my domain from which to extract my matrices. The python programming course I took never touched on anything like this. I will leave my domain and the discretization of the differential operator below. Any help to get me started would be much appreciated. Hope this explanation was clear!
If it is too small to see, the y axis goes from 0-4 and the x from 0-5.
A grid can be thought of as a list wherein each element in the list is itself a list. This nets you a two-dimensional matrix-like object:
myList = [[None for i in range(10)] for k in range(10)]
will, for instance, make you a 10x10 grid. You can change the expression to create grids of varying sizes and content (ie, instead of None substitute your computation, and instead of the range(10) expressions you substitute whatever it is that conditions the length of the row/col).

Categories