Skip to content

[BUG] Unable to plot PCP figure properly #752

@muazhari

Description

@muazhari

Checklist

  • I searched existing issues
  • I'm using the latest pymoo version

Bug Description

Unable to plot PCP figure properly.
Image

Minimal Code to Reproduce

# We simulate a dataset of social media posts, including features that represent the content and engagement metrics.
np.random.seed(1)

n = 1000
data = {
    'content_id': range(n),
    'source_influence': np.random.uniform(0.01, 1.0, n),
    'amplifier_effect': np.random.uniform(0.01, 1.0, n),
    'banter_strength': np.random.uniform(0.01, 1.0, n),
    'has_fst_intervention': np.random.choice([0, 1], n, p=[0.8, 0.2]),
    'likes': np.zeros(n),
    'shares': np.zeros(n),
    'comments': np.zeros(n),
    'views': np.zeros(n),
    'engagement_score': np.zeros(n),
}
df = pd.DataFrame(data)

# Simulate engagement based on the features
for i, row in df.iterrows():
    if row['has_fst_intervention'] == 1:
        df.loc[i, 'source_influence'] *= np.random.uniform(1.5, 3) 
        df.loc[i, 'amplifier_effect'] *= np.random.uniform(1.5, 3) 
        df.loc[i, 'banter_strength'] *= np.random.uniform(1.5, 3) 
    base_engagement = df.loc[i, 'source_influence'] * df.loc[i, 'amplifier_effect'] * df.loc[i, 'banter_strength']
    df.loc[i, 'likes'] = int(base_engagement * np.random.randint(100, 1000))
    df.loc[i, 'shares'] = int(base_engagement * np.random.randint(50, 500))
    df.loc[i, 'comments'] = int(base_engagement * np.random.randint(20, 200))
    df.loc[i, 'views'] = int(base_engagement * np.random.randint(1000, 10000))
    df.loc[i, 'engagement_score'] = df.loc[i, 'views'] * 0.4 + df.loc[i, 'likes'] * 0.3 + df.loc[i, 'comments'] * 0.2 + df.loc[i, 'shares'] * 0.1

class ContentStrategyProblem(ElementwiseProblem):
    def __init__(self, **kwargs):
        super().__init__(n_var=4, n_obj=2, xl=np.array([0.01, 0.01, 0.01, 0.0]), xu=np.array([1.0, 1.0, 1.0, 1.0]), **kwargs)

    def _evaluate(self, x, out, *args, **kwargs):
        source_influence, amplifier_effect, banter_strength, has_fst_intervention = x
        
        # Objective 1: Maximize engagement (we'll use a simplified formula)
        base_engagement = 1.5 if has_fst_intervention else 1
        engagement_score = (source_influence * amplifier_effect * banter_strength) * base_engagement
        
        # Objective 2: Minimize negative sentiment (hypothetical)
        negative_sentiment = 0.5 * has_fst_intervention + 0.1 * (1 - source_influence)

        # negate maximization objective to be minimization. Pymoo only supports minimization operations. 
        out["F"] = [-engagement_score, negative_sentiment]

pool = multiprocessing.Pool()
runner = StarmapParallelization(pool.starmap)
optimization_problem = ContentStrategyProblem(
    elementwise_runner=runner
)

agemoea2_algorithm = AGEMOEA2()
smsemoa_algorithm = SMSEMOA()

termination = get_termination("time", "00:01:00")
result = minimize(
    problem=optimization_problem,
    algorithm=smsemoa_algorithm,
    verbose=True,
    seed=1
)

plot_pcp = PCP()
plot_pcp.add(result.F, color="blue")
plot_pcp.add(result.F[decision_index], color="green")
plot_pcp.show()

Error Message

Cannot plot a one dimensional array.
Cannot plot a one dimensional array.
Cannot plot a one dimensional array.
Cannot plot a one dimensional array.
Cannot plot a one dimensional array.
<pymoo.visualization.pcp.PCP at 0x7b8875de2cd0>

PyMoo & Python Version

pymoo @ git+https://github.com/anyoptimization/pymoo@main & python3.11

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions