New/Different Bankrupcy model(s)

Forum for technical discussions regarding development. If you have a general suggestion, problem or comment, please use one of the other forums.

Moderator: OpenTTD Developers

ZxBiohazardZx
Tycoon
Tycoon
Posts: 1534
Joined: 14 Mar 2006 12:46
Location: Netherlands

New/Different Bankrupcy model(s)

Post by ZxBiohazardZx »

just for the clean-status of the thread here are all changes

Part 1: Change the bankruptcy check
0001-Change-condition-for-Bankruptcy.patch
(785 Bytes) Downloaded 112 times
Part 2: Charge interest on negative balance Part 3: Make the check monthly
0003-Make-bankruptcy-check-monthly.patch
(5.06 KiB) Downloaded 96 times
i do agree that this is a somewhat simple implementation of bankrupcy and i propose the following optional changes
all examples will use a 100.000 GBP limit and a 80.000 GBP loan active unless stated differently

suggested bankrupcy terms:
1) you can only go bankrupt if your deficit exeeds the loan-limit
quite simply put you will bankrupt if you in absolute sense have no money and cannot loan more

in the example:

your marked for bankrupcy that term IF:

Code: Select all

CurrentMoney + (abs(MaxLoan)-abs(CurrentLoan))  < 0 

Code: Select all

// lets assume you have -10.000 GBP then 
-10.000 + (100.000-80.000) > 0
-10.000 + (20.000) = 10.000 > 0 --> not-bankrupt
// lets assume you have -25.000 GBP then 
-25.000 + (100.000-80.000) > 0
-25.000 + (20.000) = -5.000 < 0 --> flag-bankrupt
2) you will go bankrupt faster when you have a higher loan then when you have a lower loan

by comparing your deficit relatively to your loan we can form a multiplier to make you go bankrupt faster on high loan then on low loan
it also gives an extra variable that users can set and manipulate to make the game easier/harder (maybe make it economy based?)

bankrupt if:

Code: Select all

(CurrentMoney * (Loan/MaxLoan))+Treshhold Value < 0 

Code: Select all

// lets assume same -10.000 GBP and a treshhold of 5.000 
-10.000*(80/100)+5.000 < 0 -> flag_bankrupt
// lets assume -10.000 GBP and a treshhold of 5.000, but this time we only have a -10.000 Loan
-10.000*(10/100)+5.000 > 0 -> not_bankrupt
3) you will go bankrupt as soon as your interest exeeds your income
a more interesting option, not sure if its usefull, but you could compare your interest on loan to your actual income. as long as you can pay your interest from your income you should be fine, not sure how much interest roughly is and how much income might have to be scaled to make it a " fair" rule
Last edited by ZxBiohazardZx on 20 Oct 2012 08:08, edited 2 times in total.
bjgttd
Engineer
Engineer
Posts: 86
Joined: 13 Aug 2012 22:04

Re: New/Different Bankrupcy model(s)

Post by bjgttd »

ZxBiohazardZx wrote: 1) you can only go bankrupt if your deficit exeeds the loan-limit
So just be careful and borrow the money on time. Or borrow to the maximum and pay the maximum interest. There is an "overdraft protection" in the real life, but it always costs you.
ZxBiohazardZx wrote: 2) you will go bankrupt faster when you have a higher loan then when you have a lower loan
Yes you will because you are paying bigger interest on bigger loan.
ZxBiohazardZx wrote: 3) you will go bankrupt as soon as your interest exeeds your income
You will because the interest will eat your funds (inflation could be used to borrow more and stay barely "alive", but that's not fun).
Kogut
Tycoon
Tycoon
Posts: 2493
Joined: 26 Aug 2009 06:33
Location: Poland

Re: New/Different Bankrupcy model(s)

Post by Kogut »

bjgttd wrote:
ZxBiohazardZx wrote: 1) you can only go bankrupt if your deficit exeeds the loan-limit
So just be careful and borrow the money on time. Or borrow to the maximum and pay the maximum interest. There is an "overdraft protection" in the real life, but it always costs you.
It is quite irritating in developing AI, btw it is on flyspray since 2010 - http://bugs.openttd.org/task/4218 .
Correct me If I am wrong - PM me if my English is bad
AIAI - AI for OpenTTD
ZxBiohazardZx
Tycoon
Tycoon
Posts: 1534
Joined: 14 Mar 2006 12:46
Location: Netherlands

Re: New/Different Bankrupcy model(s)

Post by ZxBiohazardZx »

its not 3 issues its 3 possible solutions to the horrible model we currently have, by making the bankrupcy chance depend on absolute deficit or make it scale with loan etc, maybe combine the two, just putting up suggestions for a possible fix/change
ZxBiohazardZx
Tycoon
Tycoon
Posts: 1534
Joined: 14 Mar 2006 12:46
Location: Netherlands

Re: New/Different Bankrupcy model(s)

Post by ZxBiohazardZx »

Current Bankruptcy Code:

Code: Select all

/**
 * Check for bankruptcy of a company. Called every three months.
 * @param c Company to check.
 */
static void CompanyCheckBankrupt(Company *c)
{
	/*  If the company has money again, it does not go bankrupt */
	if (c->money >= 0) {
		c->quarters_of_bankruptcy = 0;
		c->bankrupt_asked = 0;
		return;
	}

	c->quarters_of_bankruptcy++;

	switch (c->quarters_of_bankruptcy) {
		case 0:
		case 1:
			break;

		case 2: {
			CompanyNewsInformation *cni = MallocT<CompanyNewsInformation>(1);
			cni->FillData(c);
			SetDParam(0, STR_NEWS_COMPANY_IN_TROUBLE_TITLE);
			SetDParam(1, STR_NEWS_COMPANY_IN_TROUBLE_DESCRIPTION);
			SetDParamStr(2, cni->company_name);
			AddCompanyNewsItem(STR_MESSAGE_NEWS_FORMAT, cni);
			AI::BroadcastNewEvent(new ScriptEventCompanyInTrouble(c->index));
			Game::NewEvent(new ScriptEventCompanyInTrouble(c->index));
			break;
		}

		case 3: {
			/* Check if the company has any value.. if not, declare it bankrupt
			 *  right now */
			Money val = CalculateCompanyValue(c, false);
			if (val > 0) {
				c->bankrupt_value = val;
				c->bankrupt_asked = 1 << c->index; // Don't ask the owner
				c->bankrupt_timeout = 0;
				break;
			}
			/* FALL THROUGH to case 4... */
		}
		default:
		case 4:
			if (!_networking && _local_company == c->index) {
				/* If we are in offline mode, leave the company playing. Eg. there
				 * is no THE-END, otherwise mark the client as spectator to make sure
				 * he/she is no long in control of this company. However... when you
				 * join another company (cheat) the "unowned" company can bankrupt. */
				c->bankrupt_asked = MAX_UVALUE(CompanyMask);
				break;
			}

			/* Actually remove the company, but not when we're a network client.
			 * In case of network clients we will be getting a command from the
			 * server. It is done in this way as we are called from the
			 * StateGameLoop which can't change the current company, and thus
			 * updating the local company triggers an assert later on. In the
			 * case of a network game the command will be processed at a time
			 * that changing the current company is okay. In case of single
			 * player we are sure (the above check) that we are not the local
			 * company and thus we won't be moved. */
			if (!_networking || _network_server) DoCommandP(0, 2 | (c->index << 16), CRR_BANKRUPT, CMD_COMPANY_CTRL);
			break;
	}
}

but in short the real check is done here:

Code: Select all

static void CompanyCheckBankrupt(Company *c)
{
	/*  If the company has money again, it does not go bankrupt */
	if (c->money >= 0) {
		c->quarters_of_bankruptcy = 0;
		c->bankrupt_asked = 0;
		return;
	}

	c->quarters_of_bankruptcy++;
all i have to figure it out is how to get max_loan value and "current loan value" and "current company money"

then implementing 1 and 2 shouldnt be hard, though an extra parameter in 2 makes it "more difficult"
damerell
Traffic Manager
Traffic Manager
Posts: 190
Joined: 22 Feb 2008 23:09

Re: New/Different Bankrupcy model(s)

Post by damerell »

ZxBiohazardZx wrote:1) you can only go bankrupt if your deficit exeeds the loan-limit
quite simply put you will bankrupt if you in absolute sense have no money and cannot loan more
I agree completely. This merely fixes an interface annoyance. Obviously it's necessary that interest is paid on negative cash balances (I don't know if it is at present).
2) you will go bankrupt faster when you have a higher loan then when you have a lower loan
As discussed on IRC, I feel this overly complicates matters and makes bankruptcy unpredicatable.
3) you will go bankrupt as soon as your interest exeeds your income
a more interesting option, not sure if its usefull, but you could compare your interest on loan to your actual income. as long as you can pay your interest from your income you should be fine, not sure how much interest roughly is and how much income might have to be scaled to make it a " fair" rule
I like this in combination with #1. It can be kept simple. At present, you go bankrupt if you have negative cash 3 quarters running. With #1, you go bankrupt if you have negative (cash + available loan) 3 quarters running. With #1 and #3, you go bankrupt if you have negative (cash + available loan) 3 quarters running _and_ that figure has decreased from quarter to quarter.

I think this makes sense. The bank winds you up not just because you owe them money, but because they see no prospect of you paying it back. As long as you're paying it back, they're happy.
User avatar
Hyronymus
Tycoon
Tycoon
Posts: 13233
Joined: 03 Dec 2002 10:36
Location: The Netherlands
Contact:

Re: New/Different Bankrupcy model(s)

Post by Hyronymus »

I don't think 2) needs to be coded: it is a logical effect of 3) if you implement 3) well: higher loans come with higher interest over these loans and will bring you down quicker then if you had less loans and lower interest.
ZxBiohazardZx
Tycoon
Tycoon
Posts: 1534
Joined: 14 Mar 2006 12:46
Location: Netherlands

Re: New/Different Bankrupcy model(s)

Post by ZxBiohazardZx »

yeah small chat in IRC left only #1 as reasonable option

so the question is do we want it or is current implementation fine? or do we go for an option #4: allow a small negative (1-loan-step) but not more (aka max 10K GBP negative)
User avatar
planetmaker
OpenTTD Developer
OpenTTD Developer
Posts: 9432
Joined: 07 Nov 2007 22:44
Location: Sol d

Re: New/Different Bankrupcy model(s)

Post by planetmaker »

Later we've had a small chat in #openttd.dev (the patch discussion channel).

What we can talk about and what I could sign off, if well coded, is a patch which is about your choice #1, but actually does two things:
- only declare bancrupcy when you cannot take loan anymore and your bank balance is negative on the beginning of the quarters (i.e. check at the same time as now, but bank balance - current_loan > -max_loan-1)
- you pay tripple interests on a negative bank balance, thus to collect interests on a negative cash value in the monthly loop additionally to the interest collected on the loan in the quarterly loop. The reason herein lies that this avoids the cheat to not pay interests on negative money and not go bancrupt either.

Or, possibly better solution:
Do the same check for the account balance as above (money - loan > -max_loan-1). Increase a counter each day the condition is not fulfilled and reset it when it is fulfilled. Declare bancruptcy when the condition triggers true 9 months in a row.

See logs here.
ZxBiohazardZx
Tycoon
Tycoon
Posts: 1534
Joined: 14 Mar 2006 12:46
Location: Netherlands

Re: New/Different Bankrupcy model(s)

Post by ZxBiohazardZx »

thanks planet ill try to work with that when i get home :)

part1: change the check to money - loan >= max_loan:

(patch/diff)
bankrupt_part1.patch
(443 Bytes) Downloaded 107 times
Eddi
Tycoon
Tycoon
Posts: 8269
Joined: 17 Jan 2007 00:14

Re: New/Different Bankrupcy model(s)

Post by Eddi »

what i found annoying with the bankrupcy code, is that it checks the bank balance after all monthly maintenance costs, so even if you were (hardly) positive during the whole quarter, you are almost guaranteed to get into negative and are marked as bankrupcy candidate as soon as the month changes.

so my suggestion would be to move the bankrupcy check to the very beginning of the tick, so effectively you are marked as bankrupt if you were negative at the end of the previous quarter, not at the beginning of this quarter
ZxBiohazardZx
Tycoon
Tycoon
Posts: 1534
Joined: 14 Mar 2006 12:46
Location: Netherlands

Re: New/Different Bankrupcy model(s)

Post by ZxBiohazardZx »

should be checked like that logically as well, your checked on how you performed, not on current status
ZxBiohazardZx
Tycoon
Tycoon
Posts: 1534
Joined: 14 Mar 2006 12:46
Location: Netherlands

Re: New/Different Bankrupcy model(s)

Post by ZxBiohazardZx »

part2: charge companies interest for having negative balance
Attachments
bankrupt_part2.patch
(976 Bytes) Downloaded 101 times
ZxBiohazardZx
Tycoon
Tycoon
Posts: 1534
Joined: 14 Mar 2006 12:46
Location: Netherlands

Re: New/Different Bankrupcy model(s)

Post by ZxBiohazardZx »

part 3: make the check monthly and adjust calculations
bankrupt_part3.patch
(2.21 KiB) Downloaded 96 times
part4: seems to be all inclusive
bankrupt_part4.patch
(4.75 KiB) Downloaded 114 times
Aphid
Traffic Manager
Traffic Manager
Posts: 168
Joined: 16 Dec 2011 17:08

Re: New/Different Bankrupcy model(s)

Post by Aphid »

Another model could be:

If (company value + W <= 0) --> Bankruptcy in X months.

Company value is basically the amount of cash you would have after selling off all assets, less loan. There should be a window of time (X) for a startup company to make money. W here is a certain amount of cash the bank is willing to risk regardless. X could be made smaller for companies that have existed for a longer period of time.

A more generous (and complex) model would be:

If(company value + I * 70 / R * a + W <= 0) --> Bankruptcy in X months.
Where a = 4900 / R^2 + 70 / (RN).

Where R is the interest rate, N is the expected loan duration, and I the yearly income. Suggested values for N are around the 20-40 range. Basically, 70/R is approximately the return rate, or the value of your income seen as an asset. Thus, a profitable company could use its income statement to support their continued existence. The income used should probably be on a yearly basis. To keep the game transparent to the player, the newspaper should inform a company that gets a bankruptcy warning of how much extra income // money they would need to avoid this.

Of course, having a company value below 0 pretty much means you have to be in the red, so the warnings would stay roughly the same.

To be honest this model sounds better to me. After all, in a lot of cases the bankruptcy of a company with a decent value could have been avoided by selling assets (the company would continue to exist). It seems fair though that being in the red in this case should cost you a higher interest rate as suggested in the IRC discussion (monthly instead of quarterly, which means a 15% annual rate would be a 45% annual rate for being in the red, which wouldn't be unheard of).

Might I add that 4% interest rates as a default maximum might be a bit generous? It's quite easy to make double-digit returns, or even triple digits in this game. Real life interest rates tend to be around the 10% mark as well, especially for startups. At the very least they're above the stock-market average yield. I'd suggest increasing the maximum. (as well as the defaults)
ZxBiohazardZx
Tycoon
Tycoon
Posts: 1534
Joined: 14 Mar 2006 12:46
Location: Netherlands

Re: New/Different Bankrupcy model(s)

Post by ZxBiohazardZx »

just for the clean-status of the thread here are all changes

Part 1: Change the bankruptcy check
0001-Change-condition-for-Bankruptcy.patch
(785 Bytes) Downloaded 102 times
Part 2: Charge interest on negative balance Part 3: Make the check monthly
0003-make-bankruptcy-check-monthly.patch
(7.38 KiB) Downloaded 100 times
User avatar
planetmaker
OpenTTD Developer
OpenTTD Developer
Posts: 9432
Joined: 07 Nov 2007 22:44
Location: Sol d

Re: New/Different Bankrupcy model(s)

Post by planetmaker »

You may want to checkout r24617, r24618 and r24619 :-) but also possibly the logs of the last review session.
ZxBiohazardZx
Tycoon
Tycoon
Posts: 1534
Joined: 14 Mar 2006 12:46
Location: Netherlands

Re: New/Different Bankrupcy model(s)

Post by ZxBiohazardZx »

haha added to trunk, thanks guys, glad i could contribute and ofcourse thanks for fixing some codestyle etc, im not that familiar with everything yet :)

will try to do better on my next patch
User avatar
pavel1269
Route Supervisor
Route Supervisor
Posts: 473
Joined: 03 Dec 2006 13:22
Location: Czech Republic
Contact:

Re: New/Different Bankrupcy model(s)

Post by pavel1269 »

Nice. And it was faster than usual.
Kogut
Tycoon
Tycoon
Posts: 2493
Joined: 26 Aug 2009 06:33
Location: Poland

Re: New/Different Bankrupcy model(s)

Post by Kogut »

I am unsure about

Code: Select all

     3.7 +
     3.8 +		/* We have to convert the quarters of bankruptcy into months of bankruptcy */
     3.9 +		FOR_ALL_COMPANIES(c) {
    3.10 +			c->months_of_bankruptcy = 3 * c->months_of_bankruptcy;
    3.11 +		}
    3.12  	}
It seems that company that was in bankruptcy may receive 1 or 2 additional months (as month and two after quarter check quarters_of_bankruptcy would not be increased). AFAIK this code should look like

Code: Select all

     3.7 +
     3.8 +		/* We have to convert the quarters of bankruptcy into months of bankruptcy */
     3.9 +		FOR_ALL_COMPANIES(c) {
    3.10 +			c->months_of_bankruptcy = 3 * c->months_of_bankruptcy;
    3.11 +			if( c->months_of_bankruptcy > 0 ){
    3.11 +				if(MONTH is directly after quarter check) c->months_of_bankruptcy = c->months_of_bankruptcy + 1;
    3.11 +				if(MONTH is directly before quarter check) c->months_of_bankruptcy = c->months_of_bankruptcy + 2;
    3.11 +			}
    3.11 +		}
    3.12  	}
Sorry for low quality pseudocode.
Correct me If I am wrong - PM me if my English is bad
AIAI - AI for OpenTTD
Post Reply

Return to “OpenTTD Development”

Who is online

Users browsing this forum: No registered users and 48 guests