def method_1(self):
		#creates a distance matrix that essentially is a list containing lists with the property where matrix[i][j] is the distance between point i and point j
		t0 = dt.time()#starts to time the method until its completion
		matrix = []
		meth1sol = []
		for i in li:
			r = []
			for j in li:
				r.append(i.distance(j))
			matrix.append(r)
		n = len(matrix)
		V = range(n)
		E = [(i,j) for i in V for j in V if i!=j]
		#the algorithm that eliminates invalid subtours
		pm.begin('subtour elimination')
		x = pm.var('x', E, bool)
		#minimizes the sum of the distances found in the matrix
		pm.minimize(sum(matrix[i][j]*x[i,j] for i,j in E), 'dist')
		for k in V:
			sum( x[k,j] for j in V if j!=k ) == 1
			sum( x[i,k] for i in V if i!=k ) == 1
			#calls the solver method and deactivates the result message 
			pm.solver(float, msg_lev = pm.glpk.GLP_MSG_OFF)
			pm.solver(int, msg_lev= pm.glpk.GLP_MSG_OFF)
			pm.solve()
		global subtourg
		#the function that creates subtours
		def subtourl(x):
			succ = 0
			subt = [succ] #start from node 0
			while True:
				succ=sum(x[succ,j].primal*j for j in V if j!=succ)
				if succ == 0: break #tour found
				subt.append(int(succ+0.5))
			return subt
		subtourg = subtourl
		while True:
			#a loop that creates subtours and keeps them if they are valid, terminating the programme in the process, or discards them if they are not
		   subt = subtourg(x)
		   if len(subt) == n:
		      #print("Optimal tour length: %g"%pm.vobj())
		      #print("Optimal tour:"); print(subt)
		      break
		   print("New subtour: %r"% subt)
		   if len(subt) == 1: break #something wrong
		   #now add a subtour elimination constraint:
		   nots = [j for j in V if j not in subt]
		   sum(x[i,j] for i in subt for j in nots) >= 1
		   pm.solve() #solve the IP problem again
		pm.end()
		#print(subt)
		#now the solution is added to a list that can be interpreted by the connect method
		for i in subt:
			meth1sol.append(li[i])
		print(len(meth1sol))
		self.connect(meth1sol, method1_colour, 1)
		t1 = dt.time()
		t = t1 - t0
		t = round(t, 2)#the required time is calculated and rounded for conviniency
		self.t1.set("time:\n{}s".format(t))
Пример #2
0
def ppSolver(expectedReturn, numberClients, numberChannels, numberProducts,
             cost, budget, channelCap, minOfferProduct, maxOfferProduct,
             rurdleRate):

    startTime = timeit.default_timer()
    rNumberClients = range(numberClients)
    rNumberChannels = range(numberChannels)
    rNumberProducts = range(numberProducts)

    t = pp.iprod(rNumberClients, rNumberChannels, rNumberProducts)
    pp.begin('basic')  # begin modelling
    pp.verbose(False)  # be verbose

    x = pp.var('choice', t, bool)

    pp.maximize(sum(x[i,j,k]*expectedReturn[i][j][k] for i in rNumberClients\
                 for j in rNumberChannels for k in rNumberProducts))

    #channelLimitConstraint:
    for j in rNumberChannels:
        sum(x[i,j,k] for i in rNumberClients for k in rNumberProducts)\
        <=channelCap[j]

    #maxOfferProductConstraint:
    for k in rNumberProducts:
        sum(x[i,j,k] for i in rNumberClients for j in rNumberChannels)\
        <=maxOfferProduct[k]

    #minOfferProductConstraint:


#    for k in rNumberProducts:
#        sum(x[i,j,k] for i in rNumberClients for j in rNumberChannels)\
#        >=minOfferProduct[k]

#budgetConstraint:

    pp.st(sum(x[i,j,k]*cost[j] for i in rNumberClients for j in\
        rNumberChannels for k in rNumberProducts)<=budget,"Budget Constr.")

    #clientLimitConstraint:

    for i in rNumberClients:
        pp.st(sum(x[i,j,k] for j in rNumberChannels for k in rNumberProducts)\
              <=1,"Client "+str(i)+" limit")

    #rurdleRateConstraint:

    pp.st(sum(x[i,j,k]*expectedReturn[i][j][k] for i in rNumberClients for j \
          in rNumberChannels for k in rNumberProducts)>= (1+rurdleRate)\
            *sum(x[i,j,k]*cost[j] for i in rNumberClients for j in\
                rNumberChannels for k in rNumberProducts),"Rurdle Rate Constr")

    pp.solve()  # solve the model

    #    pp.sensitivity() # sensitivity report
    endTime = timeit.default_timer() - startTime
    print("Objetivo encontrado: ", round(pp.vobj(), 2), " em ",
          round(endTime, 3), " segundos")

    print("\n\n\n")
    appendCsv(numberClients, "Solver method", endTime, True,
              round(pp.vobj(), 2))
    pp.end()  #Good habit: do away with the model
Пример #3
0
def summerize(tweets_df):
    print(len(tweets_df))
    #print(tweets_df['tweet_texts'][1])

    tf_idf.compute_tf_idf(tweets_df)
    term_matrix = np.load('term_matrix.npy')
    vocab_to_idx = np.load('vocab_to_idx.npy', allow_pickle=True).item()
    content_vocab = list(np.load('content_vocab.npy'))
    # tfidf_dict = np.load('tfidf_dict.npy', allow_pickle=True).item()

    print("1 ##################")

    spacy_tweets = []

    for doc in nlp.pipe(tweets_df['tweet_texts'].astype('unicode'),
                        n_threads=-1):
        spacy_tweets.append(doc)
    spacy_tweets = [tweet for tweet in spacy_tweets if len(tweet) > 1]
    # spacy_tweets = np.random.choice(spacy_tweets, 10, replace=False)
    # spacy_tweets = spacy_tweets[:20]
    print(len(spacy_tweets))
    print(spacy_tweets[0])

    print("2 ##################")

    all_bigrams = [
        list(bigrams([token.lemma_ for token in tweets]))
        for tweets in spacy_tweets
    ]
    starting_nodes = [single_bigram[0] for single_bigram in all_bigrams]
    end_nodes = [single_bigram[-1] for single_bigram in all_bigrams]
    all_bigrams = [
        node for single_bigram in all_bigrams for node in single_bigram
    ]
    all_bigrams = list(set(all_bigrams))
    print("all_bigrams len=", len(all_bigrams))
    print(all_bigrams[0])

    print("3 ##################")

    # bigram_graph = make_bigram_graph(all_bigrams, starting_nodes[1])
    # print(len(bigram_graph))
    # print(bigram_graph)
    # path = breadth_first_search(bigram_graph, starting_nodes[1], end_nodes[2])
    # print(path)

    bigram_paths = []

    for single_start_node in tqdm(starting_nodes):
        bigram_graph = make_bigram_graph(all_bigrams, single_start_node)
        for single_end_node in end_nodes:
            possible_paths = breadth_first_search(bigram_graph,
                                                  single_start_node,
                                                  single_end_node)
            for path in possible_paths:
                bigram_paths.append(path)
    print("bigram_paths len=", len(bigram_paths))
    # print(bigram_paths[10])

    # for tweet in spacy_tweets:
    #     bigram_paths.append(list(bigrams([token.lemma_ for token in tweets])))
    word_paths = []
    for path in tqdm(bigram_paths):
        word_paths.append(make_list(path))
    print(word_paths[0])

    print("4 ##################")

    mp.begin('COWABS')
    # Defining my first variable, x
    # This defines whether or not a word path is selected
    x = mp.var(str('x'), len(word_paths), bool)
    # Also defining the second variable, which defines
    # whether or not a content word is chosen
    y = mp.var(str('y'), len(content_vocab), bool)

    mp.maximize(
        sum([
            linguistic_quality(word_paths[i]) *
            informativeness(word_paths[i], term_matrix, vocab_to_idx) * x[i]
            for i in range(len(x))
        ]) + sum(y))
    # hiding the output of this line since its a very long sum
    # sum([x[i] * len(word_paths[i]) for i in range(len(x))]) <= 150

    for j in range(len(y)):
        sum([
            x[i]
            for i in paths_with_content_words(j, word_paths, content_vocab)
        ]) >= y[j]

    for i in range(len(x)):
        sum(y[j] for j in content_words(i, word_paths, content_vocab)) >= len(
            content_words(i, word_paths, content_vocab)) * x[i]
    mp.solve()
    result_x = [value.primal for value in x]
    result_y = [value.primal for value in y]
    mp.end()

    chosen_paths = np.nonzero(result_x)
    chosen_words = np.nonzero(result_y)
    print("*** Total = ", len(chosen_paths[0]))

    min_cosine_sim = 999
    final_sentence = None
    for i in chosen_paths[0]:
        print('--------------')
        print(str(" ").join([token for token in word_paths[i]]))
        cosine_sim = informativeness(word_paths[i], term_matrix, vocab_to_idx)
        print(cosine_sim)
        if min_cosine_sim > cosine_sim:
            min_cosine_sim = cosine_sim
            final_sentence = str(" ").join([token for token in word_paths[i]])

    # print("####### Summary ###########")
    # print(final_sentence)

    return final_sentence