Exemple #1
0
def rankPop(chromos):
    errors, results = [], []
    # print chromos
    for chromo in chromos:
        selectedKeys = []
        for i in range(len(chromo)):
            if chromo[i] == 1:
                selectedKeys.append(keys[i])
        selectedKeys.append('"PRICE"')
        scores = myownq.runInnerLoop(selectedKeys, myownq.qAgent())
        if scores[0] <= 0:
            errors.append(1)
        else:
            errors.append(scores[0])
        results.append(scores)
        # print scores
        # print errors

    fitnessScores = calcFitness(errors)  # calc fitness scores from the erros calculated
    pairedPop = zip(
        chromos, errors, results, fitnessScores
    )  # pair each chromo with its protein, ouput and fitness score
    rankedPop = sorted(
        pairedPop, key=operator.itemgetter(-1), reverse=True
    )  # sort the paired pop by ascending fitness score
    return rankedPop
Exemple #2
0
def runStochasticDescent():
	gradientEpsilon = 0.01
	initialized = False
	#no price in these keys
	keys = ['"ACCOCI"', '"ASSETS"', '"ASSETSC"', '"ASSETSNC"', '"BVPS"', '"CAPEX"', '"CASHNEQ"', '"COR"', '"CURRENTRATIO"', '"DE"', '"DEBT"', '"DEPAMOR"', '"DILUTIONRATIO"', '"DPS"', '"EBIT"', '"EBITDA"', '"EBT"', '"EPS"', '"EPSDIL"', '"EQUITY"', '"FCF"', '"FCFPS"', '"GP"', '"INTANGIBLES"', '"INTEXP"', '"INVENTORY"', '"LIABILITIES"', '"LIABILITIESC"', '"LIABILITIESNC"', '"NCF"', '"NCFCOMMON"', '"NCFDEBT"', '"NCFDIV"', '"NCFF"', '"NCFI"', '"NCFO"', '"NCFX"', '"NETINC"', '"NETINCCMN"', '"NETINCDIS"', '"PAYABLES"', '"PB"', '"PREFDIVIS"', '"RECEIVABLES"', '"RETEARN"', '"REVENUE"', '"RND"', '"SGNA"', '"SHARESWA"', '"SHARESWADIL"', '"TANGIBLES"', '"TAXEXP"', '"TBVPS"', '"WORKINGCAPITAL"']
	selectedKeys = random.sample(keys, 10)
	selectedKeys += ['"PRICE"']
	remainingKeys = [key for key in keys if key not in selectedKeys]
	hashVal = hash(tuple(selectedKeys))
	fScoreCorrect = dict()
	fScoreRewards = dict()
	previousState = dict()
	visited = []
	maxState = dict()

	for x in range(1,100000):
		#random restarts
		if (x%10000 == 0):
			selectedKeys = random.sample(keys, 10)
			selectedKeys += ['"PRICE"']
			remainingKeys = [key for key in keys if key not in selectedKeys]
			hashVal = hash(tuple(selectedKeys))
			previousState = dict()
			print ("RESTART")

		result = myownq.runInnerLoop(selectedKeys)
		fScoreRewards[hashVal] = result[0]
		fScoreCorrect[hashVal] = result[1]
		agent = result[2]
		visited.append(hashVal)
		move = False
		if (len(previousState.keys()) == 0):
			move = True
		elif (fScoreRewards[previousState["hash"]] < fScoreRewards[hashVal]):
			move = True
		else:
			move = util.flipCoin(gradientEpsilon)
		if (move):
			previousState["hash"] = hashVal
			previousState["selected"] = selectedKeys
			previousState["remaining"] = remainingKeys
			print (fScoreRewards[hashVal])
			print (fScoreCorrect[hashVal])
			if (len(maxState.keys()) == 0 or fScoreRewards[hashVal] > maxState["score"]):
				maxState["hash"] = hashVal
				maxState["selected"] = selectedKeys
				maxState["remaining"] = remainingKeys
				maxState["score"] = fScoreRewards[hashVal]
				maxState["correct"] = fScoreCorrect[hashVal]
				maxState["agent"] = agent

		repeatedState = True
		numRepeatedStates = 0
		STOPIT = False
		while (repeatedState):
			numRepeatedStates += 1
			hashVal = previousState["hash"]
			selectedKeys = previousState["selected"]
			remainingKeys = previousState["remaining"]
			#either randomly remove a fundamental, or add one
			#don't want too few fundamentals, so only remove if there are more than 4
			#also don't want too many, so limit it at like 45
			if ((len(selectedKeys) > 4 and util.flipCoin(0.5)) or len(selectedKeys) > 45):
				randomKey = random.sample(selectedKeys, 1)[0]
				while (randomKey == '"PRICE"'):
					randomKey = random.sample(selectedKeys, 1)[0]
				selectedKeys.remove(randomKey)
				remainingKeys.append(randomKey)
			else:
				randomKey = random.sample(remainingKeys, 1)[0]
				selectedKeys.append(randomKey)
				remainingKeys.remove(randomKey)

			hashVal = hash(tuple(selectedKeys))
			if (hashVal not in visited):
				repeatedState = False
			if (numRepeatedStates >=1000):
				STOPIT = True
				break
		if (STOPIT):
			break
	print ("FINAL SOLUTION")
	print (maxState)
Exemple #3
0
def runStochasticDescent():
	initialized = False
	#randomly select 10 fundamentals to use
	selectedKeys = random.sample(keys, 10)
	#add in price
	selectedKeys += ['"PRICE"']
	remainingKeys = [key for key in keys if key not in selectedKeys]
	hashVal = hash(tuple(selectedKeys))
	#store the scores for this set of fundamentals
	fScoreCorrect = dict()
	fScoreRewards = dict()
	previousState = dict()
	visited = []
	maxState = dict()

	#loop through 100,000 iterations
	for x in range(1,100000):
		#random restarts
		if (x%10000 == 0):
			selectedKeys = random.sample(keys, 10)
			selectedKeys += ['"PRICE"']
			remainingKeys = [key for key in keys if key not in selectedKeys]
			hashVal = hash(tuple(selectedKeys))
			previousState = dict()
			#print ("RESTART")

		#get the score for this set of fundamentals by running the inner loop
		result = myownq.runInnerLoop(selectedKeys)
		fScoreRewards[hashVal] = result[0]
		fScoreCorrect[hashVal] = result[1]
		#save the inner loop agent that ran the inner q-learning for this set
		agent = result[2]
		visited.append(hashVal)
		move = False
		#move if its the first step
		if (len(previousState.keys()) == 0):
			move = True
		#move if the new state has a better score
		elif (fScoreRewards[previousState["hash"]] < fScoreRewards[hashVal]):
			move = True
		#move randomly with a chance equal to gradientEpsilon
		else:
			move = util.flipCoin(gradientEpsilon)
		if (move):
			#update values for previous state
			previousState["hash"] = hashVal
			previousState["selected"] = selectedKeys
			previousState["remaining"] = remainingKeys
			#print (fScoreRewards[hashVal])
			#print (fScoreCorrect[hashVal])

			#update max state if necessary
			if (len(maxState.keys()) == 0 or fScoreRewards[hashVal] > maxState["score"]):
				maxState["hash"] = hashVal
				maxState["selected"] = selectedKeys
				maxState["remaining"] = remainingKeys
				maxState["score"] = fScoreRewards[hashVal]
				maxState["correct"] = fScoreCorrect[hashVal]
				maxState["agent"] = agent

		#avoid repeated states
		repeatedState = True
		numRepeatedStates = 0
		while (repeatedState):
			numRepeatedStates += 1
			hashVal = previousState["hash"]
			selectedKeys = previousState["selected"]
			remainingKeys = previousState["remaining"]
			#either randomly remove a fundamental, or add one
			#don't want too few fundamentals, so only remove if there are more than 4
			#also don't want too many, so limit it at around 45
			if ((len(selectedKeys) > 4 and util.flipCoin(0.5)) or len(selectedKeys) > 45):
				randomKey = random.sample(selectedKeys, 1)[0]
				while (randomKey == '"PRICE"'):
					randomKey = random.sample(selectedKeys, 1)[0]
				selectedKeys.remove(randomKey)
				remainingKeys.append(randomKey)
			else:
				randomKey = random.sample(remainingKeys, 1)[0]
				selectedKeys.append(randomKey)
				remainingKeys.remove(randomKey)

			hashVal = hash(tuple(selectedKeys))
			if (hashVal not in visited):
				repeatedState = False
			#we are probably stuck in an infinite loop if this occurs, so we need to randomly sample
			if (numRepeatedStates >=1000):
				selectedKeys = random.sample(keys, 10)
				selectedKeys += ['"PRICE"']
				remainingKeys = [key for key in keys if key not in selectedKeys]
				hashVal = hash(tuple(selectedKeys))
				previousState = dict()

	#print ("FINAL SOLUTION")
	print (maxState)
Exemple #4
0
def runSimulatedAnnealing():
	initialized = False
	#no price in these keys
	selectedKeys = random.sample(keys, 10)
	selectedKeys += ['"PRICE"']
	remainingKeys = [key for key in keys if key not in selectedKeys]
	hashVal = hash(tuple(selectedKeys))
	fScoreCorrect = dict()
	fScoreRewards = dict()
	previousState = dict()
	visited = []
	maxState = dict()
	maxAgent = None
	testing = False

	#run the iterations
	for x in range(1,100000):
		# if (x % 100 == 0):
		# 	print x
		# if (x < 10):
		result = myownq.runInnerLoop(selectedKeys, myownq.qAgent())
		# else:
		# 	if (not testing):
		# 		#print maxState
		# 		testing = True
		# 	maxAgent = maxState["agent"]
		# 	selectedKeys = maxState["selected"]
		# 	result = myownq.runTestLoop(selectedKeys, maxAgent)
		# 	#print "RESULT"
		# 	#print result
		# 	#print maxState

		fScoreRewards[hashVal] = result[0]
		fScoreCorrect[hashVal] = result[1]
		agent = result[2]
		visited.append(hashVal)

		move = False
		#move if we are on the first step
		if (len(previousState.keys()) == 0):
			move = True
		else:
			delta = fScoreRewards[hashVal] - fScoreRewards[previousState["hash"]]
			#move if we move to a better state
			if (delta >= 0):
				move = True
			#move with a probablity defined by the anneal schedule function
			else:
				move = random.random() <= annealSchedule(delta, x)

		if (len(previousState.keys()) == 0 or move):
			previousState["hash"] = hashVal
			previousState["selected"] = selectedKeys
			previousState["remaining"] = remainingKeys
			# print (fScoreRewards[hashVal])
			# print (fScoreCorrect[hashVal])
			# print x
			if (len(maxState.keys()) == 0 or fScoreRewards[hashVal] > maxState["score"]):
				maxState["hash"] = hashVal
				maxState["selected"] = selectedKeys
				maxState["remaining"] = remainingKeys
				maxState["score"] = fScoreRewards[hashVal]
				maxState["correct"] = fScoreCorrect[hashVal]
				maxState["agent"] = agent

		repeatedState= True
		numRepeatedStates = 0
		#avoid repeating states
		while (repeatedState):
			numRepeatedStates += 1
			hashVal = previousState["hash"]
			selectedKeys = previousState["selected"]
			remainingKeys = previousState["remaining"]
			#either randomly remove a fundamental, or add one
			#don't want too few fundamentals, so only remove if there are more than 4
			#also don't want too many, so limit it at like 45
			if ((len(selectedKeys) > 4 and util.flipCoin(0.5)) or len(selectedKeys) > 45):
				randomKey = random.sample(selectedKeys, 1)[0]
				while (randomKey == '"PRICE"'):
					randomKey = random.sample(selectedKeys, 1)[0]
				selectedKeys.remove(randomKey)
				remainingKeys.append(randomKey)
			else:
				randomKey = random.sample(remainingKeys, 1)[0]
				selectedKeys.append(randomKey)
				remainingKeys.remove(randomKey)

			hashVal = hash(tuple(selectedKeys))
			if (hashVal not in visited):
				repeatedState = False
			if (numRepeatedStates >=1000):
				selectedKeys = random.sample(keys, 10)
				selectedKeys += ['"PRICE"']
				remainingKeys = [key for key in keys if key not in selectedKeys]

	# print ("FINAL SOLUTION")
	print (maxState)