Edge

August 29, 2005

Happiness

Filed under: Life

I read for newspaper from interview of some reality TV-series “star”.

She had a motto something like: “I want to live a happy life baah blaah blaah…”

I started to wonder the about the happy life and what is behind it. I came about with quick thought about happiness.

You should not try to live a happy life. I mean that if you all time just think I want to live a happy life and try to find happiness in everything you do – I bet that you are not going to have a happy life. In this way you are never going to bee satisfied what you are doing. You just all the time want more “happiness”.

So how do I find happiness? Hard to say, it much easier is to say where you can’t find it (or where I can’ find it).

Just that this post would not be so pessimistic here are few things that makes me happy:
- Laughter and pure joy from Arttu.
- Love and caring from Kirsi.
- Drinking beers talking bullshit with friends.
- Achieving goals that you have set your self.
- Learning some new things.

August 25, 2005

Weighted graph in SQL.

Filed under: Programming

Have you ever tried to store hierarchies in SQL. That’s easy but, have you ever tried to do some calculation in that hierarchy. That’s bit harder.

Let’s take example situation that I have some hierarchy and some cost that should be allocated in that hierarchy. Let assume that you have an HierarchyItem that could have another item many other HierarchyItems as child of it and many parents also. Then you could have some cost allocated to that hierarchy let’s say DirectCost. You share this according to weight between the HierarchyItem connection say ConnectionShare.

Here is the class diagram of this.
Class diagram

So HierarchyItem cost is sum of direct costs and cost of items child according to share ratios.

Example if have file server and it costs are 1000 euros. Application A file usage is 80GB and Application B file usage is 120GB:

Application A has costs from file server (80 / (80 + 120)) * 1000 euros = 400 euros
Application B has costs from file server (120 / (80 + 120)) * 1000 euros = 600 euros

In this easily solved with recursion as flows (Detailed code in C#):

public float GetCost(string costType)
{
	float retval = 0;
	
	//Calculate first all direct costs
	foreach(DirectCost directCost in DirectCosts)
	{
		if(directCost.CostType.Equals(costType))
			retval += directCost.Value;
	}
	//Add all child costs to this
	foreach(ConnectionShare child in ChildConnectionShares)
	{
		retval += child.GetChildCost(costType);
	}
	return retval;
}

Problem converting this to SQL is that you can’t really do recursion in SQL. So the solution in my case was to simulate the cost in time to time. Simulation is a good solution if you don’t data bee accurate all the time.

So if you think about how you would calculate this by hand. You just would start calculating this from the bottom. First calculate those items that do not have any child’s. After that you would calculate items which child’s has been calculated all ready. And the just iterate the process.

So SQL solution comes from the same idea. First calculate all those items that have no childs and after that calculate those items which child’s has been calculated. Or if I be more precise you can calculate those items where your child count is the same as calculated child count.

So if I would have an tables HierarchyItem, ConnectionShare, DirectCost and HierarchyItemCosts.

HierarchyItem you would all the hierarchy items.

ConnectionShare would include all connections between HierarchyItems

DirectCost Table would include all direct cost related to HierarchyItem.

HierarchyItemCosts would store all the simulated costs.

Let’s assume that we already have some data in there. And go step by step…

1. Find all those items that can be calculated - those items where your child count is the same as calculated child count.

SELECT 	Parent.ID
FROM 	HierarchyItem Parent LEFT JOIN ConnectionShare Child
			ON Parent.ID = Child.ParentID
		LEFT JOIN HierarchyItemCost CalulcatedItems
			ON Child.ChildID = CalulcatedItems.HierarchyItemID
GROUP BY Parent.ID
HAVING COUNT(Child.ChildID) = COUNT(CalulcatedItems.HierarchyItemID)

2. Now Select all the items that should be calculated

SELECT 	HI.ID
FROM	HierarchyItem HI
WHERE 	HI.ID IN (Query from the step 1)

3. Add all CostTypes to calculation

SELECT 	HI.ID,
	TmpCostType. CostType
FROM	HierarchyItem HI CROSS JOIN
	(SELECT DISTINCT CostType FROM DirectCost) TmpCostType
WHERE 	HI.ID IN (Query from the step 1)

4. Add the direct cost calculation

SELECT 	HI.ID,
	TmpCostType. CostType,
	(SELECT 	COALESCE(SUM(Value),0)
	 FROM 	DirectCost
	WHERE 	HierarchyItemID = HI.ID
		AND CostType = TmpCostType.CostType) 
FROM	HierarchyItem HI CROSS JOIN
	(SELECT DISTINCT CostType FROM DirectCost) TmpCostType
WHERE 	HI.ID IN (Query from the step 1)

5. Add the child costs to calculation and helpper view.


CREATE VIEW HierarchyItemTotalShare AS
SELECT 	HI.ID,COALESCE(SUM(Share),0)
FROM 	HierarchyItem HI LEFT JOIN ConnectionShare CS
		ON HI.ID = CS.ChildID
GROUP BY HI.ID 
	
SELECT COALESCE(SUM(Child.Value * (Relation.Share/TotalShare.TotalShare) ),0)
FROM 	HierarchyItemCost Child JOIN ConnectionShare Relation
			ON Child.HierarchyItemID = Relation.ChildID
		JOIN HierarchyItemTotalShare TotalShare
			ON Child.HierarchyItemID = TotalShare.ID
 WHERE	Relation.ParentID = HI.ID
		AND Child.CostType = TmpCostType.CostType
		AND TotalShare.TotalShare <> 0

6. The final insert statement that is run until all row’s have been instered.


INSERT INTO HierarchyItemCost (HierarchyItemID,CostType,Value)
SELECT 	HI.ID,
	TmpCostType.CostType,
	--Items cost is sum of direct cost +
	(SELECT COALESCE(SUM(Value),0) FROM DirectCost WHERE HierarchyItemID = HI.ID AND CostType = TmpCostType.CostType)
		+
	--Sum of all child cost acording to share
		(SELECT COALESCE(SUM(Child.Value * (Relation.Share/TotalShare.TotalShare) ),0)
		 FROM 	HierarchyItemCost Child JOIN ConnectionShare Relation
				ON Child.HierarchyItemID = Relation.ChildID
			JOIN HierarchyItemTotalShare TotalShare
				ON Child.HierarchyItemID = TotalShare.ID
		 WHERE	Relation.ParentID = HI.ID AND Child.CostType = TmpCostType.CostType AND TotalShare.TotalShare <> 0)
	
FROM	HierarchyItem HI CROSS JOIN (SELECT DISTINCT CostType FROM DirectCost) TmpCostType
WHERE 	HI.ID IN
	–All those HierarchyItems witch childs has been calculated
	(SELECT Parent.ID
	 FROM 	HierarchyItem Parent LEFT JOIN ConnectionShare Child
			ON Parent.ID = Child.ParentID
		LEFT JOIN HierarchyItemCost CalulcatedItems
			ON Child.ChildID = CalulcatedItems.HierarchyItemID
	 GROUP BY Parent.ID
	 HAVING COUNT(Child.ChildID) = COUNT(CalulcatedItems.HierarchyItemID))
	 –Only calculate every item once
	 AND HI.ID NOT IN (SELECT HierarchyItemID FROM HierarchyItemCost)

Voilá!

If you have SqlServer you can test the solution with this script.

August 23, 2005

Not enough time

Filed under: Life

Lately I have noticed that there are not enough hours for everything.

Example where you get time study?

Here is my normal week day:

7:00 - 7:15 Wake up, shower and breakfast
7:15 - 8:15 Drive to work (or some sports)
8:15 - 12:00 Work
12:00 -12:30 Lunch
17:00 – 18:00 Back to Home
18:00 - 20:00 Supper, playing with Arttu and Kirsi and
20:00 - 21:00 Trying to make Arttu sleep (Kirsi is doing this most of the time)
21:00 - 22:00 TV and reading

So the day is quite full. So only time to study is the tv-time. That is why I have decided stop watching TV. It is so rare that something good comes from TV. Occasionally TV is nice way to relax, but most of the time it just makes you phlegmatic. But let’s see how long this decision is going to last…

August 21, 2005

Poem

Filed under: Life

Sun rises,

golden sparkling dew on a leaf.

I fart and lift my zipper.

August 18, 2005

Mobile phones

Filed under: Life

Sometimes people call me and ask: “Are you at good place to talk”. I thing this question is unnesesary, because if I’m at place that I’m not able to talk I will not answer the phone. Sometimes I did also ask this question. I was thinking that question was good behaviour, because I was bothering some one with my phoning. Now I’m thinking that this question just waists time…

BTW Would be nice to have software in your mobile that when I’m example at movie I could put movie profile on. If call comes then phone would just ingnore it and send SMS that I’m at movie…

II hate when people talk through speakers and you are not sure if you are just speaking to the person you wanted or if there are others around. So this reason I have decided following rules:

IIf answer phone and I’m using the speaker I say something like following: “Hey, can you hear me because I’m using the speaker”. This way people now that I’m using speaker and someone might be other than me might be hearing what are we talking about.

IIf there is people around I say something like “Hey, I’m here with XXX and can you hear us” or something.

So basic idea is that you should somehow inform your caller that you are using the speaker…

August 17, 2005

Hungry

Filed under: Life

After my summer holidays I decided to loose some weight, that I have gain when enjoying sun, beer and grilled food. And 100 euro bet with my brother is helping a bit.

So now I’m in two week diet and exercise camp. One thing that I can now say, that being hungry is not a good feeling. All the food smells so good…

Now it is my third day and I’m already lost some weight. Mostly water I think.

Day Food Exercise Weight
Meat week
Day1 Morning: Black coffee
Lunch: 250g chicken, salad and tomatoes
Supper: An egg, tomatoes
During the day: 3 liters of low salt mineral water
30 min walk with my son 84 kg
Day2 Morning: Black coffee and one egg
Lunch: 250g chicken, salad and tomatoes
Supper: An egg, tomatoes
During the day: 3 liters of low salt mineral water
1:45 roller-skaiting to office. 82 kg
Day3 Morning: Black coffee
Lunch: 250g chicken, salad and tomatoes
Supper: 250 g chicken, tomatoe and banana
During the day: 3 liters of low salt mineral water
1:45 roller-skaiting to office. 80,5 kg
Day4 Morning: Black coffee
Lunch: 2dl jogurth
Supper: 250 g chicken, tomatoe and banana
During the day: 3 liters of low salt mineral water
1 hour of badminton 80 kg
Day5 Morning: Black coffee
Lunch: Ann Egg, 2dl Cottage chees and 2 carrots
Supper: Spinach soup
During the day: 3 liters of low salt mineral water
1km of swimming
1 hour walk
80 kg (No weight loss.)
Easy two days
Day6 Morning: Black coffee and ryed bread with two slcie of cheese
Lunch:Chicken, feata peach sallad. Coffee and blueberry pie
Supper:Tee, two cheese sandwitch, 3 slice of blueberry pie. half a bottle of sparkling winw. Half bottel of beer
45 min walk
45 min run
No weighting.
Day7 Morning: Beacon and eggs, pancake, jougurth, Coffe, Croisant and fresh orange juice.
Lunch:mushroom soup and bread
Supper:Too much of everything!
45 min walk No weighting.
Soup week
Day8 Morning: Coffee
Lunch: -
Supper: Vegetable soup, Nachos
45 min walk No weighting.
Day9 Morning: Coffee
Lunch:-
Supper: Vegetable Soup, Bread, Nachos
1 hour badmintons
1:30 indoor climbing
No weighting.
Day10 Morning: Coffee
Lunch: Vegetable Soup, Motzarella bread
Supper: Vegetable Soup, Bread, Nachos
20 min running at gym
20 min rowing at gym
10 min cycling at gym
81 kg (That easy two days!)
Day11 Morning: Coffee
Lunch: Big chicken sallad
Supper: Vegetable Soup, Bread, Apples
None 81 kg
Day12 Morning: Coffee, Porridge, Bread
Lunch: Sushi, Tee
Supper: Vegetable Soup
1 km of swinmming
Day13 Morning: Coffee
Lunch: Vegetable Soup
Supper: Pizza, Beer and some candy
Evening: More pizza and few drinks
None 81 kg
I was the winner in the dieting competition with by brotehr!

August 16, 2005

Spending your time

Filed under: Life

What I truly hate is wasting your time those things that are useless, but things that you have to do. (Makes me wonder is this bloggin useless.)

Example trips between home and work Is it really necessary to spend 1/8 of tour awaking time sitting in a car? Some times it feels that example farmers are quite lucky that they don’t have to travel to work. They can just open the door and start basically straight a way those things that are leading something useful.

Last spring I was quite lucky that one of customers where only 100 meters from home. I have to admit 100 meters is maybe too short distance. Optimally distance would be something like 2-3 km. That way you could have nice little exercise before an after work. That way you could more easily leave your work behind.

Other thing that I really hate is installing software. Currently I’m installing testing environment for BizTalk Server 2004. I stated to think what would be most painless way to do this and other future installations.

The things steps that I decided to do were:

1. Use Virtual PC:
- So that you could have multiple separate environments that are not trashing each other.
- Make some base environments that I can use for future installations.
- For future bigger projects testing a possibility share common development environments.
2. Buy fast external hardware.
- For virtual PC efficiency
- For travel with my Virtual PC:s

I will come back to this later to tell how I succeeded.

August 15, 2005

So it begins…

Filed under: Life

I have been planning for starting this blog for some time now and now it is finally the time to make the first step (Better late than ever or maybe never…). We will see how long this going to be alive. Maybe one week may, maybe longer… Or maybe this will be stored to google and that way making me immortal.

Here is picture of me as I’m now. And time to time I’ll try to post some pictures what the life have done to me. Face
And picture of my wife Kirsi and son Arttu. Kirsi and Arttu





















Get free blog up and running in minutes with Blogsome | Theme designs available here