473,320 Members | 1,876 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,320 software developers and data experts.

Strange behaviour

Please forgive me for the neverending code down here but I cannot find a
rational explanation of the output of this simple program (really!).
Soluzione class has a double[,] field to represent a matrix. A method
named init() fills this matrix with random numbers. The main constructor
requires an int param (fCosto) which is _not_used_ inside init(). Every
time I instantiate a Soluzione object the matrix is filled by init().
The incredible thing is that if I create more than 9 objects where fCosto
= 3, there is always at least one of them whose matrix contains absurd
values! This does _not_ happen for objects constructed passing 1,2 or 4!

Any suggestions?

I compile with .NET 1.1 SP1 fully patched.

Thanks in advance!

Michael

------------------------------------------------------------------------------------
using System;
using TransGene;

using System.Collections;
namespace Test
{
class Tester: IComparer
{
[STAThread]
static void Main(string[] args)
{
Tester t = new Tester();
int[] prod = {5,5,20,15,15};
int[] req = {8,8,8,8,8,20};
int tot = 10;
Soluzione[] p1 = new Soluzione[tot];
Soluzione[] p2 = new Soluzione[tot];
for (int i = 0; i < p1.Length; i++) {
p1[i] = new Soluzione(prod, req, 3);
p2[i] = new Soluzione(prod, req, 1);
}
Array.Sort(p1,t);
Array.Sort(p2,t);
Console.WriteLine("Fitness "+p1[0].GetFCosto()
+"="+ p1[0].Fitness());
Console.Write(p1[0]);
Console.WriteLine("Fitness "+p2[0].GetFCosto()
+"="+ p2[0].Fitness());
Console.Write(p2[0]);
Console.ReadLine();
}

public int Compare(object o1, object o2) {
Soluzione x = (Soluzione) o1;
Soluzione y = (Soluzione) o2;
// ordino in modo decrescente
return x.Confronta(y);
}
}
}
--------------------------------------------------------------------------------------------

using System;

namespace TransGene
{
public class Soluzione {

private double[,] u;
private int nS;
private int nD;
private int[] prod;
private int[] req;
private int fCosto;

private static Random rand = new Random();

public Soluzione(int[] prod, int[] req, int fCosto) {
nS = prod.Length;
nD = req.Length;
this.req = req;
this.prod = prod;
this.fCosto= fCosto;
u = new double[nS,nD];
this.Init();
}

public Soluzione(Soluzione x) {
nS = x.nS;
nD = x.nD;
req = x.req;
prod = x.prod;
fCosto = x.fCosto;
u = new double[nS,nD];
this.Init();
}

private Soluzione() {
/* costruttore vuoto:
*
* - variabili a 0
* - oggetti a null */
}

public static Soluzione Vuota(Soluzione x) {
Soluzione v = new Soluzione();
v.nS = x.nS;
v.nD = x.nD;
v.req = x.req;
v.prod = x.prod;
v.fCosto = x.fCosto;
v.u = new double[v.nS,v.nD];
return v;
}

private void Init() {
/*Inizializzazione casuale della matrice.
Eseguo nS*nD posizionamenti casuali e
un aggiustamento finale.*/
/* indice di riga (sorgente) */
int si;
/* indice di colonna (destinazione) */
int dj;
/* output corrente della sorgente i */
double sumProdIJ;
/* input corrente della destinazione j*/
double sumReqIJ;
double delta;
double alpha;
for (int c = 0; c < nD*nS; c++) {
si = rand.Next(nS);
dj = rand.Next(nD);
sumProdIJ = 0;
for (int d = 0; d < nD; d++) sumProdIJ += u[si,d];
sumReqIJ = 0;
for (int s = 0; s < nS; s++) sumReqIJ += u[s,dj];
delta = Math.Min(prod[si]-sumProdIJ, req[dj]-sumReqIJ);
alpha = rand.NextDouble();
u[si,dj] += (alpha*delta);
}
/* aggiustamento*/
for (si = 0; si < nS; si++)
for (dj = 0; dj < nD; dj++) {
sumProdIJ = 0;
for (int d = 0; d < nD; d++) sumProdIJ += u[si,d];
sumReqIJ = 0;
for (int s = 0; s < nS; s++) sumReqIJ += u[s,dj];
delta = Math.Min(prod[si]-sumProdIJ, req[dj]-sumReqIJ);
u[si,dj] += delta;
}
}

public double[,] GetU() {
return u;
}

public int GetNS() {
return nS;
}

public int GetND() {
return nD;
}

public Soluzione Clona() {
Soluzione clone = new Soluzione(this);
clone.u = (double[,])this.u.Clone();
return clone;
}

public double Fitness () {
double costo = 0;
double cIJ = 0;
for (int si = 0; si < nS; si++)
for (int dj = 0; dj < nD; dj++) {
switch (fCosto) {
case 1:
cIJ = Math.Pow((Math.Abs(si-dj)+1),2)*u[si,dj];
break;
case 2:
cIJ =
Math.Pow((Math.Abs(si-dj)+1),2)*Math.Pow(u[si,dj], 2);
break;
case 3:
cIJ =
Math.Abs(si*dj-11)*Math.Sqrt(u[si,dj])+Math.Sqrt(u[si,dj]);
break;
case 4:
cIJ = Math.Abs(si*dj-11)*Math.Pow(u[si,dj],
2)+Math.Pow(u[si,dj], 4);
break;
default:
break;
}
costo += cIJ;
}
return costo;
}

public double Merce() {
double merce = 0;
for (int si = 0; si < nS; si++)
for (int dj = 0; dj < nD; dj++)
merce += u[si,dj];
return merce;
}

public Soluzione Muta() {
Soluzione z = this.Clona();
// scelgo a caso un elemento
int si = rand.Next(z.nS);
int dj = rand.Next(z.nD);
double[,] uZ = z.u;
// a caso lo setto al min (0) o al max
uZ[si,dj] = rand.NextDouble() >= 0.5 ? 0 :
Math.Min(prod[si],req[dj]);
// aggiusto la soluzione
for (si = 0; si < z.nS; si++)
for (dj = 0; dj < z.nD; dj++) {
double sumProdIJ = 0;
for (int d = 0; d < z.nD; d++)
sumProdIJ += uZ[si,d];
double sumReqIJ = 0;
for (int s = 0; s < z.nS; s++)
sumReqIJ += uZ[s,dj];
double delta = Math.Min(prod[si]-sumProdIJ,
req[dj]-sumReqIJ);
// alpha = 1 sottinteso
uZ[si,dj] = Math.Max(uZ[si,dj]+delta, 0);
}
return z;
}

public int[] GetProd() {
return prod;
}

public int[] GetReq() {
return req;
}

public int GetFCosto() {
return fCosto;
}

// rimpiazzare con CompareTo
public int Confronta(Soluzione x) {
return this.Fitness().CompareTo(x.Fitness());
}

public bool Uguale(Soluzione x) {
bool uguali = true;
for (int i = 0; i < nS && uguali; i++)
for (int j = 0; j < nD && uguali; j++)
uguali = u[i,j] == x.u[i,j];
return uguali;
}

public override string ToString() {
string s = "";
for (int i = 0; i < nS; i++) {
for (int j = 0; j < nD; j++) {
// s += Math.Round(u[i,j],2).ToString("0.00") + "\t";
s += "u["+i+","+j+"] ="+u[i,j] + "\n";
}
s+="\n";
}
s+="\n";
return s;
}

}

}
----------------------------------------------------------------------------------------------
This is the output
----------------------------------------------------------------------------------------------

Fitness 3=Non un numero reale (that is 'not a number' in Italian)
u[0,0] =0,645759544586035
u[0,1] =0
u[0,2] =0,0175906380334083
u[0,3] =0,349247457857778
u[0,4] =0,0210873891839998
u[0,5] =3,96631497033878

u[1,0] =0,286085618731453
u[1,1] =0,372272107842662
u[1,2] =1,2791667337149
u[1,3] =2,25893076399279
u[1,4] =0,0900398541882886
u[1,5] =0,71350492152991

u[2,0] =3,52830816941484
u[2,1] =5,272095936
u[2,2] =4,32616900310448
u[2,3] =4,57385969305198
u[2,4] =8,75662771182562E-06
u[2,5] =2,29955844180099

u[3,0] =2,10553080996468
u[3,1] =2,26539317651639
u[3,2] =0
u[3,3] =-1,77635683940025E-15 ----it is negative! absurd!
u[3,4] =0
u[3,5] =10,6290760135189

u[4,0] =1,43431585730299
u[4,1] =0,0902387796409421
u[4,2] =2,37707362514721
u[4,3] =0,817962085097459
u[4,4] =7,888864
u[4,5] =2,39154565281139
Fitness 1=383,121099950505
u[0,0] =4,7550011217406
u[0,1] =0,241038878259402
u[0,2] =0,00395999999999999
u[0,3] =0
u[0,4] =0
u[0,5] =0

u[1,0] =3,0289
u[1,1] =0,5442669362372
u[1,2] =0,910992251562801
u[1,3] =0,366624599999999
u[1,4] =0,1492162122
u[1,5] =0

u[2,0] =0,190926198835202
u[2,1] =1,84422971053786
u[2,2] =3,5556601809682
u[2,3] =0,00214166337232413
u[2,4] =0,031324627313322
u[2,5] =14,3757176189731

u[3,0] =0
u[3,1] =5,192
u[3,2] =3,529387567469
u[3,3] =5,51189017097053
u[3,4] =0,0236308592012779
u[3,5] =0,743091402359193

u[4,0] =0,0251726794242
u[4,1] =0,178464474965537
u[4,2] =0
u[4,3] =2,11934356565715
u[4,4] =7,7958283012854
u[4,5] =4,88119097866772
-----------------------------------------------------------------------------------------------
Aug 31 '07 #1
10 1760
On Aug 31, 4:26 pm, Michele <3rr0r...@email.itwrote:
Please forgive me for the neverending code down here but I cannot find a
rational explanation of the output of this simple program (really!).
Soluzione class has a double[,] field to represent a matrix. A method
named init() fills this matrix with random numbers. The main constructor
requires an int param (fCosto) which is _not_used_ inside init(). Every
time I instantiate a Soluzione object the matrix is filled by init().
The incredible thing is that if I create more than 9 objects where fCosto
= 3, there is always at least one of them whose matrix contains absurd
values! This does _not_ happen for objects constructed passing 1,2 or 4!
It's very hard to know what's wrong without an explanation of what the
numbers are meant to mean, and why certain values are absurd. Better
than an explanation would be a shorter program which still
demonstrates the problem.

If you're only interested in the results of Init, I suggest that you
start off by removing the Fitness method and all the similarly
"uninteresting" pieces of your code.

Jon

Aug 31 '07 #2
Hello Michele,

The faulty line is the one which reads:

delta = Math.Min(prod[si] - sumProdIJ, req[dj] - sumReqIJ);

The problem is that sometimes sumReqIJ is very slightly greater than
req[dj], which results in a very small negative number. You can add a check
to round the delta value if it is found to be less than zero:

delta = Math.Min(prod[si] - sumProdIJ, req[dj] - sumReqIJ); // Referenced
row
if (delta < 0)
delta = Math.Round(delta);

Best Regards,
Stanimir Stoyanov
www.stoyanoff.info | www.aeroxp.org

Aug 31 '07 #3
In data 31 agosto 2007 alle ore 17:49:28, Stanimir Stoyanov
<ad***@nospam.stoyanoff.infoha scritto:
Hello Michele,

The faulty line is the one which reads:

delta = Math.Min(prod[si] - sumProdIJ, req[dj] - sumReqIJ);

The problem is that sometimes sumReqIJ is very slightly greater than
req[dj], which results in a very small negative number. You can add a
check to round the delta value if it is found to be less than zero:

delta = Math.Min(prod[si] - sumProdIJ, req[dj] - sumReqIJ); //
Referenced row
if (delta < 0)
delta = Math.Round(delta);

Best Regards,
Stanimir Stoyanov
www.stoyanoff.info | www.aeroxp.org
Oh yeah! Thank you very much for your patience! I have forgotten the
lessons of numerical analysis :(

One more question, it's a matter of style:

Which version of AllNegative a good programmer should use?

bool AllNegative(int[] a) {
bool negative = true;
for (int i = 0; i < a.Length && negative; i++)
negative = a[i]<0;
return negative;
}

bool AllNegative(int[] a) {
foreach (int x in a)
if (x>0) return false;
return true;
}

I think the first one is a bit too "compiler styled" :)

Have a nice day!

Michael
Aug 31 '07 #4
One more question, it's a matter of style:
Which version of AllNegative a good programmer should use?

bool AllNegative(int[] a) {
bool negative = true;
for (int i = 0; i < a.Length && negative; i++)
negative = a[i]<0;
return negative;
}

bool AllNegative(int[] a) {
foreach (int x in a)
if (x>0) return false;
return true;
}
I personally prefer the for-each approach. I believe it does not have any
noticeable performance impact and is more readable.

Best Regards,
Stanimir Stoyanov
www.stoyanoff.info | www.aeroxp.org

Aug 31 '07 #5
Stanimir Stoyanov <ad***@nospam.stoyanoff.infowrote:
One more question, it's a matter of style:
Which version of AllNegative a good programmer should use?

bool AllNegative(int[] a) {
bool negative = true;
for (int i = 0; i < a.Length && negative; i++)
negative = a[i]<0;
return negative;
}

bool AllNegative(int[] a) {
foreach (int x in a)
if (x>0) return false;
return true;
}

I personally prefer the for-each approach. I believe it does not have any
noticeable performance impact and is more readable.
Or in .NET 3.5:

bool AllNegative(int[] a)
{
return a.All(x =x < 0);
}

Note that there's a difference between the two routines above, however
- the first one will return false if there's a zero element, the second
won't.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Aug 31 '07 #6
Or in .NET 3.5:
>
bool AllNegative(int[] a)
{
return a.All(x =x < 0);
}
The lambda expession is indeed a more "handy" example but if I had to have
backward compatibility in mind I would use a for-each loop instead.
Note that there's a difference between the two routines above, however
- the first one will return false if there's a zero element, the second
won't.
Good point, I guess it is up to Michele to decide whether or not to exclude
zero.

Best Regards,
Stanimir Stoyanov
www.stoyanoff.info | www.aeroxp.org

Aug 31 '07 #7
Stanimir Stoyanov <ad***@nospam.stoyanoff.infowrote:
Or in .NET 3.5:

bool AllNegative(int[] a)
{
return a.All(x =x < 0);
}

The lambda expession is indeed a more "handy" example but if I had to have
backward compatibility in mind I would use a for-each loop instead.
Sure. I'm just in C# 3 land at the moment, and enjoying all these
examples where it makes life easier :)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Aug 31 '07 #8
"Michele" <3r******@email.itschrieb im Newsbeitrag
news:op.txxqf4mo30xyjd@notebook...
In data 31 agosto 2007 alle ore 17:49:28, Stanimir Stoyanov
One more question, it's a matter of style:
Which version of AllNegative a good programmer should use?

bool AllNegative(int[] a) {
bool negative = true;
for (int i = 0; i < a.Length && negative; i++)
negative = a[i]<0;
return negative;
}

bool AllNegative(int[] a) {
foreach (int x in a)
if (x>0) return false;
return true;
}
besides the difference between for and foreach the first example will always
test each element of the array while the second example only tests until it
finds the first non-negative.
This can have a huge performance impact, depending on the content and length
of the arrays searched.

This is independant of for/foreach since also a for loop can be exited with
return

Christof
Sep 3 '07 #9
On Sep 3, 1:55 pm, "Christof Nordiek" <c...@nospam.dewrote:

<snip>
besides the difference between for and foreach the first example will always
test each element of the array while the second example only tests until it
finds the first non-negative.
Nope - the first will quite when it finds the first non-negative
element too. Look at the for loop's condition:

i < a.Length && negative

Jon

Sep 3 '07 #10
besides the difference between for and foreach the first example will
always test each element of the array while the second example only tests
until it finds the first non-negative.
Actually, the "i < a.Length && negative" in the loop means it will exit the
loop as soon as it finds the first negative.

So, it is does come purely down to the difference between foreach() and
for() loops ;-)

Personally, I would use the foreach version, as it is easier to read.
I'm not sure which would be better for performance

HTH
--
Ged Moretta
Senior Software Engineer
AppSense Ltd
www.appsense.com

-----------------------------------------------------------------------
This signature isn't automatic. I have to type it manually every time.
"Christof Nordiek" <cn@nospam.dewrote in message
news:Oc**************@TK2MSFTNGP04.phx.gbl...
"Michele" <3r******@email.itschrieb im Newsbeitrag
news:op.txxqf4mo30xyjd@notebook...
In data 31 agosto 2007 alle ore 17:49:28, Stanimir Stoyanov
>One more question, it's a matter of style:
>Which version of AllNegative a good programmer should use?

bool AllNegative(int[] a) {
bool negative = true;
for (int i = 0; i < a.Length && negative; i++)
negative = a[i]<0;
return negative;
}

bool AllNegative(int[] a) {
foreach (int x in a)
if (x>0) return false;
return true;
}

besides the difference between for and foreach the first example will
always test each element of the array while the second example only tests
until it finds the first non-negative.
This can have a huge performance impact, depending on the content and
length of the arrays searched.

This is independant of for/foreach since also a for loop can be exited
with return

Christof
Sep 3 '07 #11

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

Similar topics

0
by: Frank Passek | last post by:
Dear all, I've encountered some strange behaviour with PHP (4.3.2) using the CLI-API. When I provide an option in the first line of my script like so: #!/usr/bin/php -c /path_to_my_ini_file and...
0
by: Phil | last post by:
Hi, I don't understand this strange behaviour: I compile this code : #include <Python.h> #include"Numeric/arrayobject.h" static PyObject *
2
by: Paul Drummond | last post by:
Hi all, I am developing software for Linux Redhat9 and I have noticed some very strange behaviour when throwing exceptions within a shared library. All our exceptions are derived from...
3
by: Bruno van Dooren | last post by:
Hi All, i have some (3) different weird pointer problems that have me stumped. i suspect that the compiler behavior is correct because gcc shows the same results. ...
3
by: Sebastian C. | last post by:
Hello everybody Since I upgraded my Office XP Professional to SP3 I got strange behaviour. Pieces of code which works for 3 years now are suddenly stop to work properly. I have Office XP...
6
by: Edd Dawson | last post by:
Hi. I have a strange problem involving the passing of command line arguments to a C program I'm writing. I tried posting this in comp.programming yesterday but someone kindly suggested that I'd...
31
by: DeltaOne | last post by:
#include<stdio.h> typedef struct test{ int i; int j; }test; main(){ test var; var.i=10; var.j=20;
4
by: Gotch | last post by:
Hi, I'm getting a very strange behaviour while running a project I've done.... Let's expose it: I've two projects. Both of them use a Form to do some Gui stuff. Other threads pack up messages...
8
by: Dox33 | last post by:
I ran into a very strange behaviour of raw_input(). I hope somebody can tell me how to fix this. (Or is this a problem in the python source?) I will explain the problem by using 3 examples....
20
by: Pilcrow | last post by:
This behavior seems very strange to me, but I imagine that someone will be able to 'explain' it in terms of the famous C standard. -------------------- code -----------------------------------...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.