473,378 Members | 1,162 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

selection on fly

Dear Friends,

I am having strange problem. I have a set with different sizes(in
KB). Ex. 66 KB, 25 KB
I am getting another set from database which also having different
sizes. Ex. 20 KB, 14 KB, 35 KB,12 KB, 6 KB

I want take a decission which combination is best fit to 66 KB and
which combination to 25 KB sets. I want to fully utilise the space in
both sets(66 KB and 25 KB).

Can anybody give an idea or an example?

Any reply is highly appreciated. Thanks in advance

Nov 30 '06 #1
1 1099

Hi Ciaran,

Thank you very much for the reply.

Ciaran O''Donnell wrote:
Thinking about your previous question, This doesnt work out what containers
are needed, just how to arrange them to waste the least space. you would need
to change it to try through a set of List<holdingset>'s in accending price
order to get the cheapest shipping.
--

Ciaran O''Donnell
http://wannabedeveloper.space.live.com
"Ciaran O''Donnell" wrote:
I saw a question like this a while ago and had an idea but didnt have time to
investigate. My idea was to work out all the permutations of filling the
holding sets (storage containers for clothes in the last example) and working
out which wasted the least space. I have spent about an hour implementing
this for you (with help for permutation logic) with some sample classes and
it seems to work. You will need to tidy it up and test it more thoroughly. It
is three classes, a holdingset and set and the setAllocator. you can call
DemoAllocateSets to run your example or AllocateSets with a pair of lists and
run it for real.

Let me know how it does for you and post up the fixed up version for the guy
with the chinese cloths factory.

class HoldingSet
{

public HoldingSet(string n, int s) { Name = n; _size = s; }

string _name;

public string Name { get { return _name; } set { _name = value; } }
private int _size;

public int Size
{
get { return _size; }
set { _size = value; }
}
private List<set_sets;

public List<setSets
{
get { return _sets; }
set { _sets = value; }
}

public int Total{
get{
int total=0;
for (int i = 0; i < _sets.Count; i++)
{
total+=_sets[i].Size;
}
return total;
}
}

public int GetWasted(){
return _size-Total;
}

}

class set
{
private string name;

public string Name
{
get { return name; }
set { name = value; }
}
private int size;

public int Size
{
get { return size; }
set { size = value; }
}
public set(string n, int s) { name = n; size = s; }
}

class setAllocator
{

public void DemoArrangeSets()
{

List<HoldingSetholds = new List<HoldingSet>();
holds.Add(new HoldingSet("25hset", 25));
holds.Add(new HoldingSet("66hset", 66));

List<setsets = new List<set>();
sets.Add(new set("20set", 20));
sets.Add(new set("14set", 14));
sets.Add(new set("35set", 35));
sets.Add(new set("12set", 12));
sets.Add(new set("6set", 6));

ArrangeSets(holds, sets);
}
public void ArrangeSets(List<HoldingSetholds, List<setsets)
{
holds.Sort(new Comparison<HoldingSet>(delegate(HoldingSet set1, HoldingSet
set2) { return ((IComparable)set1.Size).CompareTo(set2.Size); }));
sets.Sort(new Comparison<set>(delegate(set set1, set set2) { return
((IComparable)set1.Size).CompareTo(set2.Size); }));

int[] indexesForPerm = new int[sets.Count];
for (int i = 0; i < sets.Count; i++)
{
indexesForPerm[i]=i+1;
}
List<setcurrentPermutation = new List<set>(sets);

List<setbestOrder = new List<set>(currentPermutation);
int bestwaste = int.MaxValue;
int select = sets.Count;
int number = select;
while (true)
{

// for this combination, list permutations in lexicographical order

while (get_next(currentPermutation, select))
{
AssignSets(holds, currentPermutation);
int waste = int.MaxValue;
holds.ForEach(new Action<HoldingSet>(delegate(HoldingSet hset) { waste
+= hset.GetWasted(); }));
if (waste < bestwaste)
{
bestwaste = waste;
bestOrder = new List<set>(currentPermutation);
}

}
// generate next combination in lexicographical order
int i = select - 1; // start at last item

while (i >= 0 && indexesForPerm[i] == (number - select + i + 1)) // find
next item to increment
--i;

if (i < 0) break; // all done
++indexesForPerm[i]; // increment

for (int j = i + 1; j < select; j++) // do next combination
indexesForPerm[j] = indexesForPerm[i] + j - i;
currentPermutation = new List<set>();
foreach (int x in indexesForPerm)
{
currentPermutation.Add(sets[x]);
}
}
AssignSets(holds, bestOrder);

holds.ToString();

}

void AssignSets(List<HoldingSetholds, List<setcurrentPermutation)
{
holds.ForEach(new Action<HoldingSet>(delegate(HoldingSet h) { h.Sets = new
List<set>(); }));

int setindex = 0;
for (int i = 0; i < holds.Count; i++)
{
int total = holds[i].Total;
int size = holds[i].Size;
while (setindex < currentPermutation.Count && total +
currentPermutation[setindex].Size < size)
{
total += currentPermutation[setindex].Size;
holds[i].Sets.Add(currentPermutation[setindex]);
setindex++;
}
if (setindex == currentPermutation.Count) break;

}
}

void swap(List<setcurrentPermutation, int a, int b)
{
set temp = currentPermutation[a];
currentPermutation[a] = currentPermutation[b];
currentPermutation[b] = temp;
}

bool get_next(List<setcurrentPermutation, int k)
{
int i = k - 1;
while (i >0 && currentPermutation[i - 1].Size >= currentPermutation[i].Size)
i--;

if (i < 1) return false; // all in reverse order

int j = k;
while (j >0 && currentPermutation[j - 1].Size <= currentPermutation[i -
1].Size)
j--;

swap(currentPermutation, i - 1, j - 1);

i++;
j = k;

while (i < j)
{
swap(currentPermutation, i - 1, j - 1);
i++;
j--;
}

return true;
}
}
--

Ciaran O''Donnell
http://wannabedeveloper.space.live.com
"mr************@gmail.com" wrote:
Dear Friends,
>
I am having strange problem. I have a set with different sizes(in
KB). Ex. 66 KB, 25 KB
I am getting another set from database which also having different
sizes. Ex. 20 KB, 14 KB, 35 KB,12 KB, 6 KB
>
I want take a decission which combination is best fit to 66 KB and
which combination to 25 KB sets. I want to fully utilise the space in
both sets(66 KB and 25 KB).
>
Can anybody give an idea or an example?
>
Any reply is highly appreciated. Thanks in advance
>
>
Dec 1 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Martin Karlsson | last post by:
Hi guys, Does anyone know how I can restore a selection created with createRange()? The problem is that I have a function that opens a new window with window.open() which of course makes the...
15
by: lawrence | last post by:
Is this the correct way to test for a method before I use it? createRange() is, I believe, an IE only method. function wrapSelectionInTag(selection, tag) { if (document.selection.createRange)...
1
by: bob | last post by:
I asked a question yesterday about how I would parse a Word document using functions in some Interop.Word COM. Some told me to run a macro in Word to see how it works. Most of the code was......
5
by: STeve | last post by:
Hey guys, I currently have a 100 page word document filled with various "articles". These articles are delimited by the Style of the text (IE. Heading 1 for the various titles) These articles...
1
by: 4Ankit | last post by:
Hey guys i am having some trouble with nesting one selection structure within another selection structure. At the moment i am unclear what selection structures are and just need a simple example of...
5
by: Jure Bogataj | last post by:
Hi all! I have a problem (performance issue) with listview. I have implemented an ItemSelectionChange on my listview and put some code in it (I build some toolbar based on selection and update...
7
by: robtyketto | last post by:
Greetings, I'm slowly building up code to do the following:- Display TWO selection option boxes (cascading). If the FIRST selection option box changes then reload the jsp using onchange...
2
by: vanditnagar | last post by:
Hi, I am facing the difficulty in the selecting a text on the browser . And highlighting the text. I am using the window.getSelection() to get the object and after...
3
by: Venturini | last post by:
I am trying to put together a web page where the customer makes choices of products and is then given a total. I am extremely new to Javascript and have managed to get as far as I have from web...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.