Exemplo n.º 1
0
def fixedRadiusBIPV19(nColors, nCenters, nPoints, p, graph, radius, flowers=False):
	assert len(nPoints) == nColors and len(p) == nColors and len(graph) == nColors
	# Solve LP
	x, z, success = solveLPc(nColors, nCenters, nPoints, p, graph, radius)
	if not success:
		return full((nCenters, 2), -1), False
	
	# Build fractional centers
	x, z, S, D = buildFractional(nColors, nPoints, graph, radius, x, z)	

	# Make integral
	for v in range(sum(nPoints)):
		if S[v] and 1e-6 <= x[v] and x[v] <= 1 - 1e-6:
			for w in range(sum(nPoints)):
				if w != v and S[w] and 1e-6 <= x[w] and x[w] <= 1 - 1e-6:
					xsum = x[v] + x[w]
					if len(D[v]) >= len(D[w]):
						x[v] = min(1.0, xsum)
						x[w] = xsum - x[v]
					else:
						x[w] = min(1.0, xsum)
						x[v] = xsum - x[w]						
					for u in D[v]:
						z[u] = x[v]
					for y in D[w]:
						z[y] = x[w]
	for v in range(sum(nPoints)):
		if S[v] and 1e-6 <= x[v]:
			x[v] = 1.0
			for u in D[v]:
				z[v] = x[v]
		else:
			x[v] = 0.0
	for v in range(sum(nPoints)):
		if S[v] and x[v] == 0:
			S[v] = 0

	# Create and test resulting centers
	assert sum(x) <= nCenters + 1e-6 and sum(x) >= 1 - 1e-6
	centerIds = full((nCenters, 2), -1)
	centerId = 0
	nPointsCovered = zeros(nColors)
	for v in range(sum(nPoints)):
		if x[v]:
			assert x[v] == 0.0 or x[v] == 1.0
			centerIds[centerId] = [getColor(nPoints, v), getPId(nPoints, v)]
			centerId += 1
			for u in D[v]:
				nPointsCovered[getColor(nPoints, u)] += 1
	return centerIds, (nPointsCovered >= p)
Exemplo n.º 2
0
def getqP(nPoints, graph, guessedCenters, radius):
    q = []
    P = [[1 for n in range(sum(nPoints))]]
    for i in range(3):
        gC = guessedCenters[i]
        pCol = getColor(nPoints, gC)
        pId = getPId(nPoints, gC)
        # find q_i
        bestGain = -1
        bestq = [-1, -1]
        for qCol in range(len(nPoints)):
            for qId in range(nPoints[qCol]):
                if graph[pCol][pId][qCol][qId] <= radius: # q in B(p)
                    gain = getGain(nPoints, graph, pCol, pId, qCol, qId, P[i], radius)
                    if gain > bestGain:
                        bestGain = gain
                        bestq = [qCol, qId]
        assert bestq[1] != -1
        q.append(bestq)

        # Build P_(i+1)
        P_ip1 = copy(P[i])
        qCol, qId = bestq
        for vCol in range(len(nPoints)):
            for vId in range(nPoints[vCol]):
                if graph[qCol][qId][vCol][vId] <= radius:
                    for wCol in range(len(nPoints)):
                        for wId in range(nPoints[wCol]):
                            if graph[vCol][vId][wCol][wId] <= radius:
                                P_ip1[simpleId(nPoints, wCol, wId)] = 0
        P.append(P_ip1)
    return q, P, bestGain