For school I am supposed to use recursive backtracking to solve a Boat puzzle. The user inputs a maximum weight for the boat, the amount of item types, and a weight and value for each item type. More than one of each item type can be placed on the boat.
Our assignment states "The program should find a solution that fills the boat with selected valuable items such that the total value of the items in the boat is maximized while the total weight of the items stays within the weight capacity of the boat."
It also has pretty specific template for the recursive backtracking algorithm.
Currently I am using contiguous lists of items to store the possible items and the items on the boat. The item struct includes int members for weight, value, count (of how many times it is used) and a unique code for printing purposes. I then have a Boat class which contains data members max_weight, current_weight, value_sum, and members for each of the contiguous lists, and then member functions needed to solve the puzzle. All of my class functions seem to be working perfectly and my recursion is indeed displaying the correct answer given the example input.
The thing I can't figure out is the condition for extra credit, which is, "Modify your program so that it displays the best solution, which has the lowest total weight. If there are two solutions with the same total weight, break the tie by selecting the solution with the least items in it." I've looked at it for awhile, but I'm just not sure how I can change it make sure the weight is minimized while also maximizing the value. Here is the code for my solution:
bool solve(Boat &boat) {
if (boat.no_more()) {
boat.print();
return true;
}
else {
int pos;
for (int i = 0; i < boat.size(); i++){
if (boat.can_place(i)) {
pos = boat.add_item(i);
bool solved = solve(boat);
boat.remove_item(pos);
if (solved) return true;
}
}
return false;
}
}
All functions do pretty much exactly what their name says. No more returns true if none of the possible items will fit on the boat. Size returns the size of the list of possible items. Adding and removing items change the item count data and also the Boat current_weight and value_sum members accordingly. Also the add_item, remove_item and can_place parameter is the index of the possible item that is being used. In order to make sure maximized value is found, the list of possible items is sorted in descending order by value in the Boat's constructor, which takes a list of possible items as a parameter.
Also here is an example of what input and output look like:
Any insight is greatly appreciated!
It turned out that the above solution was correct. The only reason I was getting an incorrect answer was because of my implementation of the nomore() function. In the function I was checking if any item in the possible items list was less than the weight left on the boat. I should have been checking if they were less than or equal to the weight on the boat. A simple mistake.
The wikipedia entry was indeed of use and I enjoyed the comic :)
Related
I have a model that has one attribute with a list of floats:
values = ArrayField(models.FloatField(default=0), default=list, size=64, verbose_name=_('Values'))
Currently, I'm getting my entries and order them according to the sum of all diffs with another list:
def diff(l1, l2):
return sum([abs(v1-v2) for v1, v2 in zip(l1, l2)])
list2 = [0.3, 0, 1, 0.5]
entries = Model.objects.all()
entries.sort(key=lambda t: diff(t.values, list2))
This works fast if my numer of entries is very slow small. But I'm afraid with a large number of entries, the comparison and sorting of all the entries will get slow since they have to be loaded from the database. Is there a way to make this more efficient?
best way is to write it yourself, right now you are iterating over a list over 4 times!
although this approach looks pretty but it's not good.
one thing that you can do is:
have a variable called last_diff and set it to 0
iterate through all entries.
iterate though each entry.values
from i = 0 to the end of list, calculate abs(entry.values[i]-list2[i])
sum over these values in a variable called new_diff
if new_diff > last_diff break from inner loop and push the entry into its right place (it's called Insertion Sort, check it out!)
in this way, in average scenario, time complexity is much lower than what you are doing now!
and maybe you must be creative too. I'm gonna share some ideas, check them for yourself to make sure that they are fine.
assuming that:
values list elements are always positive floats.
list2 is always the same for all entries.
then you may be able to say, the bigger the sum over the elements in values, the bigger the diff value is gonna be, no matter what are the elements in list2.
then you might be able to just forget about whole diff function. (test this!)
The only way to makes this really go faster, is to move as much work as possible to the database, i.e. the calculations and the sorting. It wasn't easy, but with the help of this answer I managed to actually write a query for that in almost pure Django:
class Unnest(models.Func):
function = 'UNNEST'
class Abs(models.Func):
function = 'ABS'
class SubquerySum(models.Subquery):
template = '(SELECT sum(%(field)s) FROM (%(subquery)s) _sum)'
x = [0.3, 0, 1, 0.5]
pairdiffs = Model.objects.filter(pk=models.OuterRef('pk')).annotate(
pairdiff=Abs(Unnest('values')-Unnest(models.Value(x, ArrayField(models.FloatField())))),
).values('pairdiff')
entries = Model.objects.all().annotate(
diff=SubquerySum(pairdiffs, field='pairdiff')
).order_by('diff')
The unnest function turns each element of the values into a row. In this case it happens twice, but the two resulting columns are instantly subtracted and made positive. Still, there are as many rows per pk as there are values. These need to be summed, but that's not as easy as it sounds. The column can't be simply be aggregated. This was by far the most tricky part—even after fiddling with it for so long, I still don't quite understand why Postgres needs this indirection. Of the few options there are to make it work, I believe a subquery is the single one expressible in Django (and only as of 1.11).
Note that the above behaves exactly the same as with zip, i.e. the when one array is longer than the other, the remainder is ignored.
Further improvements
While it will be a lot faster already when you don't have to retrieve all rows anymore and loop over them in Python, it doesn't change yet that it results in a full table scan. All rows will have to be processed, every single time. You can do better, though. Have a look into the cube extension. Use it to calculate the L1 distance—at least, that seems what you're calculating—directly with the <#> operator. That will require the use of RawSQL or a custom Expression. Then add a GiST index on the SQL expression cube("values"), or directly on the field if you're able to change the type from float[] to cube. In case of the latter, you might have to implement your own CubeField too—I haven't found any package yet that provides it. In any case, with all that in place, top-N queries on the lowest distance will be fully indexed hence blazing fast.
i am trying to implement a minimax algorithm from scratch in java.
General point is known to all, through a tree i try to find the best possible move.
I dont have something crucial from code to show now, first i would like someone to give me a general approach so i can start the project and then update the post with my code.
In addition to that i am also going to implement a random heuristic for my game, that will in random choose the next move and pass it to the game, but this will be added later on.
I will be adding bounty on this question.
P.S.
This is not a duplicate, i dont want to copy someone else's code, i have to do the whole code on my own.
So the main thinking to follow is the following:
Calculate the best move with the given game field and the rating mechanism
field the input field where to calculate from ratingMechanisms the mechanism to rate the move and return the best move.
In a more in depth analysis:
The key to the Minimax algorithm is a back and forth between the two players, where the player whose "turn it is" desires to pick the move with the maximum score. In turn, the scores for each of the available moves are determined by the opposing player deciding which of its available moves has the minimum score. And the scores for the opposing players moves are again determined by the turn-taking player trying to maximize its score and so on all the way down the move tree to an end state.
A description for the algorithm, assuming X is the "turn taking player," would look something like:
-If the game is over, return the score from X's perspective.
-Otherwise get a list of new game states for every possible move
-Create a scores list
-For each of these states add the minimax result of that state to the scores list
-If it's X's turn, return the maximum score from the scores list
-If it's O's turn, return the minimum score from the scores list
-You'll notice that this algorithm is recursive, it flips back and forth between the players until a final score is found.
In the main part you should use a for in order to minimize or maximize each level (also you can try adding some debugging like this: //System.out.println("max/min level " + i);), then while getting the current level of the tree check if it is null so you get no exceptions, and for the that level add it in the max node, while parsing in the min node.
In order to use the max and min nodes you have create the functions for these.
The maximize part of the algorithms: The node will get the biggest rating of his children. WARNING: if no child exists, the rating will be the min.
For the minimize part of the algorithms. The node will get the lowest rating of his children. WARNING: if no child exists, the rating will be max.
I haven't implemented anything yet, but I was thinking of using recursion to identify all cells in a grid that are 'actively connected' to a given cell, i.e., cells that are 'active' and directly connected by virtue of sharing a face with the (active) cell in question or more distantly/indirectly connected by sharing a face with one of its (active) neighbors. The disconnections come about because some of the cells in the grid maybe deemed 'inactive' (by whatever definition). My idea/pseudo-code is as follows:
//Call function to traverse connections
traverse_connections(cell);
//Traverse function definition
bool traverse_connections(cell) {
//Check if cell is inactive or traversed - base case
if (current cell is inactive or traversed) {
return true;
}
//-Mark cell then move on to its neighbors
Mark cell as traversed and add it to the list of 'actively connected' cells;
bool ok = true;
for(neighbor_cell in neighbors of cell) {
ok &= traverse_connections(neighbor_cell);
}
return ok;
}
I think this covers the base case, marks the cell, then moves on to do the same for its neighbors, its neighbors' neighbors, and so on. Does this look OK? Are there any obvious gaps I am missing? Would appreciate if anyone with expertise in graph connectivity and recursion can weigh in. I am also having trouble coming up with an iterative equivalent. Any ideas on that would be much appreciated too.
Thanks in advance!
This solution will not work, because it mixes up the concepts of "traversed" and "active/connected". The two are orthogonal: for example, a cell may be inactive and traversed, or active and not traversed. The if statement at the top of your pseudocode returns true for both, which is incorrect.
You should keep a table that marks cells traversed separate from the table that marks cells active. You need to make sure that a cell is marked traversed before going into the recursive call, otherwise the solution may be too slow, or even run out of stack.
Finally, the solution does not need to be recursive: you can accomplish what you need by a straightforward Breadth-first search, which can be done with a queue instead of recursion.
The way you are marking the cells at the moment will most likely cause you problems. For example if one of these cells you are marking has neighbouring cells which need to be traversed later then they may go untraversed.
If you look into the A* (A-Star) algorithm it should give you a good idea of graph traversal. UCS (uniform cost search) is also another algorithm which uses graph traversal.
Thanks for looking at this question in advance.
I am trying to order the following list of items:
Bpgvjdfj,Bvfbyfzc
Zjmvxouu,Fsmotsaa
Xocbwmnd,Fcdlnmhb
Fsmotsaa,Zexyegma
Bvfbyfzc,Qkignteu
Uysmwjdb,Wzujllbk
Fwhbryyz,Byoifnrp
Klqljfrk,Bpgvjdfj
Qkignteu,Wgqtalnh
Wgqtalnh,Coyuhnbx
Sgtgyldw,Fwhbryyz
Coyuhnbx,Zjmvxouu
Zvjxfwkx,Sgtgyldw
Czeagvnj,Uysmwjdb
Oljgjisa,Dffkuztu
Zexyegma,Zvjxfwkx
Fcdlnmhb,Klqljfrk
Wzujllbk,Oljgjisa
Byoifnrp,Czeagvnj
Into the following order:
Bpgvjdfj
Bvfbyfzc
Qkignteu
Wgqtalnh
Coyuhnbx
Zjmvxouu
Fsmotsaa
Zexyegma
Zvjxfwkx
Sgtgyldw
Fwhbryyz
Byoifnrp
Czeagvnj
Uysmwjdb
Wzujllbk
Oljgjisa
Dffkuztu
This is done by:
Taking the first pair and putting the names into a list
Using the second name of the pair, find the pair where it is used as the first name
Add the second name of that pair to the list
Repeat 2 & 3
I am populating an unordered_map with the pairs then sorting and adding each name to a list. This can be seen in the following code:
westIter = westMap.begin();
std::string westCurrent = westIter->second;
westList.push_front(westCurrent);
for(int i = 0; i < 30; i++)
{
if(westMap.find(westCurrent) != westMap.end())
{
//find pair in map where first iterator is equal to "westCurrent"
//append second iterator of pair to list
}
westIter++;
}
Note: I'm not sure if "push_front" is correct at this moment in time as I have only got the first value inserted.
My question is could someone give me some insight as to how I could go about this? As I am unsure of the best way and whether my thinking is correct. Any insight would be appreciated.
There is but one weakness in your plan. You need to first find the first person of the chain, the Mr New York.
Your algorithm assumes the line starts with the first guy. For that to work, you should first scan the entire map to find the one name that does not appear as a second element. That is Mr New York and you can proceed from there. push_back is what you would need to use here.
Create a data structure that stores a chain, its front and back. Store in a hash table with 'back' as key.
Create a bunch of singleton chains (one for each element)
Iteratively, pick a chain find its 'front' in the hash table (i.e. find another chain that has the same element as 'back') and merge them
Do it until you are left with only one chain
Here is a recursive function that I'm trying to create that finds all the subsets passed in an STL set. the two params are an STL set to search for subjects, and a number i >= 0 which specifies how big the subsets should be. If the integer is bigger then the set, return empty subset
I don't think I'm doing this correctly. Sometimes it's right, sometimes its not. The stl set gets passed in fine.
list<set<int> > findSub(set<int>& inset, int i)
{
list<set<int> > the_list;
list<set<int> >::iterator el = the_list.begin();
if(inset.size()>i)
{
set<int> tmp_set;
for(int j(0); j<=i;j++)
{
set<int>::iterator first = inset.begin();
tmp_set.insert(*(first));
the_list.push_back(tmp_set);
inset.erase(first);
}
the_list.splice(el,findSub(inset,i));
}
return the_list;
}
From what I understand you are actually trying to generate all subsets of 'i' elements from a given set right ?
Modifying the input set is going to get you into trouble, you'd be better off not modifying it.
I think that the idea is simple enough, though I would say that you got it backwards. Since it looks like homework, i won't give you a C++ algorithm ;)
generate_subsets(set, sizeOfSubsets) # I assume sizeOfSubsets cannot be negative
# use a type that enforces this for god's sake!
if sizeOfSubsets is 0 then return {}
else if sizeOfSubsets is 1 then
result = []
for each element in set do result <- result + {element}
return result
else
result = []
baseSubsets = generate_subsets(set, sizeOfSubsets - 1)
for each subset in baseSubssets
for each element in set
if no element in subset then result <- result + { subset + element }
return result
The key points are:
generate the subsets of lower rank first, as you'll have to iterate over them
don't try to insert an element in a subset if it already is, it would give you a subset of incorrect size
Now, you'll have to understand this and transpose it to 'real' code.
I have been staring at this for several minutes and I can't figure out what your train of thought is for thinking that it would work. You are permanently removing several members of the input list before exploring every possible subset that they could participate in.
Try working out the solution you intend in pseudo-code and see if you can see the problem without the stl interfering.
It seems (I'm not native English) that what you could do is to compute power set (set of all subsets) and then select only subsets matching condition from it.
You can find methods how to calculate power set on Wikipedia Power set page and on Math Is Fun (link is in External links section on that Wikipedia page named Power Set from Math Is Fun and I cannot post it here directly because spam prevention mechanism). On math is fun mainly section It's binary.
I also can't see what this is supposed to achieve.
If this isn't homework with specific restrictions i'd simply suggest testing against a temporary std::set with std::includes().