|
Wow: Looks like my question is akin to smacking a wasp nest with a cricket bat. Glad to see it sparked some convo.
So with what I found that was suggested by Harold and Luc, it seems like I could get away with just a simple priority list and just do the fat jobs first. There are no dependencies (job x has to be done before job y), so there's nothing stopping me from doing the jobs in any order.
But I'm not sold that this is the way to achieve the quickest completion time. I might be wrong but there might be some cases where this wouldn't work. So I'll have to look around a little more--fortunately the links and ideas given were a good step toward what I was looking for.
|
|
|
|
|
fishing season ain't over yet.
|
|
|
|
|
Here is a simple example where biggest job first does not yield the optimum:
two machines; five jobs: 3,3,2,2,2
|
|
|
|
|
What will be the typical size of the instances of the problem that you want to solve?
For up to about 20 jobs solving the problem optimally with branch and bound is still doable..
Otherwise, it seems that in practice it is usual to use a genetic algorithm or simulated annealing - both have a tweakable time/quality trade-off and without too much trickery you can make it so that the result is guaranteed to be no worse than that of the greedy algorithm.
|
|
|
|
|
Eh... maybe 50 jobs. And the number of jobs keeps on growing too...
|
|
|
|
|
IMO the more jobs the more "biggest job first" gets nearer to the optimal solution.
|
|
|
|
|
Luc Pattyn wrote: IMO the more jobs the more "biggest job first" gets nearer to the optimal solution
I vote for "pays most".
The wonderful thing about the Darwin Awards is that everyone wins, especially the members of the audience.
|
|
|
|
|
Ok with the code I gave earlier that would take more than a million years
With a larger number of jobs, the greedy algorithm comes closer to the optimal solution, and there is a semi-greedy algorithm that is always* at least as good as the simple greedy algorithm but usually better:
1) Let s be the sum of all job-lengths and let bound be (s+m-1)/m (where m is the number of machines)
2) For each job in descending order (by length), assign it to the first machine where it wouldn't cause the sum of the lengths of the jobs assigned to that machine so far to exceed bound , if there is no such machine mark the job as "non scheduled".
3) If there are no jobs marked "non scheduled", return the schedule.
4) Otherwise, take the length (mlength) of the schedule of the machine with the shortest schedule, and take the length (jlength) of the smallest job.
5) Change the bound to bound+(jlength-(bound-mlength)) (IOW, make it so that you could add the smallest job to the smallest schedule without exceeding the bound )
6) Make all jobs non-marked and go back to step 2.
It is, of course, slower than the usual greedy approach, but (usually, at least) very fast compared to the optimal approach.
It always terminates, because jlength-(bound-mlength) can not be zero. If it were, that job would have been scheduled in step 2, so it wouldn't be the smallest "non scheduled" job. So the bound will grow until everything fits (potentially in a sub-optimal way).
* I have no proof, but after a large number of random tests I still had no disproof. But you could always run the usual greedy algorithm as well and take the best result, it takes essentially no time anyway.
edit: I didn't make this up myself, btw.
modified on Monday, April 19, 2010 7:26 PM
|
|
|
|
|
An aerosol of solvent is my first choice for a wasps nest.
No Smoking.
No Electric.
etc
Tadeusz Westawic
An ounce of Clever is worth a pound of Experience.
|
|
|
|
|
Hello All,
I was just stopping-by to make a rare post and I poked into this thread.
In my mind I think of these kinds of problems as 'least energy state' problems. These very often correlate to conditions in analog setttings. By thinking one's way forth-and-back through analog-digital conversions, some unnoticed feature of the problem often presents itself. Your analogue thougtht-picture of blocks in a tall wall is an example.
I imagined myself with a large appliance box and spheres of varying diameter representing the jobs. With spheres in the box I shake the box, stop, note the arrangement of spheres, and repeat.
Anyone else with a thought picture?
Tadeusz Westawic
An ounce of Clever is worth a pound of Experience.
|
|
|
|
|
Need C# codez urgentzzzzz!!!!!
Not really, I have code. However, I am after some advice: under embedded .NET, is this better implemented under event-driven code or in a more linear fashion with the dreaded Application.DoEvents() ? An event-driven implementation looks a bit of a brain-f*** IMO...
|
|
|
|
|
|
I have this 3rd party software that spits out a list of numbers formatted into text. They are integers but of varying number of digits. I usually get around 10,000 numbers in one string.
I'm turning all these numbers into an array of integers in C++ but it's taking me over 200 milli-seconds.
I was told that LabView can do the same thing in less than a milli-second.
Then a kind CP patron ran an experiment on C# and told me it only takes up to 20 milli-seconds.
I'm using CString::Find() to look for the space that separates each number in the string and CString::Mid() to get a piece of string per number and finally atoi() to turn it into integer. I know CString is slow, but I'm a bit surprised at the difference in the time of execution.
What is LabView doing that is so wonderful?
How can I improve my code in C++? Someone said something about not using Mid() to create new string each time but to put a section of char* directly into atoi() . I'm not sure how to do that except for doing things like memcpy in which case I might as well create a new CString as I do now.
Any ideas appreciated!
Almost, but not quite, entirely unlike... me...
|
|
|
|
|
One major observation:
- Get a profiler, and profile your code.
One minor observation:
- it is likely the over-allocation and deleting of memory of CString::Mid killing your performance.
Two options:
- modify the string in place, replacing spaces with \x0's, and use atoi to convert values.
- parse the digits by hand. Pseudo-pseudo code:
set n to 0.
Look at next character
Is it a space? store the integer in the result list.
Is it a number. set n = n * 10 + (int)character - 48.
Also:
- pre-size the resulting list to roughly input string length / estimated digit size, if you happen to know the string length up front.
|
|
|
|
|
My C# implementation, as posted in the Lounge... takes 1ms or less for 10000 numbers. This shows you how to avoid Mid() and therefore the overhead of creating strings, as well as a custom string-to-number function to avoid the overhead of a more generalized function.
static void Main(string[] args)
{
StringBuilder sb = new StringBuilder();
int j = 100;
for (int i = 100; i < 101000; i++)
{
if (j > 999) j = 100;
sb.Append((j*10).ToString() + " ");
j++;
}
string s = sb.ToString();
int idx = 0;
int startidx = 0;
int length = s.Length;
List<int> nrs = new List<int>((int)(length*0.25));
var watch = Stopwatch.StartNew();
do
{
if (s[idx] == ' ')
{
if(idx>startidx)
nrs.Add(atoi(s, startidx, idx));
startidx = idx + 1;
}
idx ++;
}
while (idx < length);
Console.WriteLine(watch.ElapsedMilliseconds);
Console.ReadLine();
}
static int atoi(string c, int start, int len) {
int res = 0;
int i = start;
len += start;
while (i<len && c[i] >= '0' && c[i] <= '9')
res = res * 10 + c[i++] - '0';
return res;
}
|
|
|
|
|
PaulowniaK wrote: How can I improve my code in C++?
Next time, show us your code so we can answer that.
PaulowniaK wrote: CString::Find()
Depending on the overload, this could be your problem. You searching from the beginning of the string each time?
Those superficial observations aside, the above post (by J. Dun... something) looks like a great answer. Though it would have been nice to see your code for comparison.
|
|
|
|
|
This (plain C ) takes 1 ms circa on my system (Dual Core 1.8 GHz).
int get_all_numbers( const char * txt, int txt_size, int * a, int max_count)
{
const char * p0 = txt;
const char * p1 = NULL;
const char * guard = p0+txt_size-1;
int count = 0;
while (p1 < guard && count < max_count)
{
a[count++] = (int) strtol(p0, (char**)&p1, 10);
p0 = p1;
}
return count;
}
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
hii..i need the source code for 15 puzzle problem in C or C++
|
|
|
|
|
then why do you post in the algorithm forum, if all you care about is getting code handed over to you?
|
|
|
|
|
On top of that, they cross posted in the Wicked Code forum too.
"The clue train passed his station without stopping." - John Simmons / outlaw programmer
"Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon
"Not only do you continue to babble nonsense, you can't even correctly remember the nonsense you babbled just minutes ago." - Rob Graham
|
|
|
|
|
If you want someone to just hand over code to you, you've come to the wrong site. You're looking for Rent-A-Coder.com[^].
|
|
|
|
|
i am sorry but i need an algorithm itself for 15 puzzle problem for which i trying hard but unable to get it !
|
|
|
|
|
123lucy45 wrote: i need an algorithm itself
Aha, that is different now. I used to solve such puzzles with an exhaustive search, breadth first. The only caveat is one must detect positions that have been reached before. Not hard at all.
|
|
|
|
|
Wow. A little Googling for "15 puzzle solve algorithm" reveals a bunch of interesting stuff, and examples.
|
|
|
|
|
I'll give you a starting point. This puzzle can be solved efficiently with three algorithms:
1. The simple case: For numbers not in the rightmost column or the last two rows, move tiles to create a blank space between the next tile to place, and the correct location for this tile. Then move the tile into the blank space, bringing you one step closer to correct placement for this tile. Repeat until the tile is in the correct location.
2. For a position in the rightmost column (but not the last two rows), the problem is getting the wrong tile out and the right tile in. Get the right tile under the correct location (in the rightmost column). Then move another tile in the same row as the correct location down one (temporarily moving it out of its correct location), slide the rest of the row to the left, move the right tile up (to its correct position), get a blank spot under the wrong tile, move the wrong tile down, move the rest of the row right, then move the tile you moved out of its correct position back, thus completing the row.
3. For the last two rows, move them in a circular pattern until you get the opportunity to swap two tiles that are in the wrong order. Repeat until the last two rows are correct.
|
|
|
|